We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create chart and add in word from excel data aspose c#

hi, how can I create a bar chart from excel data and embed that it on word doc in C#. Help is greatly appreciated thanks.

@kiran5388,

You can create chart based on Excel data (either from the scratch by inserting data (in new workbook) or based on existing data in the Excel spreadsheet) via Aspose.Cells APIs. Then render chart to image file via Aspose.Cells APIs. Now use Aspose.Words API to insert the chart image to your document for your needs. See the sample code on how to create bar chart from the scratch for your reference, the code also renders the chart to image at the bottom:
e.g.
Sample code:

            //Create a new Workbook.
            Workbook workbook = new Workbook();

            //Get the first worksheet.
            Worksheet sheet = workbook.Worksheets[0];

            //Set the name of worksheet
            sheet.Name = "Data";

            //Get the cells collection in the sheet.
            Cells cells = workbook.Worksheets[0].Cells;

            //Put some values into a cells of the Data sheet.
            cells["A1"].PutValue("Region");
            cells["A2"].PutValue("A");
            cells["A3"].PutValue("B");
            cells["A4"].PutValue("C");
            cells["A5"].PutValue("D");
            cells["A6"].PutValue("E");
            cells["A7"].PutValue("F");
            cells["A8"].PutValue("G");
            cells["B1"].PutValue("Sale");
            cells["B2"].PutValue(70000);
            cells["B3"].PutValue(55000);
            cells["B4"].PutValue(30000);
            cells["B5"].PutValue(40000);
            cells["B6"].PutValue(35000);
            cells["B7"].PutValue(32000);
            cells["B8"].PutValue(10000);

            //Add a chart sheet.
            int sheetIndex = workbook.Worksheets.Add(SheetType.Chart);
            sheet = workbook.Worksheets[sheetIndex];

            //Set the name of worksheet
            sheet.Name = "Chart";

            //Create chart
            int chartIndex = 0;
            chartIndex = sheet.Charts.Add(Aspose.Cells.Charts.ChartType.Bar, 1, 1, 25, 10);
            Aspose.Cells.Charts.Chart chart = sheet.Charts[chartIndex];

            //Set properties of chart title
            chart.Title.Text = "Sales By Region";
            chart.Title.TextFont.Color = Color.Blue;
            chart.Title.TextFont.IsBold = true;
            chart.Title.TextFont.Size = 12;

            //Set properties of nseries
            chart.NSeries.Add("Data!B2:B8", true);
            chart.NSeries.CategoryData = "Data!A2:A8";
            chart.NSeries.IsColorVaried = true;

            //Set the DataLabels in the chart
            Aspose.Cells.Charts.DataLabels datalabels;
            for (int i = 0; i < chart.NSeries.Count; i++)
            {
                datalabels = chart.NSeries[i].DataLabels;
               
            }

         //Render chart to image
         chart.ToImage("e:\\test2\\out1chart1.png", ImageFormat.Png);

See the document on how to insert image into the document via Aspose.Words API for your reference.

Hope, this helps a bit.

this is very nice feature. Thank you.
But is it possible the charts should editable in word instead of images?

@kiran5388,

You can embed Excel workbook/file (containing the chart) as OLE object into MS Word document. See the Aspose.Words for .NET API reference page for the purpose:

Also, see the document with some general examples on inserting Ole Objects for your reference:

Hope, this helps a bit.