Articles in this section
Category / Section

How to copy an Excel chart into a Word document using C#, VB.NET?

8 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

This article explains how to copy an Excel chart into a Word document using C# and VB.NET.

Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.XlsIO;
using Syncfusion.OfficeChart;

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.XlsIO
Imports Syncfusion.OfficeChart

Use the following code snippet for copying an Excel chart into a Word document.

C#

static void Main(string[] args)
{
    string excelFilePath = @"Data/Input.xlsx";
    string outputPath = @"Output/Output.docx";

    ConvertExcelChartToWordChart(excelFilePath, outputPath);
    Console.WriteLine("Chart successfully copied from Excel to Word.");
}

static void ConvertExcelChartToWordChart(string excelFilePath, string outputPath)
{
    using (ExcelEngine excelEngine = new ExcelEngine())
    {
        IApplication application = excelEngine.Excel;
        IWorkbook workbook = application.Workbooks.Open(excelFilePath);
        IWorksheet worksheet = workbook.Worksheets[0];

        //Get the first chart from Excel
        IChart excelChart = worksheet.Charts[0];

        WordDocument wordDocument = new WordDocument();
        IWSection section = wordDocument.AddSection();
        IWParagraph paragraph = section.AddParagraph();

        WChart wordChart = paragraph.AppendChart(500, 400);
        wordChart.ChartType = ConvertExcelChartTypeToWord(excelChart.ChartType);

        //Set chart size
        wordChart.Width = (float)excelChart.Width;
        wordChart.Height = (float)excelChart.Height;

        //Copy data from Excel chart to Word chart
        CopyChartData(excelChart, wordChart);

        //Apply formatting
        ApplyChartFormatting(excelChart, wordChart);

        //Save the Word document
        wordDocument.Save(outputPath);
        wordDocument.Close();
        workbook.Close();
    }
}

static OfficeChartType ConvertExcelChartTypeToWord(ExcelChartType excelChartType)
{
    return excelChartType switch
    {
        ExcelChartType.Column_Clustered => OfficeChartType.Column_Clustered,
        ExcelChartType.Column_Stacked => OfficeChartType.Column_Stacked,
        ExcelChartType.Line => OfficeChartType.Line,
        ExcelChartType.Pie => OfficeChartType.Pie,
        ExcelChartType.Bar_Clustered => OfficeChartType.Bar_Clustered,
        ExcelChartType.Area => OfficeChartType.Area,
        _ => OfficeChartType.Column_Clustered
    };
}

static void CopyChartData(IChart excelChart, WChart wordChart)
{
    IRange dataRange = excelChart.DataRange;

    for (int i = 0; i < dataRange.Rows.Length; i++)
    {
        for (int j = 0; j < dataRange.Columns.Length; j++)
        {
            object cellValue = dataRange[i + 1, j + 1].Value;
            wordChart.ChartData.SetValue(i + 1, j + 1, cellValue ?? "");
        }
    }

    wordChart.DataRange = wordChart.ChartData[dataRange.Row, dataRange.Column, dataRange.LastRow, dataRange.LastColumn];
}

static void ApplyChartFormatting(IChart excelChart, WChart wordChart)
{
    //Set chart title
    if (excelChart.HasTitle && !string.IsNullOrWhiteSpace(excelChart.ChartTitle))
    {
        wordChart.ChartTitle = excelChart.ChartTitle;
        wordChart.ChartTitleArea.FontName = excelChart.ChartTitleArea.FontName;
    }

    //Set legend properties
    wordChart.HasLegend = excelChart.HasLegend;
    if (excelChart.HasLegend && excelChart.Legend != null && wordChart.Legend != null)
    {
        wordChart.Legend.Position = excelChart.Legend.Position switch
        {
            ExcelLegendPosition.Bottom => OfficeLegendPosition.Bottom,
            ExcelLegendPosition.Left => OfficeLegendPosition.Left,
            ExcelLegendPosition.Right => OfficeLegendPosition.Right,
            ExcelLegendPosition.Top => OfficeLegendPosition.Top,
            _ => wordChart.Legend.Position
        };
    }

    //Set axis titles
    if (!string.IsNullOrEmpty(excelChart.PrimaryValueAxis?.Title))
        wordChart.PrimaryValueAxis.Title = excelChart.PrimaryValueAxis.Title;

    if (!string.IsNullOrEmpty(excelChart.PrimaryCategoryAxis?.Title))
        wordChart.PrimaryCategoryAxis.Title = excelChart.PrimaryCategoryAxis.Title;
}

VB.NET

Sub Main(args As String())
    Dim excelFilePath As String = "Data/Input.xlsx"
    Dim outputPath As String = "Output/Output.docx"

    ConvertExcelChartToWordChart(excelFilePath, outputPath)
    Console.WriteLine("Chart successfully copied from Excel to Word.")
End Sub


Sub ConvertExcelChartToWordChart(excelFilePath As String, outputPath As String)
    Using excelEngine As New ExcelEngine()
        Dim application As IApplication = excelEngine.Excel
        Dim workbook As IWorkbook = application.Workbooks.Open(excelFilePath)
        Dim worksheet As IWorksheet = workbook.Worksheets(0)

        'Get the first chart from Excel
        Dim excelChart As IChart = worksheet.Charts(0)

        Dim wordDocument As New WordDocument()

        Dim section As IWSection = wordDocument.AddSection()

        Dim paragraph As IWParagraph = section.AddParagraph()

        Dim wordChart As WChart = paragraph.AppendChart(500, 400)
        wordChart.ChartType = ConvertExcelChartTypeToWord(excelChart.ChartType)

        'Set chart size
        wordChart.Width = excelChart.Width
        wordChart.Height = excelChart.Height

        'Copy data from Excel chart to Word chart
        CopyChartData(excelChart, wordChart)

        'Apply formatting
        ApplyChartFormatting(excelChart, wordChart)

        'Save the Word document
        wordDocument.Save(outputPath)
        wordDocument.Close()

        workbook.Close()
    End Using
End Sub

Function ConvertExcelChartTypeToWord(excelChartType As ExcelChartType) As OfficeChartType
    Select Case excelChartType
        Case ExcelChartType.Column_Clustered
            Return OfficeChartType.Column_Clustered
        Case ExcelChartType.Column_Stacked
            Return OfficeChartType.Column_Stacked
        Case ExcelChartType.Line
            Return OfficeChartType.Line
        Case ExcelChartType.Pie
            Return OfficeChartType.Pie
        Case ExcelChartType.Bar_Clustered
            Return OfficeChartType.Bar_Clustered
        Case ExcelChartType.Area
            Return OfficeChartType.Area
        Case Else
            Return OfficeChartType.Column_Clustered
    End Select
End Function

Sub CopyChartData(excelChart As IChart, wordChart As WChart)
    Dim dataRange As IRange = excelChart.DataRange

    For i As Integer = 0 To dataRange.Rows.Length - 1
        For j As Integer = 0 To dataRange.Columns.Length - 1

            Dim cellValue As Object = dataRange(i + 1, j + 1).Value

            wordChart.ChartData.SetValue(i + 1, j + 1, If(cellValue, ""))
        Next
    Next

    wordChart.DataRange = wordChart.ChartData(dataRange.Row, dataRange.Column, dataRange.LastRow, dataRange.LastColumn)

End Sub

Sub ApplyChartFormatting(excelChart As IChart, wordChart As WChart)
    'Set chart title
    If excelChart.HasTitle AndAlso Not String.IsNullOrWhiteSpace(excelChart.ChartTitle) Then
        wordChart.ChartTitle = excelChart.ChartTitle

        wordChart.ChartTitleArea.FontName = excelChart.ChartTitleArea.FontName
    End If

    'Set legend properties
    wordChart.HasLegend = excelChart.HasLegend
    If excelChart.HasLegend AndAlso excelChart.Legend IsNot Nothing AndAlso wordChart.Legend IsNot Nothing Then
        Select Case excelChart.Legend.Position
            Case ExcelLegendPosition.Bottom
                wordChart.Legend.Position = OfficeLegendPosition.Bottom
            Case ExcelLegendPosition.Left
                wordChart.Legend.Position = OfficeLegendPosition.Left
            Case ExcelLegendPosition.Right
                wordChart.Legend.Position = OfficeLegendPosition.Right
            Case ExcelLegendPosition.Top
                wordChart.Legend.Position = OfficeLegendPosition.Top
        End Select
    End If

    'Set axis titles
    If Not String.IsNullOrEmpty(excelChart.PrimaryValueAxis?.Title) Then
        wordChart.PrimaryValueAxis.Title = excelChart.PrimaryValueAxis.Title
    End If

    If Not String.IsNullOrEmpty(excelChart.PrimaryCategoryAxis?.Title) Then
        wordChart.PrimaryCategoryAxis.Title = excelChart.PrimaryCategoryAxis.Title
    End If
End Sub

Below are the screenshots of the input Excel and the generated Word document.
Screenshot

Screenshot

You can get the complete sample for copying an Excel chart into a Word document from

I hope you enjoyed learning about how to copy an Excel chart into a Word document using C# and VB.NET in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied