Keeping the header and the footer when saving a xlsx file in html

Hello,

I’ve tried to save a xlsx file in html, but I’ve lost the header and the footer in the output file.
I think it’s the normal functionality of excel but just to be sur do you have a way to keep the header dans footer in the html output file?

Aspose.Cell.NET 25.7.0

In the attachment the code and the document I’ve used for the test.
element.zip (11.9 KB)

Thank you, best regards.

@ISIWARE

Cause

When saving an Excel file as HTML, many libraries, including Aspose.Cells, typically exclude headers and footers from the output. This is because headers and footers are primarily designed for printed documents rather than web formats.

Solution

To retain the header and footer when exporting to HTML, you need to manually add them to the HTML output after the conversion, as Aspose.Cells does not provide a direct option to include headers and footers in the HTML export.

Code

Here’s a sample code snippet that demonstrates how to save an Excel file as HTML and then manually add the header and footer:

// Load the Excel file
Workbook workbook = new Workbook("input.xlsx");

// Save the workbook as HTML
workbook.Save("output.html", SaveFormat.Html);

// Load the HTML file to modify it
string htmlContent = File.ReadAllText("output.html");

// Define your header and footer
string header = "<div style='text-align:center; font-weight:bold;'>Your Header Here</div>";
string footer = "<div style='text-align:center; font-weight:bold;'>Your Footer Here</div>";

// Insert header at the beginning and footer at the end
htmlContent = header + htmlContent + footer;

// Save the modified HTML back to file
File.WriteAllText("output_with_header_footer.html", htmlContent);

Next Steps

  1. Adjust the header and footer content as needed.
  2. Test the output HTML file to ensure it meets your requirements.
  3. If you have specific formatting needs for the header and footer, enhance the HTML/CSS accordingly.

If you have any further questions or need additional assistance, please let me know!

@ISIWARE,

Headers and footers are not rendered when converting Excel spreadsheets to HTML using Aspose.Cells. This is the expected behavior, similar to rendering as an HTML (web page). If you wish to include headers or footers, you may manually insert them into the HTML output using your code, or alternatively, you can add the header/footer content directly into worksheet cells using Aspose.Cells. For instance, you could insert one or two rows at the top of the worksheet and place the header content into the appropriate cells before rendering to HTML.

@ISIWARE ,

You may please try the following code to acheive it:

Workbook workbook = new Workbook("input.xlsx");
 HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.Html)
 {
    SaveAsSingleFile = true,  
    ExportSingleTab=true,    // Set to true if there is a single sheet
    ExportPageHeaders =true,
    ExportPageFooters=true
}
workbook.Save("output.html", saveOptions);