所以我一直在尝试解决这个问题,但似乎无处可去。我正在使用 C# 创建一个 Excel 电子表格。我的电子表格包含一个图表。除了更改 X 轴标签外,我可以用图表做所有事情。我已经尝试了几乎所有我能找到的东西,但没有任何效果。
Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlChart.Add(1420, 660, 320, 180);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Title Text";
chartPage.HasLegend = false;
var yAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Y-Axis Title text";
yAxis.MaximumScale = 20;
yAxis.AxisTitle.Orientation = Excel.XlOrientation.xlUpward;
Excel.Range Data_Range = xlWorkSheet.get_Range("A10", "C10");//Data to be plotted in chart
Excel.Range XVal_Range = xlWorkSheet.get_Range("A1", "C1");//Catagory Names I want on X-Axis as range
var x_labels = new List<string>() { "Val1", "Val2", "Val3" }; //Catagory Names I want on X-Axis as text array
Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(misValue);
Excel.Series Data = oSeriesCollection.NewSeries();
Data.Values = Data_Range;
Data.Name = "Plot Data";
Excel.Axis valueAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
/*
Methods I've tried with no luck:
1) Data.XValues = XVal_Range;
2) Data.XValues = x_labels.ToArray();
3) valueAxis.CategoryNames = x_labels.ToArray();
4) valueAxis.CategoryNames = XVal_Range;
*/
每次它只显示默认数值......我很茫然