Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-kb/e6efab8db9b21326f66b64b91e5b554f to your computer and use it in GitHub Desktop.
Save aspose-com-kb/e6efab8db9b21326f66b64b91e5b554f to your computer and use it in GitHub Desktop.
This sample code creates a worksheet with chart and then Saves Excel chart as JPG. For more details refer to https://kb.aspose.com/cells/net/how-to-convert-excel-chart-to-jpg-in-csharp-without-interop/
using Aspose.Cells;
using Aspose.Cells.Charts;
using Aspose.Cells.Rendering;
namespace ConvertExcelChartToJPGInCSharpWithoutInterop
{
class Program
{
static void Main(string[] args)
{
//Initialize the license object
License license = new License();
//Set license by providing path to license file
license.SetLicense("Aspose.Cells.lic");
//Initialize a workbook object
Workbook ChartToImageWorkbook = new Workbook();
//Get the first worksheet
Worksheet ChartWorksheet = ChartToImageWorkbook.Worksheets[0];
//Insert sample data into worksheet
ChartWorksheet.Cells[23, 2].PutValue(2014);
ChartWorksheet.Cells[24, 2].PutValue(2015);
ChartWorksheet.Cells[25, 2].PutValue(2016);
ChartWorksheet.Cells[26, 2].PutValue(2017);
ChartWorksheet.Cells[27, 2].PutValue(2018);
ChartWorksheet.Cells[28, 2].PutValue(2019);
ChartWorksheet.Cells[29, 2].PutValue(2020);
ChartWorksheet.Cells[23, 3].PutValue(3);
ChartWorksheet.Cells[24, 3].PutValue(4);
ChartWorksheet.Cells[25, 3].PutValue(9);
ChartWorksheet.Cells[26, 3].PutValue(13);
ChartWorksheet.Cells[27, 3].PutValue(16);
ChartWorksheet.Cells[28, 3].PutValue(3);
ChartWorksheet.Cells[29, 3].PutValue(7);
//Create a bar chart
Chart chart = ChartWorksheet.Charts[ChartWorksheet.Charts.Add(ChartType.Bar , 1, 1, 22, 12)];
//Add chart series and set its properties
Series aSerie = chart.NSeries[chart.NSeries.Add("D24:D30", true)];
chart.Title.Text = "Company Yearly Sales";
aSerie.XValues = "C24:C30";
aSerie.Name = "Sale in Millions";
//Set image options
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.ImageType = Aspose.Cells.Drawing.ImageType.Jpeg;
//Save the image as JPEG
chart.ToImage("ExcelChartToJPG.jpg", imageOrPrintOptions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment