BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
Hi,
I wan to convert word (.doc) file to excel using .net c#. Do we have any option for this.
Tried below code:
public void ConvertWordToExcel()
{
string wordFilePath = Path.Combine(AppPath.API_TEMP_OUTPUT, "LABELCHALLAN.doc");
string htmlFileTempPath = Path.Combine(AppPath.API_TEMP_OUTPUT, DateTime.Now.Ticks.ToString() + ".html");
string excelFileTempPath = Path.Combine(AppPath.API_TEMP_OUTPUT, DateTime.Now.Ticks.ToString() + ".xlsx");
IWordDocument document = new Syncfusion.DocIO.DLS.WordDocument();
document.ImportStyles = true;
document.UpdateStylesOnOpen = true;
document.Open(wordFilePath, Syncfusion.DocIO.FormatType.Doc);
document.Save(htmlFileTempPath, Syncfusion.DocIO.FormatType.Html);
document.Dispose();
FileStream htmlFileStrem = new FileStream(htmlFileTempPath, FileMode.Open);
XlsIO.ExcelEngine engion = new XlsIO.ExcelEngine();
XlsIO.IApplication app = engion.Excel;
app.DefaultVersion = XlsIO.ExcelVersion.Excel2007;
IWorkbook wb = app.Workbooks.Create(1);
IWorksheet ws = wb.Worksheets[0];
ws.ImportHtmlTable(htmlFileStrem, 1, 1);
wb.SaveAs(excelFileTempPath);
wb.Close();
wb = null;
ws = null;
htmlFileStrem.Close();
htmlFileStrem.Dispose();
htmlFileStrem = null;
engion.Dispose();
engion = null;
app = null;
}
Hi Nagendra,
Syncfusion Word library (DocIO) does not convert the Word document into Excel.
However, you can convert the Word document into Excel by using any of our
workaround solutions.
Solution 1: Convert Word document to HTML and HTML to Excel (As per your code
snippet):
From the given code snippet, we have found that while converting Word document
to HTML, we wrote the elements in a div tag in html. The ImportHtmlTable method
will import the html table only without div tag. Due to the div tag in DocIO
converted html, ImportHtmlTable method does not import the html table. So, the table
is not preserved in converted excel.
To resolve this problem, we suggest you to open the DocIO converted html and
remove <div> </div> tags. Then import the modified html into a worksheet
using XlsIO library.
Note: Syncfusion XlsIO supports importing HTML tables with the inline
styles alone. HTML documents with embedded styles or style sheets are not
supported.
Solution 2: Extract table data from a Word document and add those data into a
worksheet:
We suggest you to iterate through a table in a Word document and get the
data from the table cell using DocIO library. Then added the data into the
table in the new worksheet using XlsIO library.
We have prepared sample application as per this solution to achieve your requirement
and it can be downloaded from the below attachment.
Refer below UG documentation to know about how to iterate through table
elements in Word document,
https://help.syncfusion.com/file-formats/docio/working-with-tables#iterating-through-table-elements
Refer below UG documentation to know about how to add table in excel,
https://help.syncfusion.com/file-formats/xlsio/working-with-excel-tables
Solution 3: Convert Word document to images and add images into a worksheet:
We suggest you to convert Word document to images using DocIO library. Then add
the resultant images into the excel worksheet using XlsIO library.
Refer below UG documentation to know about how to convert Word document to
images,
https://help.syncfusion.com/file-formats/docio/word-to-image
Refer below UG documentation to know about how to add images into excel,
https://help.syncfusion.com/file-formats/xlsio/working-with-pictures
Regards,
Suriya Balamurugan.
If this post is helpful, please consider accepting it as the solution so
that other members can locate it more quickly.
How to open a Word document in Excel using a file converter
Open a Word document.
Select "File," "Save As" and save the document.
Open an Excel spreadsheet and choose "Data"
Click on "From Text/CSV" and choose your Word document.
Make personal changes in the following window.
Click "Load" and save the spreadsheet
Regards
Diana