Excel charts +C#

2019-01-28 03:18发布

I am using excel to draw charts from c#, but i need the chart to be one series related to each other not two series (when i select a range that has two columns of data)
can any one help:

        xla.Visible = true; 
        Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
        Worksheet ws = (Worksheet)xla.ActiveSheet;

        // Now create the chart.
        ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
        ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
        Chart xlChart = chartObj.Chart;
        Range rg = ws.get_Range("B2", "C17");
        xlChart.SetSourceData(chartRange, XlRowCol.xlColumns);

thanks

标签: c# excel charts
2条回答
祖国的老花朵
2楼-- · 2019-01-28 03:47

I cleaned the code up a bit and added the generation of random data so this should run on its own.

Random random = new Random();
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);

Worksheet ws = (Worksheet)xla.ActiveSheet;

// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects();
ChartObject chartObj = chartObjs.Add(150, 20, 300, 300);
Chart xlChart = chartObj.Chart;

// generate some random data
for (int row = 0; row < 16; row++)
{
    ws.Cells[row + 2, 2] = row + 1;
    ws.Cells[row + 2, 3] = random.Next(100);
}

Range xValues = ws.Range["B2", "B17"];
Range values = ws.Range["C2", "C17"];

SeriesCollection seriesCollection = xlChart.SeriesCollection();

Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values; 
查看更多
rogerwiga
3楼-- · 2020-11-09 06:03

Hey friend, try ZetExcel.com Library

查看更多
登录 后发表回答