How to create a Aspose.Cells for .NET to convert an Excel file into HTML for web preview

@amjad.sahi, @Professionalize.Discourse, @John.He, @simon.zhao

The goal is to show the entire worksheet (visible excel rows and columns) in a scrollable HTML preview inside a browser tab before excel create or download.

Use this excel file first sheet convert to html but this will proper not working.

Excel Preview.zip (1.8 MB)

I am use this convert excel to html. give me batters and fast generating approach

		// This method will open HTML preview in browser
		public ActionResult PreviewHtml(string fileName)
		{
			string filePath = Server.MapPath("~/App_Data/" + fileName);

			if (!System.IO.File.Exists(filePath))
				return HttpNotFound("File not found: " + fileName);

			Workbook workbook = new Workbook(filePath);

			// Configure HTML export options for full fidelity
			HtmlSaveOptions options = new HtmlSaveOptions
			{
				ExportActiveWorksheetOnly = false,  // All sheets
				ExportGridLines = true,
				PresentationPreference = true,      // Better visual fidelity
				ExportRowColumnHeadings = true,     // Show column/row headers
				ExportHiddenWorksheet = true,				
				ExportWorksheetCSSSeparately = false,
				ExportSimilarBorderStyle = true,
				ExportImagesAsBase64 = true,        // Shapes & images inline
				HtmlCrossStringType = HtmlCrossType.Cross,
				CellCssPrefix = "c"                 // Avoid CSS conflicts
			};

			using (MemoryStream ms = new MemoryStream())
			{
				workbook.Save(ms, options);
				ms.Position = 0;

				// Read HTML as string
				string html = Encoding.UTF8.GetString(ms.ToArray());

				// Add scrollable full-screen styling
				string wrappedHtml = @"<!DOCTYPE html>
										<html>
										<head>
										<meta charset='UTF-8'>
										<title>Excel Preview</title>
										<style>
											html, body {
												margin: 0;
												padding: 0;
												width: 100%;
												height: 100%;
												overflow: auto;
												background: #fff;
											}
											table {
												border-collapse: collapse;
											}
										</style>
										</head>
										<body>
										" + html + @"
										</body>
										</html>";

				return Content(wrappedHtml, "text/html", Encoding.UTF8);
			}
		}

@imparthgalani

To convert an Excel file into HTML for web preview using Aspose.Cells for .NET, you can utilize the HtmlSaveOptions class. This allows you to save the entire worksheet as an HTML file, which can then be displayed in a scrollable format in a browser.

Cause:

You may need to present Excel data on a web page, and converting it to HTML is an effective way to achieve this.

Solution:

Here’s a simple example to get you started:

using Aspose.Cells;
using Aspose.Cells.Rendering;

class Program
{
    static void Main()
    {
        // Load the Excel file
        Workbook workbook = new Workbook("path/to/your/excel/file.xlsx");

        // Create an instance of HtmlSaveOptions
        HtmlSaveOptions saveOptions = new HtmlSaveOptions
        {
            ExportActiveWorksheetOnly = false, // Export all worksheets
            ShowAllSheets = true, // Show all sheets in the HTML
            ExportGridLines = true, // Include grid lines
            ExportHeadings = true, // Include row and column headings
            SaveAsSingleFile = true // Save as a single HTML file
        };

        // Save the workbook as HTML
        workbook.Save("output.html", saveOptions);
    }
}

Next Steps:

  1. Load the Workbook: Replace "path/to/your/excel/file.xlsx" with the path to your Excel file.
  2. Configure HtmlSaveOptions: Adjust the options to fit your requirements, such as exporting specific worksheets or modifying styles.
  3. Save the File: The output will be saved as output.html, which you can open in a web browser to view the scrollable HTML preview.

This will create a scrollable HTML preview of the entire worksheet. If you have any further questions or need additional customization, feel free to ask!

Please provide solution for this excel,
@amjad.sahi, @Professionalize.Discourse, @John.He, @simon.zhao, @Aspose.CellsUser

@imparthgalani,

Thanks for the sample files.

I tested your scenario using the provided template XLSX file with Aspose.Cells to convert it to HTML. The process worked smoothly, and the resulting HTML appears as expected, closely resembling the original Excel spreadsheet. Additionally, it is scrollable both horizontally and vertically (using scrollbars attached) when viewed in a browser, such as Google Chrome.

Workbook workbook = new Workbook("e:\\test2\\(æSòö)_210401ü`320331.xlsx");

// Configure HTML export options for full fidelity
Aspose.Cells.HtmlSaveOptions options = new Aspose.Cells.HtmlSaveOptions
{
				ExportActiveWorksheetOnly = false,  // All sheets
				ExportGridLines = true,
				PresentationPreference = true,      // Better visual fidelity
				ExportRowColumnHeadings = true,     // Show column/row headers
				ExportHiddenWorksheet = true,				
				ExportWorksheetCSSSeparately = false,
				ExportSimilarBorderStyle = true,
				ExportImagesAsBase64 = true,        // Shapes & images inline
				HtmlCrossStringType = HtmlCrossType.Cross,
				CellCssPrefix = "c"                 // Avoid CSS conflicts
};

workbook.Save("e:\\test2\\out1_html.html", options);

Please find attached the output HTML file in the zipped archive for your reference.
out1_html.zip (2.1 MB)

Please open/check the output HTML file and in case you find any issue in it, please share some screen captures or demo video to highlight the problems and problematic areas. Also, please share your expected output HTML file for reference. We will look into it soon.

I will check this, thanks for answering @amjad.sahi

@imparthgalani,

Sure, please take your time and in case, you find any issues, please provide more details, screenshots or/and demo video to demonstrate the problems. Also, provide a sample HTML with your expected results.