如何使用c#定位Excel图表

时间:2014-12-17 07:00:21

标签: c# wpf excel

我正在使用c#和excel interop dll以编程方式开发excel图表。 我开发了这种图表。这是屏幕截图

enter image description here

但我想要图表...(新图表)

enter image description here

我在这里给出完整的代码。请看它并指导我制作新图表所需的额外代码。

Excel数据..

enter image description here

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart;
Excel.Range chartRange;
Excel.Chart chartPage;
myChart = (Excel.ChartObject)xlCharts.Add(630, 20, 300, 250);
chartRange = xlWorkSheet.Range["AW2", "BA5"];
chartPage = myChart.Chart;    
chartPage.Legend.Delete();                        
myChart.Height = 380;
myChart.Width = 565;
myChart.Chart.HasDataTable = true;
myChart.Chart.DataTable.Font.Size = 10;
myChart.Chart.DataTable.Font.Name = "+Body";
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlLineMarkers;

1 个答案:

答案 0 :(得分:0)

我解决了问题。现在只需在上面给出的代码中添加一些代码行。最后调用此函数chart_series_design(chartPage, "", "");。功能就像

public void chart_series_design(Excel.Chart chartPage,string xmsg,string ymsg)
        {
            chartPage.set_HasAxis(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary, true);
            chartPage.set_HasAxis(Excel.XlAxisType.xlSeriesAxis, Excel.XlAxisGroup.xlSecondary, true);
            chartPage.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary, true);
            chartPage.set_HasAxis(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary, true);
            chartPage.set_HasAxis(Excel.XlAxisType.xlSeriesAxis, Excel.XlAxisGroup.xlPrimary, true);
            chartPage.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary, true);


            Excel.Series series = (Excel.Series)chartPage.SeriesCollection(1);
            series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
            series.AxisGroup = Excel.XlAxisGroup.xlPrimary;

            series.Format.Line.Weight = 1.0F;
            series.Format.Line.Visible = MsoTriState.msoFalse;  //Tri-State 
            series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;


            series = (Excel.Series)chartPage.SeriesCollection(2);
            series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
            series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
            series.Format.Line.Weight = 1.0F;
            series.Format.Line.Visible = MsoTriState.msoFalse;  //Tri-State 
            series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;

            series = (Excel.Series)chartPage.SeriesCollection(3);
            series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
            series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
            series.Format.Line.Weight = 1.0F;
            series.Format.Line.Visible = MsoTriState.msoFalse;  //Tri-State 
            series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;

            series = (Excel.Series)chartPage.SeriesCollection(4);
            series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
            series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
            series.Format.Line.Weight = 1.0F;
            series.Format.Line.Visible = MsoTriState.msoFalse;  //Tri-State 
            series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;


            Excel.Axis axis;
            axis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
            axis.HasTitle = true;
            axis.AxisTitle.Text = xmsg;

            axis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
            axis.HasTitle = true;
            axis.AxisTitle.Text = ymsg;


        }