Free Support Forum - aspose.com

Convert HTML to Excel/CSV using Aspose.Cells for .NET in C#

Hi ,

I am trying to convert HTML to xlsx/CSV format using Aspose.Cells for .NET
File Version : 18.4.0.0

I am using below lines of code :

        string[] paths = { @"D:\Projects", "ASPOSE_POC" };
        string fullPath = Path.Combine(paths);
        string fileName = string.Format("{0}", "Report.html");
        string html1 = Path.Combine(fullPath, fileName);

        Aspose.Cells.HTMLLoadOptions options = new Aspose.Cells.HTMLLoadOptions();
        options.DeleteRedundantSpaces = true;
        Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(html1, options);
        workbook.Save("c:\\temp\\output_JUN27_1.xlsx");

But I am getting below error:
An unhandled exception of type ‘System.FormatException’ occurred in mscorlib.dll
Input string was not in a correct format.

Could you please help me with above error?
I have attached sample file of HTML for which the above code is failing.

Report.zip (1.3 KB)

Thanks,
Poonam

@PoonamB,
I have tried to open this “Report.html” using Excel 2016 but could not open it as Excel raises an error and fails to open it. It seems that this sample HTML file has some issue and as Aspose.Cells mimics the behavior of Excel, so it also raises an exception.

You may please provide some HTML file which opens fine using Excel but cannot be loaded using Aspose.Cells. We will test it here and provide assistance accordingly.

Hi ,

Thank you for your reply.
Ok I understand that this HTML is not in correct format for Aspose.Cells
I will try to work on the HTML.
One question do you have any API to convert HTML to CSV files ?
Or is it first we have to convert it to Excel file and then save as CSV ?

Regards,
Poonam

@PoonamB,
There is no direct way but you may use Aspose.Cells with a simple code given below to achieve this functionality.

Aspose.Cells.HtmlLoadOptions options = new Aspose.Cells.HtmlLoadOptions();
options.DeleteRedundantSpaces = true;
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(path + "sample.html", options);
Aspose.Cells.TxtSaveOptions opts = new Aspose.Cells.TxtSaveOptions(Aspose.Cells.SaveFormat.CSV);
workbook.Save(path + "output.csv",opts);