【问题标题】:C# Excel Interop Chart Column Series ProblemC# Excel 互操作图表列系列问题
【发布时间】:2021-12-22 14:12:10
【问题描述】:

我正在阅读一个 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

正确输出示例图片

错误的输出示例图片

应该看起来像这张照片

【问题讨论】:

    标签: c# excel row yaxis x-axis


    【解决方案1】:

    已解决

    问题:程序无法确定我的值(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"));
    

    【讨论】:

    • 我试图解释解决方案,如果您有任何疑问,请再次告诉我。我会尽力而为。 SetSourceData Explanation
    • 没问题。现在完美了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2013-07-25
    • 2010-10-21
    • 2013-01-15
    相关资源
    最近更新 更多