C# を使用して Excel スプレッドシートでグラフを作成します

ブログ カテゴリ: イメージングOffice.NET

2024/05/08

Microsoft Office Excel は、スプレッドシートのデータを編集し、スプレッドシートのデータを使用して視覚化できるスプレッドシート アプリケーションです。グラフ。Excel では、縦棒グラフ、折れ線グラフ、円グラフ、横棒グラフ、面グラフ、XY グラフ、地図グラフ、株価グラフ、曲面グラフ、レーダー グラフ、ツリー マップ、サンバースト グラフ、ヒストグラム、箱ひげ図、ウォーターフォール グラフ、ファネル グラフ、コンボ グラフなどのグラフの種類がサポートされています。

VintaSoft Imaging .NET SDK + VintaSoft Office .NET Plug-in は、XLSX ファイルに保存されたスプレッドシートを操作するための強力な .NET API を提供します。
VintaSoft Imaging .NET SDK では、次の種類の Excel グラフを作成できます:

VintaSoft Imaging .NET SDK を使用して Excel スプレッドシートでグラフを作成するには、次の手順に従います。

Excel スプレッドシートに「標準」グラフを追加する方法を示した C# コードは次のとおりです。
public static void AddStandardChartToXlsxWorksheet(string outXlsxFilename)
{
    // create a spreadsheet editor for synchronous editing of new spreadsheet document
    using (SpreadsheetEditor editor = SpreadsheetEditor.CreateEditor())
    {
        // get the first worksheet (empty)
        Worksheet sheet = editor.Document.Worksheets[0];
        
        // start the worksheet editing
        WorksheetEditor worksheetEditor = editor.StartEditing(sheet);

        // set worksheet name
        worksheetEditor.SetName("Example of add chart");
        
        // set categories and series for chart
        editor.SetCellValue(sheet, "A2", "Series 1");
        editor.SetCellValue(sheet, "A3", "Series 2");
        editor.SetCellValue(sheet, "A4", "Series 3");
        editor.SetCellValue(sheet, "B1", "Category 1");
        editor.SetCellValue(sheet, "C1", "Category 2");
        editor.SetCellValue(sheet, "D1", "Category 3");
        editor.SetCellValue(sheet, "E1", "Category 4");

        // set sample values for chart
        editor.SetCellValue(sheet, "B2", 5);
        editor.SetCellValue(sheet, "C2", 15);
        editor.SetCellValue(sheet, "D2", 45.5);
        editor.SetCellValue(sheet, "E2", 35);
        editor.SetCellValue(sheet, "B3", 110);
        editor.SetCellValue(sheet, "C3", 90);
        editor.SetCellValue(sheet, "D3", 88);
        editor.SetCellValue(sheet, "E3", 25);
        editor.SetCellValue(sheet, "B4", 15);
        editor.SetCellValue(sheet, "C4", 50);
        editor.SetCellValue(sheet, "D4", 20);
        editor.SetCellValue(sheet, "E4", 45);

        // add new Line chart to the worksheet
        SheetDrawing chart = editor.AddChart(sheet, StandardChartType.Line_LineWithMarkers_Horizontal, "A1:E4", new SheetDrawingLocation("A7:F17"));
        // set the chart title
        worksheetEditor.CreateChartPropertiesEditor(chart).SetTitle("Line Chart Example (Horizontal)");

        // finish the spreadsheet editing
        editor.FinishEditing();

        // save created spreadsheet document to a XLSX file
        editor.SaveAs(outXlsxFilename);
    }
}