Articles in this section

How to extract content from multiple Excel and insert it into a Word document?

Syncfusion® Essential® DocIO is a .NET Core Word Library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Syncfusion® Essential® XlsIO is a .NET Core Excel Library used to create, read, edit, and convert Excel files programmatically without Microsoft Office COM libraries & Microsoft Office. Using these libraries, you can extract the content from multiple Excel and insert it into a Word document using C#.

Steps to extract content from multiple Excel and insert it into a Word document using C#:

  1. Create a new C# Console application project. Create ASP.NET Core Web application in Visual Studio
  2. Install the Syncfusion.DocIORenderer.Net.Core and Syncfusion.XlsIO.Net.Core NuGet package as a reference to your project from
    image.png

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Include the following namespace in the Program.cs file:
    C#
using Syncfusion.XlsIO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Drawing;
  1. Use the following code snippet in Program.cs to extract the multiple Excel content and insert it in a Word document.
    C#
//Creates a new Word document.
using (WordDocument document = new WordDocument())
{
   // List of Excel file names (without extension)
   string[] excelFiles = { "Excel1", "Excel2" };
   // Loop through each Excel file
   foreach (string excelName in excelFiles)
   {
       // Get the Excel content
       UpdateExcelToWord(document, excelName);
   }
   //Creates file stream.
   using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
   {
       //Saves the Word document to file stream.
       document.Save(outputFileStream, FormatType.Docx);
   }
}
  1. Use the following method to extract the multiple Excel content to an table in a Word document.
    C#
private static void UpdateExcelToWord(WordDocument document, string excelName)
{
    // Open the Excel file
    ExcelEngine excelEngine = new ExcelEngine();
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    // Load the file into stream
    using (FileStream inputExcelStream = new FileStream(Path.GetFullPath(@"Data/") + excelName + ".xlsx", FileMode.Open, FileAccess.Read))
    {
        // Open the workbook from the stream.
        IWorkbook workbook = application.Workbooks.Open(inputExcelStream);
        // Loop through all worksheets
        foreach (IWorksheet worksheet in workbook.Worksheets)
        {
            // Add a new section for each worksheet
            IWSection section = document.AddSection();
            // Add a new table to the section
            WTable table = section.AddTable() as WTable;
            table.AutoFit(AutoFitType.FitToContent);
            //Set Title for table
            table.Title = excelName + "_" + worksheet.Name;
            // Determine the number of rows and columns in the worksheet.
            int rows = worksheet.Rows.Length;
            int columns = worksheet.Columns.Length;
            // Initialize the table with the required number of rows and columns.
            table.ResetCells(rows, columns);
            table.TableFormat.Borders.BorderType = BorderStyle.Single;
            table.TableFormat.Borders.LineWidth = 1;
            table.TableFormat.Borders.Color = Color.Black;
            // Populate the table cell-by-cell from the worksheet.
            for (int rowIndex = 0; rowIndex < rows; rowIndex++)
            {
                // Get the current row range in the worksheet.
                IRange rowRange = worksheet.Rows[rowIndex];

                for (int cellIndex = 0; cellIndex < columns; cellIndex++)
                {
                    // Access the specific cell within the current row.
                    IRange cell = rowRange.Cells[cellIndex];
                    // Add a paragraph into the corresponding Word table cell.
                    WParagraph paragraph = table[rowIndex, cellIndex].AddParagraph() as WParagraph;
                    // Insert the cell's display text into the Word paragraph.
                    WTextRange textRange = paragraph.AppendText(cell.DisplayText) as WTextRange;
                    // Preserve basic font styling from the Excel cell into the Word text.
                    textRange.CharacterFormat.Bold = cell.CellStyle.Font.Bold;
                    textRange.CharacterFormat.Italic = cell.CellStyle.Font.Italic;
                    textRange.CharacterFormat.FontSize = (float)cell.CellStyle.Font.Size;
                    textRange.CharacterFormat.FontName = cell.CellStyle.Font.FontName;
                }
            }
        }
    }              
}

You can download a complete working sample of extracting the content from multiple Excel and insert it into a Word document from GitHub

By executing the program, you will get the Word document as follows:

Extract Excel.png

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion
I hope you enjoyed learning about how to extract the multiple Excel content and insert it into a Word document using .NET Core Word Library.

You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, 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)
Access denied
Access denied