Free Support Forum - aspose.com

Merge excel range into word document

We cannot find an example anywher of how to merge an excel range into a word document. We own both cells and words.
This message was posted using Aspose.Live 2 Forum

You can insert an Excel file as an OLE object into a word file. For how to make it, our auckland team will support you soon.

Hi

Thanks for your interest in Aspose products. Unfortunately, Aspose.Words doesn’t allow inserting OLE objects into the document. But you can import data from Excel workbook into Word document using the following code.

//Open workbook

Workbook workbook = new Workbook();

workbook.Open("in.xls");

//Create new document

Document doc = new Document();

//Create instance of DocumentBuilder class

DocumentBuilder builder = new DocumentBuilder(doc);

//Every worksheet in Excel workbook is represented as table in Word document

foreach (Worksheet worksheet in workbook.Worksheets)

{

//Get number of last row with data

int rowCount = worksheet.Cells.MaxDataRow;

//Get number of last column with data

int columnCount = worksheet.Cells.MaxDataColumn;

//Build table in the word document

for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++)

{

builder.RowFormat.Height = worksheet.Cells.GetRowHeightPixel(rowIndex) * 0.75;

for (int columnIndex = 0; columnIndex <= columnCount; columnIndex++)

{

builder.CellFormat.Width = worksheet.Cells.GetColumnWidthPixel(columnIndex) * 0.75;

builder.InsertCell();

builder.Write(worksheet.Cells[rowIndex, columnIndex].StringValue);

}

builder.EndRow();

}

builder.EndTable();

builder.InsertBreak(BreakType.ParagraphBreak);

}

//Save output document

doc.Save("out.doc");

This code allows import only text values without formatting. Currently I work on Excel2Word converter based on Aspose.Words and Aspose.Cells. It will be very good if you attach your workbook for testing here.

Please let me know if you have any questions or idea.

Best regards.

Hi

Thanks for your interest in Aspose products. Unfortunately, Aspose.Words doesn’t allow inserting OLE objects into the document. But you can import data from Excel workbook into Word document using the following code.

//Open workbook

Workbook workbook = new Workbook();

workbook.Open("in.xls");

//Create new document

Document doc = new Document();

//Create instance of DocumentBuilder class

DocumentBuilder builder = new DocumentBuilder(doc);

//Every worksheet in Excel workbook is represented as table in Word document

foreach (Worksheet worksheet in workbook.Worksheets)

{

//Get number of last row with data

int rowCount = worksheet.Cells.MaxDataRow;

//Get number of last column with data

int columnCount = worksheet.Cells.MaxDataColumn;

//Build table in the word document

for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++)

{

builder.RowFormat.Height = worksheet.Cells.GetRowHeightPixel(rowIndex) * 0.75;

for (int columnIndex = 0; columnIndex <= columnCount; columnIndex++)

{

builder.CellFormat.Width = worksheet.Cells.GetColumnWidthPixel(columnIndex) * 0.75;

builder.InsertCell();

builder.Write(worksheet.Cells[rowIndex, columnIndex].StringValue);

}

builder.EndRow();

}

builder.EndTable();

builder.InsertBreak(BreakType.ParagraphBreak);

}

//Save output document

doc.Save("out.doc");

This code allows import only text values without formatting. Currently I work on Excel2Word converter based on Aspose.Words and Aspose.Cells. It will be very good if you attach your workbook for testing here.

Please let me know if you have any questions or idea.

Best regards.