We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Export data from Excel sheet to a Word Document for Mail Merge?

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: November 8, 2017

 

Please refer the below steps to work on this:

* IWorkbook Interface is used to open the Excel file.

* Worksheet having the data (basically a table)is assigned to IWorksheet object

* A DataTable is created to hold the data imported from SpreadSheet.

* ExportDataTable method is used to get the data from the Worksheet and assigned to DataTable

* ExportDataTable method has two arguments. One for specifying the range of the data in the worksheet and another for specifying the column names.

* The DataTable is executed using the WordDocument object

Please refer the below code snippet which illustrates this:

C#

ExcelEngine excelEngine = new ExcelEngine();

IApplication application = excelEngine.Excel;

IWorkbook workbook = application.Workbooks.Open("NorthwindDataTemplate.xls");

IWorksheet sheet = workbook.Worksheets[0];

DataTable customersTable = new DataTable();

//Assigning DataTable with Excel data

customersTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);

customersTable.TableName = "Customers";

WordDocument doc = new WordDocument("CustomersReport.doc");

doc.MailMerge.Execute(customersTable);

doc.Save("Result.docx");

doc.Close();

workbook.Close();

excelEngine.Dispose();

 

VB

Dim excelEngine As New ExcelEngine()

Dim application As IApplication = excelEngine.Excel

Dim workbook As IWorkbook = application.Workbooks.Open("NorthwindDataTemplate.xls")

Dim sheet As IWorksheet = workbook.Worksheets(0)

Dim customersTable As New DataTable()

'Assigning DataTable with Excel data

customersTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames)

customersTable.TableName = "Customers"

Dim doc As New WordDocument("CustomersReport.doc")

doc.MailMerge.Execute(customersTable)

doc.Save("Result.docx")

doc.Close()

workbook.Close()

excelEngine.Dispose()

 

The sample which illustrates the above behavior can be downloaded here.

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
David Menzi
Nov 02, 2017

Sample not working.

Reply
Sridhar Sukumar [Syncfusion]
Nov 08, 2017

We have fixed the sample issue. Please download and try the sample.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon