Skip to content

Uses EPPlus and HtmlAgilityPack to convert an HTML table to an Excel file

Notifications You must be signed in to change notification settings

mrpynk37/HtmlToExcel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TowerSoft HtmlToExcel

Currently in Beta.

Small Nuget package that uses HtmlAgilityPack to read a HTML table and generate an Excel file using EPPlus

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.

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
data-excel-hyperlink URI
data-excel-bold Boolean
data-excel-comment String
data-excel-comment-author String
colspan Integer

ASP Core 2 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");
}

About

Uses EPPlus and HtmlAgilityPack to convert an HTML table to an Excel file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%