C# Excel 互操作圖表列系列問題

古爾坎·奧茲德姆

我正在閱讀一個 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"));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章