Free Support Forum - aspose.com

Convert HTML to Excel

I am trying to convert HTML string which has multiple table elements in it with style. I want to convert it to Excel and print table on multiple worksheets, but there is no option to Aspose.Words.SaveFormat.Xls.Please provide me a solution. I even don’t want to lose it’s styling.

@wardaarable,

Aspose.Words for .NET API does not support Excel file formats such as XLSX, XLS, XLT etc. To learn more about what file formats are supported by Aspose.Words for .NET, please refer to the following link:

Instead, please use Aspose.Cells for .NET API to convert HTML file (string) to XLS format. I am moving your thread in Aspose.Cells Product Family Forum where you will be guided appropriately.

@wardaarable,
Could you share runnable code sample and output file created with Aspose.Cells for .NET. We will analyze it and provide our feedback accordingly.

            byte[] template = null;
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            string tempFilePath = basePath + @"\Templates\" + fileName+".doc";
            string downloadFile = basePath + @"\Templates\" + fileName;
            template = System.IO.File.ReadAllBytes(tempFilePath);

            using (MemoryStream inStream = new MemoryStream(template))
            {
                if (!NeRA.AggreementHelper.IsLicenseAlreadyOpen)
                    NeRA.AggreementHelper.InitializeLicense();
                Aspose.Words.Document doc = new Aspose.Words.Document(inStream);
                Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
                var HtmlHead = HtmlHeader.Replace("_and_", "&").Replace("&lt;", "<").Replace("&gt;", ">");
                builder.MoveToBookmark("html_header");
                builder.InsertHtml(HtmlHead);
                foreach (var bookmark in HtmlObj.Reverse())
                {
                    var HtmlString = bookmark.HtmlInput.Replace("_and_", "&").Replace("&lt;", "<").Replace("&gt;", ">");
                    builder.MoveToBookmark("html_bookmark");
                    builder.InsertHtml(HtmlString);
                    builder.InsertBreak(BreakType.PageBreak);
                }
                doc.Save(downloadFile + ".docx", Aspose.Words.SaveFormat.Doc);
                doc.Save(downloadFile+".pdf", Aspose.Words.SaveFormat.Pdf);
            }

This is the runable code for generating pdf and word using html string but I need to generate excel with multiple worksheets and to print each table on separte worksheet
.

@wardaarable,

You have two options to accomplish the task (you may choose any of the following options):

  1. As @awais.hafeez told you that you may use Aspose.Cells for .NET API to convert HTML file (string) to XLS format. See the document for your reference:
    https://docs.aspose.com/display/cellsnet/Support+the+layout+of+DIV+tags+while+loading+HTML+to+excel+workbook

  2. Try using Aspose.PDF for .NET API which supports converting PDF (you generated it via Aspose.Words API) to XLS/XLSX file format. See the document for your reference:
    https://docs.aspose.com/display/pdfnet/Convert+PDF+to+Excel+XLS

Hope, this helps you.

Thanks Amjad, I’ll try Aspose.Cells. Thankyou, for the provided link.

@wardaarable,

You are welcome.

I want to load tables in separate worksheets by looping how should I create worksheets and load tables on each worksheet.

@wardaarable,

You can load individual tables (HTML strings) into Aspose.Cells object model and then save each table to separate Excel file. Now merge Excel files to make it one. See the document for your reference:
https://docs.aspose.com/display/cellsnet/Merge+Files

Hope, this helps a bit.

While merging it gives me “This is not a structured storage file.”

@wardaarable,

Please zip your Excel files (that you generated using individual HTML tables via Aspose.Cells APIs) and attach the zipped archive, we will check it soon.

Files.zip (1.9 KB)

This is a single file with multiple tables which I have directly exported html to excel. The only problem is all the divs gets downloaded on single sheet.

@wardaarable,

I checked the file and your so called XLS file is actually an HTML file which contains all the tables in the single worksheet. I am afraid, as I told you have to split the individual tables (HTML string) by yourselves (using your own code) if you need Aspose.Cells to load individual HTML strings (as per the suggested document (above)) and save to separate Excel file. Then merge all those Excel files by referring to the document.

Surely, when you render this HTML to Excel, you will get a single sheet as all the data (in source file) is there on single sheet (source file). By the way, another way can be you may copy selective data to other sheets using Copy Ranges feature. For example, you may copy range A9:M24 to other sheet (you may add sheet via the API). Similarly you may add another sheet where you will copy range “A25:M40” accordingly. Once you have copied all the ranges to other sheets (one range per sheet), you may remove the original sheet and then save to Excel file. This is more like a manual way.