C# Excel Interop Chart Column Series 问题
C# Excel Interop Chart Column Series Problem
我正在读取一个 16 行、270 列的 Excel 文件。经过一些算法和比较,我创建了一个新的 Excel 并在新的 Excel 中创建了一个新图表。当 Excel 包含超过 16 列时,图表不正确。
这是我的图表创建代码;
xlRange2 = xlWorksheet2.UsedRange;
xlRange2.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
xlRange2.Borders.Weight = Excel.XlBorderWeight.xlThin;
rowCount2 = xlRange2.Rows.Count;
colCount2 = xlRange2.Columns.Count;
Excel.ChartObjects xlCharts2 = (Excel.ChartObjects)xlWorksheet2.ChartObjects(Type.Missing);
xlRange2 = xlWorksheet2.Range[xlWorksheet2.Cells[1, 2], xlWorksheet2.Cells[16, colCount2]]; //Here is my Y-Axis Values and Series Names
Excel.Chart ct2 = xlWorksheet2.Shapes.AddChart(null, 1, 275, 650, 350).Chart;
var missing = System.Type.Missing;
ct2.ChartWizard(xlRange2, Excel.XlChartType.xlLineMarkers, missing, missing, missing, missing, missing, missing, "Frequency[Hz]", "Absorption Coefficient[-]", missing);
Excel.Series oSeries2 = (Excel.Series)ct2.SeriesCollection(1);
oSeries2.XValues = xlWorksheet2.get_Range("A2", "A16"); //Here is my X-Axis Values
正确输出示例图片:
输出示例图片错误:
应该像这张图片:
已解决
问题:程序无法确定我的值(Y 轴或我的项目吸收系数)是行还是列。 Excel 是用自己的算法来做的。当我使用这段代码时,我的问题已经解决了。
ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
所有带解释的代码;
xlRange2 = xlWorksheet2.UsedRange; //Compute used range in excel file
xlRange2.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; //Draw cell borders
xlRange2.Borders.Weight = Excel.XlBorderWeight.xlThin;
rowCount2 = xlRange2.Rows.Count; //Count used rows
colCount2 = xlRange2.Columns.Count; //Count used columns
//Add empty xlLineMarkers chart type
//Location (x,y) = (1,275)
//Size: (650,350)
Excel.Shape chart_shape=xlWorksheet2.Shapes.AddChart(Excel.XlChartType.xlLineMarkers, 1, 275, 650, 350);
//Define chart with shape
Excel.Chart ct2 = chart_shape.Chart;
//This code provide the source data range which was B1 to B16 and end of the column range
Excel.Range chart_range = xlWorksheet2.Range[xlWorksheet2.Cells[1, 2], xlWorksheet2.Cells[16, colCount2]];
//This line is the solution of my problem, my source(Y axis values) data are columns...
//...because of this reason I use ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
//If your sources are on the row you should use ct2.SetSourceData(chart_range, Excel.XlRowCol.xlRows);
ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
//Set the X axis values
//For me A2 to A16
Excel.Range axis_range = xlWorksheet2.get_Range("A2", "A16");
Excel.Series series = (Excel.Series)ct2.SeriesCollection(1);
series.XValues = axis_range;
//Y Axis Label Configuration
Excel.Axis axis = (Excel.Axis)ct2.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = "Absorption Coefficient [-]";
//X Axis Label
Excel.Axis Xaxis = (Excel.Axis)ct2.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
Xaxis.HasTitle = true;
Xaxis.AxisTitle.Text = "Frequency [Hz]";
//If you want to see the chart on pictureBox use following code
//In addition, you can use 'null' instead of 'misValue'
object misValue = System.Reflection.Missing.Value;
//Export Chart as a picture into the project folder
//Such as C:\Users\....\bin\Debug\net5.0-windows
ct2.Export((Directory.GetCurrentDirectory() + "\excelChartV5.bmp"), "BMP", misValue);
//To show in pictureBox the exported picture
pictureBox1.Image = new Bitmap((Directory.GetCurrentDirectory() + "\excelChartV5.bmp"));
我正在读取一个 16 行、270 列的 Excel 文件。经过一些算法和比较,我创建了一个新的 Excel 并在新的 Excel 中创建了一个新图表。当 Excel 包含超过 16 列时,图表不正确。
这是我的图表创建代码;
xlRange2 = xlWorksheet2.UsedRange;
xlRange2.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
xlRange2.Borders.Weight = Excel.XlBorderWeight.xlThin;
rowCount2 = xlRange2.Rows.Count;
colCount2 = xlRange2.Columns.Count;
Excel.ChartObjects xlCharts2 = (Excel.ChartObjects)xlWorksheet2.ChartObjects(Type.Missing);
xlRange2 = xlWorksheet2.Range[xlWorksheet2.Cells[1, 2], xlWorksheet2.Cells[16, colCount2]]; //Here is my Y-Axis Values and Series Names
Excel.Chart ct2 = xlWorksheet2.Shapes.AddChart(null, 1, 275, 650, 350).Chart;
var missing = System.Type.Missing;
ct2.ChartWizard(xlRange2, Excel.XlChartType.xlLineMarkers, missing, missing, missing, missing, missing, missing, "Frequency[Hz]", "Absorption Coefficient[-]", missing);
Excel.Series oSeries2 = (Excel.Series)ct2.SeriesCollection(1);
oSeries2.XValues = xlWorksheet2.get_Range("A2", "A16"); //Here is my X-Axis Values
正确输出示例图片:
输出示例图片错误:
应该像这张图片:
已解决
问题:程序无法确定我的值(Y 轴或我的项目吸收系数)是行还是列。 Excel 是用自己的算法来做的。当我使用这段代码时,我的问题已经解决了。
ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
所有带解释的代码;
xlRange2 = xlWorksheet2.UsedRange; //Compute used range in excel file
xlRange2.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; //Draw cell borders
xlRange2.Borders.Weight = Excel.XlBorderWeight.xlThin;
rowCount2 = xlRange2.Rows.Count; //Count used rows
colCount2 = xlRange2.Columns.Count; //Count used columns
//Add empty xlLineMarkers chart type
//Location (x,y) = (1,275)
//Size: (650,350)
Excel.Shape chart_shape=xlWorksheet2.Shapes.AddChart(Excel.XlChartType.xlLineMarkers, 1, 275, 650, 350);
//Define chart with shape
Excel.Chart ct2 = chart_shape.Chart;
//This code provide the source data range which was B1 to B16 and end of the column range
Excel.Range chart_range = xlWorksheet2.Range[xlWorksheet2.Cells[1, 2], xlWorksheet2.Cells[16, colCount2]];
//This line is the solution of my problem, my source(Y axis values) data are columns...
//...because of this reason I use ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
//If your sources are on the row you should use ct2.SetSourceData(chart_range, Excel.XlRowCol.xlRows);
ct2.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);
//Set the X axis values
//For me A2 to A16
Excel.Range axis_range = xlWorksheet2.get_Range("A2", "A16");
Excel.Series series = (Excel.Series)ct2.SeriesCollection(1);
series.XValues = axis_range;
//Y Axis Label Configuration
Excel.Axis axis = (Excel.Axis)ct2.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = "Absorption Coefficient [-]";
//X Axis Label
Excel.Axis Xaxis = (Excel.Axis)ct2.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
Xaxis.HasTitle = true;
Xaxis.AxisTitle.Text = "Frequency [Hz]";
//If you want to see the chart on pictureBox use following code
//In addition, you can use 'null' instead of 'misValue'
object misValue = System.Reflection.Missing.Value;
//Export Chart as a picture into the project folder
//Such as C:\Users\....\bin\Debug\net5.0-windows
ct2.Export((Directory.GetCurrentDirectory() + "\excelChartV5.bmp"), "BMP", misValue);
//To show in pictureBox the exported picture
pictureBox1.Image = new Bitmap((Directory.GetCurrentDirectory() + "\excelChartV5.bmp"));