Export chart?

Hello,

Is it possible to programmatically export a chart as a .gif, .jpg or any other image format? For example, the following code will draw me a chart, is it possible to save the chart off as an image?:

int chartIndex = excel.Worksheets[0].Charts.Add(ChartType.Column3DClustered, 10, 1, 31, 14);

Chart chart = excel.Worksheets[0].Charts[0];

chart.NSeries.Add(string.Format(@"C2:G9}"), true);
chart.NSeries.CategoryData = string.Format(@"A2:B9");

chart.NSeries[0].Name = "=C1";
chart.NSeries[1].Name = "=D1";
chart.NSeries[2].Name = "=E1";
chart.NSeries[3].Name = "=F1";
chart.NSeries[4].Name = "=G1";

// chart.SaveAs("c:\mychart.jpg") - something like this do-able ?

This feature is not supported. We had though about this feature but it's too complex and we don't have enough information.

Hi Laurence,

Thanks for the answer & help; is it possible to save in HTML format then ?

Do you mean save an Excel file in HTML format? Currently the answer is no.

You can try File Format APIs for .NET Core, Java, Python, C++, Android | products.aspose.com. It may serve you need if you want to find a web spreadsheet component.

@stevepuri,
Aspose.Excel is discontinued now and no more available. It is replaced by the latest product Aspose.Cells that supports rich features to manipulate charts and then render them to image or convert into HTML. Here is a sample code that creates a chart and then saves it as image and HTML.

// 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[0];

// 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
Worksheet ws2 = workbook.Worksheets[sheetIndex];
int chartIndex = ws2.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 0, 0, 15, 5);


// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = ws2.Charts[chartIndex];

// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.ImageType = Aspose.Cells.Drawing.ImageType.Jpeg;
chart.ToImage("output.jpeg", imageOrPrintOptions);
workbook.Worksheets.ActiveSheetIndex = 1;
            
// Access the sheet
Worksheet ws = workbook.Worksheets[1];

// Set the print area.            
ws.PageSetup.PrintArea = "A1:O5";

// Initialize HtmlSaveOptions
HtmlSaveOptions options = new HtmlSaveOptions();

// Set flag to export print area only
options.ExportPrintAreaOnly = true;
options.ExportActiveWorksheetOnly = true;

//Save to HTML format
workbook.Save("outputInlineCharts.html", options);

For more information about working with charts, have a look at the following article:
Chart

Get a free trial version here for testing this new product:
Aspose.Cells for .NET (Latest Version)

A comprehensive detailed solution is available here which contains hundreds of ready-to-run examples for testing different features of this product.