How to generate a chart from an Excel sheet?

前端 未结 1 1231
暖寄归人
暖寄归人 2020-12-10 21:17

I can generate an Excel file when I pass a datatable into the below function:

public static void ExportDataTableToExcel(DataTable dt, string fil         


        
相关标签:
1条回答
  • 2020-12-10 22:02

    Take a look at the tutorial here (one of the first Google hits).

    It very clearly describe how to make a simple chart in Excel from C# code.

    The general idea is like this:

    // Add chart.
    var charts = worksheet.ChartObjects() as
        Microsoft.Office.Interop.Excel.ChartObjects;
    var chartObject = charts.Add(60, 10, 300, 300) as
        Microsoft.Office.Interop.Excel.ChartObject;
    var chart = chartObject.Chart;
    
    // Set chart range.
    var range = worksheet.get_Range(topLeft, bottomRight);
    chart.SetSourceData(range);
    
    // Set chart properties.
    chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
    chart.ChartWizard(Source: range,
        Title: graphTitle,
        CategoryTitle: xAxis,
        ValueTitle: yAxis);
    
    0 讨论(0)
提交回复
热议问题