Skip to content

Repository files navigation

TowerSoft HtmlToExcel

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

Usage

Single sheet

stringhtmlString="<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";byte[]fileData=newWorkbookGenerator().FromHtmlString(htmlString);

Multiple sheets

stringhtmlString1="<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";stringhtmlString2="<table><tbody><tr><td>Cell contents</td></tr></tbody></table>";byte[]fileData;using(WorkbookBuilderworkbookBuilder=newWorkbookBuilder()){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 NameDefault ValueDescription
AutofitColumnstrueEnables/disables fitting the width of the columns to fit the contents.
ShowFiltertrueEnables/disables showing table filters. Does not work with the table has any colspans.
ShowRowStripestrueEnables/disables row stripes. Does not work with the table has any colspans.
PrintingPageOrientationPortraittrueSets the printing page default orientation to Portrait (Landscape if false)
ShowGridLinesfalseEnables/disables grid lines

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

HtmlToExcelSettingssettings=HtmlToExcelSettings.Defaults;settings.AutofitColumns=false;settings.ShowFilter=false;settings.ShowRowStripes=false;// Using custom settings with a single sheetbyte[]fileDatanewWorkbookGenerator(settings).FromHtmlString(htmlString);// Using custom settings with multiple sheetsusing(WorkbookBuilderworkbookBuilder=newWorkbookBuilder(settings){// Settings can also be used in the AddSheet method which overrides the setting on the WorkbookBuilderworkbookBuilder.AddSheet("sheetName",htmlString,settings)}

Individual Cell Options

Attribute NameExpected Data TypeComments
data-excel-hyperlinkURICreates a hyperlink on the cell. Must be a parsable absolute URI.
data-excel-boldBooleanSets if the cell style will be set to bold.
data-wrapBooleanSets if the cell text is wrapped.
data-typeStringSets the data type for the cell. Valid options are: Text, Number, Boolean, DateTime, TimeSpan
data-formatStringSets the cell format. Only works with data-type="Number" and data-type="DateTime"
data-excel-commentStringAdds a comment to the cell
data-excel-comment-authorStringSets the author for the comment
data-horizontal-alignmentStringSets the cell horizontal alignment. Valid options are: Center, CenterContinuous, Distributed, Fill, General, Justify, Left, Right
colspanIntegerMerges this cell with the following cells
data-font-sizeIntegerSets the cell font size
data-font-colorString(HexColor)Sets the font color of the cell. Must be a valid hex color code, Example: #006688
data-background-colorString(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.

publicclassHomeController:CustomController{publicIActionResultExcelFile(){varmodel=//Get model datastring htmlString =RenderViewAsync("viewName",model,true);byte[]fileData=newWorkbookGenerator().FromHtmlString(htmlString);returnFile(fileData,MimeType.xlsx,"filename.xlsx");}}

MVC 5 Example

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

publicActionResultGetExcelFile(){varmodel=//Get model datastring htmlString =PartialView("ViewName",model).RenderToString();byte[]fileData=newWorkbookGenerator().FromHtmlString(htmlString);returnFile(fileData,MimeType.xlsx,"filename.xlsx");}

About

Uses ClosedXML and AngleSharp to convert an HTML table to an Excel file

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages