Search code examples
c#openxmlopenxml-sdkaspose-cells

Exporting OpenXML based Charts into images


Can I generate image from office OpenXml based chart definition? My idea is to use the program dynamically generate OpenXml definition of a chart and export it as image.


Solution

  • Aspose.Cells can

    • Generate the OpenXML or XLSX Charts,
    • Manipulate the existing Charts and
    • Export them into Image

    Exporting Chart to Image is very simple. Just call Chart.ToImage() method and you have accomplished it.

    The following sample code creates the Chart and saves the Excel file in XLSX format.

    // Instantiating a Workbook object
    Workbook workbook = new Workbook();
    
    // Adding a new worksheet to the Excel object
    int sheetIndex = workbook.Worksheets.Add();
    
    // Obtaining the reference of the newly added worksheet by passing its sheet index
    Worksheet worksheet = workbook.Worksheets[sheetIndex];
    
    // Adding sample values to cells
    worksheet.Cells["A1"].PutValue(50);
    worksheet.Cells["A2"].PutValue(100);
    worksheet.Cells["A3"].PutValue(150);
    worksheet.Cells["B1"].PutValue(4);
    worksheet.Cells["B2"].PutValue(20);
    worksheet.Cells["B3"].PutValue(50);
    
    // Adding a chart to the worksheet
    int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);
    
    // Accessing the instance of the newly added chart
    Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
    
    // Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
    chart.NSeries.Add("A1:B3", true);
    
    // Saving the Excel file
    workbook.Save(dataDir + "output.xlsx");
    

    The following sample code exports the Chart into Image.

    // Converting chart to Bitmap
    System.Drawing.Bitmap bitmap = chart.ToImage();
    bitmap.Save(dataDir + "chartBMP_out.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
    

    Note: I am working as Developer Evangelist at Aspose