Load Html to excel and preserve the design

Hi,
We working with Aspose.Cells, Version=23.5.0.0.
We trying to load html content to workbook and preserve the design as is.
Any idea how we can achieve that? I tried to search with Claude but I don’t see option that valid to our version. maybe you have some insight for that?
if this is matter - we need to load the data from html to the workbook and save it to xlsx file of pdf.
but the design should remain the same.
this is my code for loading the html content to workbook.

 public async Task<Workbook> RenderHtmlWorkBookAsync(string html)
 {
     byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(html);
     var lo = new HtmlLoadOptions(LoadFormat.Html)
     {
         ConvertNumericData = false
     };
     using (var stream = new MemoryStream(byteArray))
     {
         return await Task.FromResult(new Workbook(stream, lo));
     }
     
 }

that called from:

 
        // Use the new rendering method based on flags
        string html = await RenderHtmlTemplateAsync(formattedJson, htmlTemplate, flags?.ShouldUseExternalHtmlRenderer ?? false);

        var workbook = await asposeHelper.RenderHtmlWorkBookAsync(html);
        var designer = new WorkbookDesigner(workbook)
        {
            LineByLine = false
        };

        var activeWorksheet = designer.Workbook.Worksheets[designer.Workbook.Worksheets.ActiveSheetIndex];
        if (!ignoreLogo)
        {
            activeWorksheet.Cells["A2"].PutValue("&=$logo(Picture:Scale20)");
            designer.SetDataSource("logo", logo);
        }
        designer.Process();
designer.Workbook.Settings.Author = "JET";
return await asposeHelper.SaveToStreamAsync(designer, fileFormat);

save to stream:

   public async Task<byte[]> SaveToStreamAsync(WorkbookDesigner designer, FileFormat fileFormat)
   {
       using (var stream = new MemoryStream())
       {
           switch (fileFormat)
           {
               case FileFormat.PDF:
                   var pdfSaveOptions = GetPdfSaveOption();
                   designer.Workbook.Save(stream, pdfSaveOptions);
                   break;
               case FileFormat.EXCEL:
                   designer.Workbook.Save(stream, SaveFormat.Xlsx);
                   break;
           }

           return await Task.FromResult(stream.ToArray());
       }
   }
 private PdfSaveOptions GetPdfSaveOption()
 {
     return new PdfSaveOptions
     {
         OptimizationType = PdfOptimizationType.MinimumSize,
         AllColumnsInOnePagePerSheet = true,
         ExportDocumentStructure = true
     };
 }

@zivdaniel

To load HTML content into an Excel workbook while preserving the design, you can utilize the HtmlLoadOptions class in Aspose.Cells. This class provides various options that can help maintain the formatting and layout of the HTML content.

Here’s how you can modify your existing code to ensure that the design is preserved:

  1. Set the AutoFitColsAndRows option: This option automatically adjusts the column widths and row heights based on the content, which can help in maintaining the layout.

  2. Use the KeepPrecision option: If your HTML contains large numbers or specific formatting, enabling this option will help retain the original formatting.

Here’s an updated version of your RenderHtmlWorkBookAsync method that incorporates these options:

public async Task<Workbook> RenderHtmlWorkBookAsync(string html)
{
    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(html);
    var lo = new HtmlLoadOptions(LoadFormat.Html)
    {
        ConvertNumericData = false,
        AutoFitColsAndRows = true, // Automatically fit columns and rows
        KeepPrecision = true // Keep precision for large numbers
    };
    
    using (var stream = new MemoryStream(byteArray))
    {
        return await Task.FromResult(new Workbook(stream, lo));
    }
}

Saving the Workbook

When saving the workbook, you can continue using your existing SaveToStreamAsync method. The workbook will retain the design as specified by the HTML content.

Example Usage

You can call the modified RenderHtmlWorkBookAsync method as follows:

string html = await RenderHtmlTemplateAsync(formattedJson, htmlTemplate, flags?.ShouldUseExternalHtmlRenderer ?? false);
var workbook = await asposeHelper.RenderHtmlWorkBookAsync(html);

Additional Considerations

  • Ensure that your HTML content is well-structured and valid, as malformed HTML can lead to unexpected results.
  • Test with different HTML samples to see how well the design is preserved and make adjustments to the HtmlLoadOptions as necessary.

By following these steps, you should be able to load HTML content into an Excel workbook while preserving its design effectively. If you encounter any specific issues with certain HTML elements, feel free to ask for further assistance!

this is not answering my question - i would like to preserve the design that i have in the html when my aspose.cells version is 23.5.0.0

@zivdaniel
Regarding how to load HTML and save it in PDF format, please refer to the following document.

If you still have questions, please provide sample files and runnable test code. If you can take a screenshot and highlight the error, it will be very helpful for us to locate the issue. We will check it soon.