首页 文章

C#Excel Interop图表,多系列 - X轴为DateTime

提问于
浏览
0

我有一个用C#实现的excel插件 . 这连接到服务器并获得对,其中key是DateTime . 获取数据后,将在Excel工作表中填充此数据 . 该数据用于绘制多系列线图 . 我正在使用ChartWizard,在Chart对象上我使用.NewSeries选项添加了多个系列 . 指定我的X轴是DateTime的最佳方法是什么 . 当我使用ScatterLines或XLLines时,我看到行为有所不同 . 我无法找到一种方法,我可以对图表说我的X轴是DateTime并指定dateformat to same任何帮助将不胜感激 . Vikron

Microsoft.Office.Tools.Excel.Chart chart = worksheet.Controls.AddChart(0, 0, xx, yy, "Trend");
                chart.ChartWizard(Type.Missing,
               Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterLines,
               Type.Missing,
               XlRowCol.xlColumns,
               Type.Missing,
               Type.Missing,
               true,
               "Trend",
               "Time stamp",
               Type.Missing,
               Type.Missing);
            chart.AutoScaling = false;
            chart.ChartStyle = 34
            chart.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
            chart.HasTitle = true;
            chart.ChartType = XlChartType.xlXYScatterLines;
            chart.ChartTitle.Text = ""Trend";


foreach (series)
            {
                Excel.Series xlSeries = (Excel.Series)((Excel.SeriesCollection)chart.SeriesCollection()).NewSeries();
                Microsoft.Office.Interop.Excel.Range seriesRange = worksheet.get_Range(series.seriesBeginRange, 
                                                                    worksheet.get_Range(series.seriesBeginRange).get_End(XlDirection.xlDown));
                xlSeries.Name = series.seriesName;
                xlSeries.XValues = timeSeriesRange; //This has datetime
                xlSeries.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterLines;
                xlSeries.Values = seriesRange; 
            }

cell.NumberFormat = "MM/dd/yy HH:mm";
cell.Value = ts;

1 回答

  • 0

    发现,如果不是最好的话,对我有用的东西 . 当我使用DateTime格式化为所需的日期时间显示模式时,Cell类型被设置为“General”,当然该值被视为“String”,这个xlLines确实工作得很好 . 然后问题是用户将无法将DateTime格式修改为不同的显示模式,因为单元格被更新为“String” . 现在,我使用C#DateTime对象填充Excel单元格 . 我将格式设置为

    cell.NumberFormat = "m/d/yy h:mm;@";
    

    在Excel中获取格式的AxFor => Number => Category => Format Code属性的精确NumberFormat . 这将允许Excel将类型视为时间,用户可以修改日期时间格式 . 然后,我修改为xlXLScatterLines,修改了Tickspaces,如下所示:

    Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory);
                TickLabels ticks = xAxis.TickLabels;
                xAxis.MinimumScale = timeSeriesRange.Value2[1, 1];
                xAxis.MaximumScale = timeSeriesRange.Value2[timeSeriesRange.Count, 1];
                xAxis.MajorTickMark = XlTickMark.xlTickMarkCross;
                xAxis.MinorTickMark = XlTickMark.xlTickMarkInside;
                xAxis.MajorUnit = 0.005;
                xAxis.HasMinorGridlines = true;
                xAxis.HasMajorGridlines = true;
                ticks.Orientation = XlTickLabelOrientation.xlTickLabelOrientationUpward;
                chart.PlotArea.Select();
                chart.PlotArea.Height = 300;
    

相关问题