Skip to content

A Xlsx to Html file converter and parser. Support cell fill, font, border, alignment and other styles. Support custom column width and row height. Support vertical and/or horizontal merged cells. Support sheet tab color and hidden sheet. Support pictures. Support progress callback. It only depends on the Microsoft Open Xml SDK.

License

Notifications You must be signed in to change notification settings

Fei-Sheng-Wu/XlsxToHtmlConverter

Repository files navigation

XlsxToHtmlConverter

C# Target Framework Target Framework Target Framework Nuget Lincense

A Xlsx to Html file converter and parser. Support cell fill, font, border, alignment and other styles. Support custom column width and row height. Support vertical and/or horizontal merged cells. Support sheet tab color and hidden sheet. Support pictures. Support progress callback. It only depends on the Microsoft Open Xml SDK.

Dependencies

DocumentFormat.OpenXml = 2.20.0

Main Features

  • Cell fill, font, border, alignment, and other styles
  • Custom column width and row height
  • Vertical and/or horizontal merged cells
  • Sheet tab color and hidden sheet
  • Pictures embedding as Base64 images
  • Conversion progress callback

Original Xlsx File

Original Xlsx File

Converted Html File

Converted Html File

How to Use

Only one line to convert a Xlsx file to Html with the use of Stream.

XlsxToHtmlConverter.Converter.ConvertXlsx(inputStream, outputStream);

Or to convert with specific ConverterConfig and progress callback.

XlsxToHtmlConverter.Converter.ConvertXlsx(inputStream, outputStream, config, progressCallback);

Convert Local Files

Just use a string of the path to the file instead of a Stream to convert a local Xlsx file.

string filename = @"C:\path\to\file.xlsx";
XlsxToHtmlConverter.Converter.ConvertXlsx(filename, outputStream);

A third optional parameter can be set to decide whether to use MemoryStream or FileStream. When set to false, it uses a FileStream to read the file instead of loading the entire file into a MemoryStream at once, which reduces the memory usage but at the cost of slowing down the conversion significantly.

XlsxToHtmlConverter.Converter.ConvertXlsx(filename, outputStream, false);

Conversion Configurations

ConverterConfig include flexible and customizable conversion configurations.

In cases where the converter is unable to produce the correct stylings, it is suggested to set ConvertStyle to false, which will ensure the conversion of all the content with default stylings.

XlsxToHtmlConverter.ConverterConfig config = new XlsxToHtmlConverter.ConverterConfig()
{
    PageTitle = "My Title",
    PresetStyles = "body { background-color: skyblue; } table { width: 100%; }",
    ErrorMessage = "Oh, no. An error occured.",
    Encoding = System.Text.Encoding.UTF8,
    ConvertStyle = true,
    ConvertSize = true,
    ConvertPicture = true,
    ConvertSheetNameTitle = true,
    ConvertHiddenSheet = false,
    ConvertFirstSheetOnly = false,
    ConvertHtmlBodyOnly = false
};

Progress Callback

A progress callback event can be set up with ConverterProgressCallbackEventArgs, where things like ProgressPercent can be used.

EventHandler<XlsxToHtmlConverter.ConverterProgressCallbackEventArgs> progressCallback = ConverterProgressCallback;
private static void ConverterProgressCallback(object sender, XlsxToHtmlConverter.ConverterProgressCallbackEventArgs e)
{
    string info = string.Format("{0:##0.00}% (Sheet {1} of {2} | Row {3} of {4})", e.ProgressPercent, e.CurrentSheet, e.TotalSheets, e.CurrentRow, e.TotalRows);
    string progress = new string('█', (int)(e.ProgressPercent / 2)) + new string('░', (int)((100 - e.ProgressPercent) / 2));
    Console.WriteLine(info + new string(' ', 5) + progress);
}

License

This project is under the MIT License.

About

A Xlsx to Html file converter and parser. Support cell fill, font, border, alignment and other styles. Support custom column width and row height. Support vertical and/or horizontal merged cells. Support sheet tab color and hidden sheet. Support pictures. Support progress callback. It only depends on the Microsoft Open Xml SDK.

Topics

Resources

License

Stars

Watchers

Forks

Languages