TowerSoft.HtmlToExcel 3.0.0

Prefix Reserved
dotnet add package TowerSoft.HtmlToExcel --version 3.0.0
                    
NuGet\Install-Package TowerSoft.HtmlToExcel -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TowerSoft.HtmlToExcel" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TowerSoft.HtmlToExcel" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TowerSoft.HtmlToExcel" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TowerSoft.HtmlToExcel --version 3.0.0
                    
#r "nuget: TowerSoft.HtmlToExcel, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TowerSoft.HtmlToExcel@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TowerSoft.HtmlToExcel&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TowerSoft.HtmlToExcel&version=3.0.0
                    
Install as a Cake Tool

TowerSoft HtmlToExcel

Small Nuget package that uses AngleSharp to read an HTML table and generate an Excel file using ClosedXML

Usage

Single sheet

string htmlString = "<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";

byte[] fileData = new WorkbookGenerator().FromHtmlString(htmlString);

Multiple sheets

string htmlString1 = "<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";
string htmlString2 = "<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";

byte[] fileData;
using (WorkbookBuilder workbookBuilder = new WorkbookBuilder()) {
    workbookBuilder.AddSheet("sheet1", htmlString1);
    workbookBuilder.AddSheet("sheet2", htmlString2);

    fileData = workbookBuilder.GetAsByteArray();
}

Settings

Some settings do not work if there are any colspans in the table. This is because Excel does not allow tables with merged cells and those settings only work in tables.

Setting Name Default Value Description
AutofitColumns true Enables/disables fitting the width of the columns to fit the contents.
ShowFilter true Enables/disables showing table filters. Does not work with the table has any colspans.
ShowRowStripes true Enables/disables row stripes. Does not work with the table has any colspans.
PrintingPageOrientationPortrait true Sets the printing page default orientation to Portrait (Landscape if false)
ShowGridLines false Enables/disables grid lines

You can change the settings using HtmlToExcellSettings and passing it in the constructor of htmlToEzxel

HtmlToExcelSettings settings = HtmlToExcelSettings.Defaults;
settings.AutofitColumns = false;
settings.ShowFilter = false;
settings.ShowRowStripes = false;

// Using custom settings with a single sheet
byte[] fileData new WorkbookGenerator(settings).FromHtmlString(htmlString);

// Using custom settings with multiple sheets
using (WorkbookBuilder workbookBuilder = new WorkbookBuilder(settings) {
    // Settings can also be used in the AddSheet method which overrides the setting on the WorkbookBuilder
    workbookBuilder.AddSheet("sheetName", htmlString, settings)
}
Individual Cell Options
Attribute Name Expected Data Type Comments
data-excel-hyperlink URI Creates a hyperlink on the cell. Must be a parsable absolute URI.
data-excel-bold Boolean Sets if the cell style will be set to bold.
data-wrap Boolean Sets if the cell text is wrapped.
data-type String Sets the data type for the cell. Valid options are: Text, Number, Boolean, DateTime, TimeSpan
data-format String Sets the cell format. Only works with data-type="Number" and data-type="DateTime"
data-excel-comment String Adds a comment to the cell
data-excel-comment-author String Sets the author for the comment
data-horizontal-alignment String Sets the cell horizontal alignment. Valid options are: Center, CenterContinuous, Distributed, Fill, General, Justify, Left, Right
colspan Integer Merges this cell with the following cells
data-font-size Integer Sets the cell font size
data-font-color String(HexColor) Sets the font color of the cell. Must be a valid hex color code, Example: #006688
data-background-color String(HexColor) Sets the fill/background color of the cell. Must be a valid hex color code, Example: #006688
ASP Core Example

Add the following code to your project to render a view to a string: CustomController.cs

Use this example to return the file to the client. Make sure you change the inherited class to your custom controller class.

public class HomeController : CustomController {
    public IActionResult ExcelFile() {
        var model = //Get model data

        string htmlString = RenderViewAsync("viewName", model, true);
        byte[] fileData = new WorkbookGenerator().FromHtmlString(htmlString);

        return File(fileData, MimeType.xlsx, "filename.xlsx");
    }
}
MVC 5 Example

Add the following code to your project to render a view to a string: ViewExtensions.cs

public ActionResult GetExcelFile() {
    var model = //Get model data

    string htmlString = PartialView("ViewName", model).RenderToString();
    byte[] fileData = new WorkbookGenerator().FromHtmlString(htmlString);

    return File(fileData, MimeType.xlsx, "filename.xlsx");
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 935 12/4/2024
2.4.0 6,089 4/22/2024
2.3.0 720 3/14/2024
2.2.1 323 12/28/2023
2.2.0 1,110 1/4/2023
2.1.0 7,911 5/17/2022
2.0.1 1,065 5/24/2021
2.0.0 489 4/6/2021
1.0.2 1,309 2/11/2021
1.0.1 646 6/29/2020
0.6.1 724 10/25/2019