我正在创建一个VSTO插件,其中包括创建一份年度数据的折线图。这些数据每周都有数据点。我希望横轴按月分组,如下所示:
但是我在VSTO文档中找不到任何关于是否可能实现此功能的内容。据我所知,Series 只接受X轴的一维数组值。有人有相关经验吗?
在 Microsoft 论坛的帮助下,我找到了一个解决方案(MSDN 线程)。
对于其他想知道怎么做的人,这是我最终得出的结果:
Chart chart = slide.Shapes.AddChart(XlChartType.xlLine).Chart;
Excel.Workbook wb = (Excel.Workbook) chart.ChartData.Workbook;
Excel.Worksheet ws = wb.Worksheets[1];
ws.ListObjects["Table1"].Resize(ws.Range["A1:C13"]);
ws.Range["B1"].Value = "X";
ws.Range["A2"].Value = "2015";
ws.Range["A3:A7"].ClearContents();
ws.Range["A8"].Value = "2016";
ws.Range["A9:A13"].ClearContents();
ws.Range["B2"].Value = "7/15/2015";
ws.Range["B3"].Value = "8/15/2015";
ws.Range["B2:B3"].AutoFill(Destination: ws.Range["B2:B13"]);
ws.Range["B2:B13"].NumberFormat = "d-mmm";
ws.Range["C1"].Value = "Y";
ws.Range["C2"].Value = "100";
ws.Range["C3"].Value = "150";
ws.Range["C2:C3"].AutoFill(Destination: ws.Range["C2:C13"]);
(chart.SeriesCollection(1) as Series).Delete();
Series series = chart.SeriesCollection(1) as Series;
series.Values = "=Sheet1!$C$2:$C$13";
series.XValues = "=Sheet1!$A$2:$B$13";