Is there Aspose Cells and Words sample code to create an Excel chart and place the chart in a Word docx?

Hi, I want to create an Excel Scatter Chart and put it in a Word docx as an Excel chart. Is there sample code for this using Aspose Cells and Words? Is there sample code to show me how to “bookmark” the place where the chart is to be placed? If Aspose doesn’t support putting a chart into word then the chart would have to be converted to an image and placed into the docx at the right place. Thanks very much!

Hi William,


Thank you for contacting support. There is no direct way to export charts. You can create an Excel Scatter chart, and then export it to an image using Aspose.Cells API. Using Aspose.Words API, you can insert an image anywhere in the Word document. Please refer to the following help topics: How to Create a Chart, Convert an Excel Chart to Image and Insert an Image in the Word Document

@wtp1000,

Using newer versions of Aspose.Cells, you can directly convert an Excel spreadsheet with charts to a Word DOCX with high fidelity and accuracy, without requiring any intermediate/additional steps or using other APIs. So, you don’t require or use the custom utility anymore. The Workbook.Save method can be used for the task while you may also specify the SaveFormat.Docx enumeration.

  1. For .NET, more details are available on C# EXCEL to WORD - EXCEL to WORD Converter | products.aspose.com page.
  2. For Java, see the details on Java EXCEL to WORD - EXCEL to WORD Converter | products.aspose.com page.

Sample Code

The process involves loading the Excel file into a Workbook object and then calling the Save method.

C# (.NET)

using Aspose.Cells;
using System;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Load the source Excel file
            Workbook workbook = new Workbook("input.xlsx"); // Replace with your input file path

            // Save the workbook as a DOCX file
            workbook.Save("output.docx", SaveFormat.Docx); // Specify the output file path and format

            Console.WriteLine("Excel file successfully converted to DOCX.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

This example is detailed in the Aspose knowledge base. Also, see the document: Convert Excel to PDF, Image and other formats|Documentation

Java

import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

public class ExcelToDocxConverter {
    public static void main(String[] args) throws Exception {
        // Load the source Excel file
        Workbook workbook = new Workbook("input.xlsx"); // Replace with your input file path

        // Save the workbook as a DOCX file
        workbook.save("output.docx", SaveFormat.DOCX); // Specify the output file path and format

        System.out.println("Excel file successfully converted to DOCX.");
    }
}

Further details and options can be found in the Aspose documentation.

We recommend you to download the latest version of Aspose.Cells here:

Please feel free to write us back if you have questions or comments.

@wtp1000 You can directly create chart in MS Word document using Aspose.Words. Please see our documentation for more information:
https://docs.aspose.com/words/net/working-with-charts/