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 : Nov 16th 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?

 

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.

Article ID: Published Date: Last Revised Date: Platform: Control:
1928 04/30/2011 11/08/2017 WinForms XlsIO
Tags:
Did you find this information helpful?
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.

Add Comment
You must log in to leave a comment

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