I have a word document that has some text and tables. I need to bring it to Excel file. How can I do this?
Please help.
I have a word document that has some text and tables. I need to bring it to Excel file. How can I do this?
Please help.
Hi
Thank you for your request. There is no direct way to convert Word document to Excel. However, there is Excel2Word demo project created using Apsoe.Cells and Aspose.Words, which does exactly this. You can download this demo from here:
I also attached the latest version of Xls2DocConverter class (I updated it to work with the latest versions of Aspose.Words and Aspose.Cells).
Hope this information could be useful for you.
Best regards.
Thanks Alexey. This class file works fine to convert word documet to Excel. However, it does not bring the contents of Header and Footer.
I have a title and a small table in header and footer. How can we bring all contents to the Excel/
Thank you.
Hi
Thanks for your inquiry. You can use the same technique as you use to convert Document Sections. Headers and Footer can contain the same nodes as Body of the section. So you can loop through child nodes of Header/footer and import them to Excel.
Also, you can try modifying the class to use DocuemntVisitor, which can simplify the task:
Best regards.
Could you give me an example for importing the header and footer?
Thank you.
Hi
Thanks for your request. Please try modifying the code as shown below:
///
/// Imports Word Section to Excel Worksheet
///
/// Word Section
/// Excel Worksheet
private void ImportSection(Section wordSection, Worksheet excelWorksheet)
{
// Import paper size
excelWorksheet.PageSetup.PaperSize = ConvertPaperSize(wordSection.PageSetup.PaperSize);
// Import page orientation
excelWorksheet.PageSetup.Orientation = ConvertPageOrientation(wordSection.PageSetup.Orientation);
// Import margins
excelWorksheet.PageSetup.LeftMarginInch = ConvertUtil.PointToInch(wordSection.PageSetup.LeftMargin);
excelWorksheet.PageSetup.RightMarginInch = ConvertUtil.PointToInch(wordSection.PageSetup.RightMargin);
excelWorksheet.PageSetup.TopMarginInch = ConvertUtil.PointToInch(wordSection.PageSetup.TopMargin);
excelWorksheet.PageSetup.BottomMarginInch = ConvertUtil.PointToInch(wordSection.PageSetup.BottomMargin);
int excelRowIndex = 0;
if (wordSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
excelRowIndex = ImportStory(wordSection.HeadersFooters[HeaderFooterType.HeaderPrimary], excelWorksheet, excelRowIndex);
excelRowIndex = ImportStory(wordSection.Body, excelWorksheet, excelRowIndex);
if (wordSection.HeadersFooters[HeaderFooterType.FooterPrimary] != null)
excelRowIndex = ImportStory(wordSection.HeadersFooters[HeaderFooterType.FooterPrimary], excelWorksheet, excelRowIndex);
}
///
/// Imports word document Story (Body, Header/Footer) to excel.
///
/// Body or Header/Footer
/// Excel Worksheet
/// Index of the row in teh excel sheet, where story should be inserted.
///
private int ImportStory(Story wordStory, Worksheet excelWorksheet, int excelRowIndex)
{
foreach (Node wordNode in wordStory.ChildNodes)
{
switch (wordNode.NodeType)
{
case NodeType.Paragraph:
{
Paragraph wordParagraph = (Paragraph)wordNode;
Aspose.Cells.Cell excelCell = excelWorksheet.Cells[excelRowIndex, 0];
//Import paragraph
ImportParagraph(wordParagraph, excelCell, excelWorksheet);
break;
}
case NodeType.Table:
{
Table wordTable = (Table)wordNode;
foreach (Aspose.Words.Tables.Row wordRow in wordTable.Rows)
{
int cellIndex = 0;
foreach (Aspose.Words.Tables.Cell wordCell in wordRow.Cells)
{
Aspose.Cells.Cell excelCell = excelWorksheet.Cells[excelRowIndex, cellIndex];
//Impotr table cell
ImportCell(wordCell, excelCell);
cellIndex++;
}
excelRowIndex++;
}
break;
}
}
excelRowIndex++;
}
return excelRowIndex;
}
Hope this helps.
Best regards.
Thank you Alexey. Your example helped me to bring header and footer.
But it did not convert all contents correctly. Here is my test word file. I need to convert to Excel file and display this excel file in the Asp.net browser page.
Please help.
Hi
Thanks for your request. The problem in this case occurs because Excel document is one big table and each row in this table should have the same number of cells. In other hand MS Word document can contain any number of tables with any number of columns and moreover rows in the same table can contain different number of cells. So it is extremely difficult to convert Word document with tables to Excel.
There should be complex algorithm, which should calculate number of cells, which should be merged. This number will depend from width of the cell, number of cells and width of them in all other rows.
Code, I provide, is not a ready to use solution, this is just a simple demo. You are free to experiment with code I provided and change it to fill your needs.
Best regards.
I also need to convert the tables in a Word document into Excel worksheets. I can see from the Words API how to acquire Table objects from the document, but after that it isn’t too clear. The link to the Xls2DocConverter class seems to be broken, so I’m hoping that it can be restored or some additional information can be provided.
Thanks.
Hi
Thanks for your request. I sent this class to your e-mail.
Hope this helps.
Best regards.
Thanks for the Word to Excel conversion class. I translated it to Java, but have run into a problem in regard to Words finding sections and tables in the document.
The development document supplied by our customer has many sections (it appears that page breaks define ‘sections’) and almost 100 tables. It’s only the tables we’re interested in, so I modified the code to ignore paragraphs and process only tables.
The loop processing the SectionCollection locates the first 2 sections and converts a table in each section into an Excel worksheet. This is correct and it functions reasonably well (the first two tables are simple). In the third section it fails to locate the table (a more complex one), then the loop terminates after finding only 3 sections. Most of the document is not found.
I’ve been through this in the debugger and the basic loops appear to be sound. This all assumes the Tables appear only within a Body and a Body only appears within a Section.
I’m working with the latest Aspose Total release (for Java) and we do have a license (this is not a demo version). If I made a mistake in setting the license, would it throw an exception or silently fall back to some restricted demo mode that might explain the limited processing?
Thanks for your help.
Kent
Hi Kent,
Thanks for your request. Could you please attach your docuemtn here for testing? I will check the issue and provide you more information.
Also, you can try just saving the document, if Aspose.Words works in evaluation mode and truncates the document, you will see the following message at the end of the document:
This document was truncated here because it was created using Aspose.Words in Evaluation Mode.
In this case, you should check whether the license is applied properly.
Best regards.
Alexey,
This did turn out to be an issue with setting the license properly. Thanks for all your help.
Kent
Hi!
Is this Excel2Word demo going to be integrated into the product?
Would it be possible to get a hold of the demo projekt? The link is dead.
Thanks
Hi
Thanks for your inquiry. You can download the complete source code of the Excel2Word sample from here:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/category1177.aspx
Best regards,