We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HTML content to Excel

Hi Aspose Support Team,

My requirement is, i need to convert HTML content to Excel. For this i am using the following html and c# code.

HTML:

NIGERIA Africa 4.63167+ 4.575+

Africa Name : Africa

  1. NIGERIA
  2. Nation
  3. Place
    • Test
    • test

    • code:

      public byte[] HtmlToExcelByte(string html)
      {
      byte[] byteArray = Encoding.ASCII.GetBytes(html);
      MemoryStream ms = new MemoryStream();
      using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
      {
      Aspose.Cells.License license = new Aspose.Cells.License();
      license.SetLicense(Properties.Settings.Default.App_Setting_License);

      Aspose.Cells.LoadOptions lo = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Html);
      Aspose.Cells.Workbook workBook = new Aspose.Cells.Workbook(stream, lo);
      ms = workBook.SaveToStream();
      workBook.Save("C://testop.xlsx");
      }
      byte[] sheetData = ms.ToArray();

      return sheetData;
      }

      Problem:

      1) While converting i am getting the exception from this line "Aspose.Cells.Workbook workBook = new Aspose.Cells.Workbook(stream, lo);" and the Exception is "HtmlLoadOptions was not set,image path is null."

      2) If we remove the
      1. <\li> and
          <\ul> from html content, i am getting the few values in the Excel(Attached: Output.xls). For this issue HTML(Input.jpeg) is:
      2. NIGERIA Africa 4.63167+ 4.575+

        Africa Name : Africa


        Please help me to solve the above issues.

        Thanks
        Saravanan

        Hi,


        Thanks for proving sample code and template files.

        Well, as Aspose.Cells is a primarily spreadsheet management library/component, so, I am afraid, it does not render or work for all the html tags. Anyways, I have tested your scenario a bit.
        I have successfully rendered to Excel format from your HTML (I pasted the html code to save an .html file) with v7.5.0.x (latest version/fix). But it does not convert all the contents and formatting fine.

        Sample code (updated):

        /*
        var wb = new Workbook(@“e:\test2\html.html”, new LoadOptions(LoadFormat.Html));
        wb.Save(“e:\test2\output2.xlsx”, SaveFormat.Xlsx);
        */

        string html = “e:\test2\html.html”;
        byte[] byteArray = Encoding.ASCII.GetBytes(html);
        MemoryStream ms = new MemoryStream();
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
        {
        //Aspose.Cells.License license = new Aspose.Cells.License();
        //license.SetLicense(Properties.Settings.Default.App_Setting_License);


        Aspose.Cells.HTMLLoadOptions lo = new Aspose.Cells.HTMLLoadOptions(Aspose.Cells.LoadFormat.Html);
        Aspose.Cells.Workbook workBook = new Aspose.Cells.Workbook(stream, lo);
        ms = workBook.SaveToStream(); //Do not use SaveToStream as it would save to XLS format. you need to use Workbook.Save(stream, SaveFormat.Xlsx) overload to do this.
        workBook.Save(“e:\test2\33output1.xlsx”);
        }
        byte[] sheetData = ms.ToArray();

        I have logged a ticket with an id “CELLSNET-41797” for your issue. We will look into your issue soon if we can fix it.

        Once we have any update on it, we will let you know here.

        Thank you.

        Hi Support team,


        I have downloaded v7.5.0.0 and used. now i am getting the following Exception:
        Cannot access a closed Stream while using this line: workBook.Save(stream, Aspose.Cells.SaveFormat.Xlsx); .

        Following sourcecode i used:
        byte[] byteArray = Encoding.ASCII.GetBytes(html);
        MemoryStream ms = new MemoryStream();
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        Aspose.Cells.HTMLLoadOptions lo = new Aspose.Cells.HTMLLoadOptions(Aspose.Cells.LoadFormat.Html);
        Aspose.Cells.Workbook workBook = new Aspose.Cells.Workbook(stream, lo);
        workBook.Save(stream, Aspose.Cells.SaveFormat.Xlsx);
        workBook.Save(“C://testop.xlsx”);
        byte[] sheetData = stream.ToArray();

        return sheetData;

        HTML is:
        NIGERIA Africa 4.63167+ 4.575+

        Africa Name : Africa

        1. NIGERIA
        2. Nation
        3. Place
          • Test
          • test

          • Thanks,
            Saravanan
            Hi,

            Please change your lines of code:
            //...........
            workBook.Save(stream, Aspose.Cells.SaveFormat.Xlsx);
            workBook.Save("C://testop.xlsx");
            byte[] sheetData = stream.ToArray();

            return sheetData;

            to:
            //...........
            workBook.Save(ms, Aspose.Cells.SaveFormat.Xlsx);
            workBook.Save("C://testop.xlsx");
            byte[] sheetData = ms.ToArray();

            return sheetData;

            Thank you.

            Hi,

            Thanks for your posting and using Aspose.Cells for .NET.

            We have fixed the issue.

            Please download and try this fix: Aspose.Cells for .NET (Latest Version) and let us know your feedback.