DEV Community

Cover image for Easy Steps to Export HTML Tables to an Excel Worksheet in C#
Suresh Mohan for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

Easy Steps to Export HTML Tables to an Excel Worksheet in C#

Syncfusion Excel (XlsIO) Library is a .NET Excel library that allows users to export data from various data sources to Excel worksheets in C# and VB.NET. This library allows you to export even HTML tables to Excel worksheets. This feature will be useful when there is a need to extract data from an HTML document and manage it.

If you are looking for easy steps to do this, then you are in the right place.

Consider a scenario where you have webpages with HTML tables. To retrieve data from each webpage, you might need to copy the tables in the page manually. To automate this process, XlsIO provides the support to export any number of HTML tables in a webpage to an Excel worksheet. This makes the work simple and saves time, too. The export includes the table formatting, as well.

Let’s see how to export HTML tables from a webpage to Excel worksheets in C# using XlsIO.

Export HTML table

The ImportHtmlTable method loads an HTML file and imports all the tables in the file to the worksheet. This import operation includes the table formatting that is defined within the HTML file.

For demonstration, I am going to create an ASP.NET Core application to export HTML tables to an Excel worksheet using the Syncfusion XlsIO library.

Step 1: Create an ASP.NET Core project.

Step 2: Add a view; I am naming it ExportHtmlTables.cshtml. Add the following sample code. This code contains an HTML table in the HTML DIV, a hidden field, and an Export button. This button has been assigned a jQuery click event handler to copy the contents of the HTML DIV to the hidden field, so that the HTML string can be sent to the server.

@{Html.BeginForm("ImportHtmlTable", "ImportHtmlTable", FormMethod.Post);
    {
        <div class="Common">
            <div class="tablediv">
                <div class="rowdiv">
                    <p>
                        Essential XlsIO supports exporting HTML tables into Excel worksheets. The <b>ImportHtmlTable</b> method loads an HTML file and exports all the tables in the file to the worksheet.
                    </p>
                    <b>Features:</b>
                    <br />
                    <ul>
                        <li>Imports HTML table</li>
                        <li>Imports with table formatting </li>
                    </ul>
                    <br />
                    <div id="cssStyle">
                        <style type="text/css">
                            th {
                                color:rgb(0,0,0);
                                font-family:Tahoma, sans-serif;
                                font-size:10pt;
                                white-space:nowrap;
                                background-color:rgb(255,174,33);
                                border-top:solid;
                                border-top-width: thin;
                                border-bottom:solid;
                                border-bottom-width:thin;
                                border-left:solid;
                                border-left-width:thin;
                                border-right:solid;
                                border-right-width:thin;
                                border-top-color:rgb(0,0,0);
                                border-bottom-color:rgb(0,0,0);
                                border-left-color:rgb(0,0,0);
                                border-right-color:rgb(0,0,0);
                                font-weight:bold;
                                vertical-align:bottom;
                            }

                            td {
                                color:rgb(0,0,0);
                                font-family:Tahoma, sans-serif;
                                font-size:10pt;
                                white-space:nowrap;
                                background-color:rgb(239,243,247);
                                border-left:solid;
                                border-left-width:thin;
                                border-right:solid;
                                border-right-width:thin;
                                border-top-color:rgb(0,0,0);
                                border-bottom-color:rgb(0,0,0);
                                border-bottom:solid;
                                border-bottom-width:thin;
                                border-left-color:rgb(0,0,0);
                                border-right-color:rgb(0,0,0);
                                vertical-align:bottom;
                            }
                        </style>
                    </div>
                    <div id="Grid">
                        <table cellspacing="0" width="500" style="table-layout:fixed;border-collapse:collapse;width:500pt">
                            <tr>
                                <th class="X64" style="text-align:left;">
                                    <span>CustomerID</span>
                                </th>
                                <th class="X64" style="text-align:left;">
                                    <span>CompanyName</span>
                                </th>
                                <th class="X64" style="text-align:left;">
                                    <span>ContactName</span>
                                </th>
                                <th class="X64" style="text-align:left;">
                                    <span>Phone</span>
                                </th>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>ALFKI</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Alfreds Futterkiste</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Maria Anders</span>
                                </td>

                                <td class="X65" style="text-align:left;">
                                    <span>030-0074321</span>
                                </td>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>ANATR</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Ana Trujillo Emparedados</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Ana Trujillo</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>(5) 555-4729</span>
                                </td>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>ANTON</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Antonio Moreno Taquería</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Antonio Moreno</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>(5) 555-3932</span>
                                </td>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>AROUT</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Around the Horn</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Thomas Hardy</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>(171) 555-7788</span>
                                </td>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>BERGS</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Berglunds snabbköp</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Christina Berglund</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>0921-12 34 65</span>
                                </td>
                            </tr>
                            <tr height="25">
                                <td class="X65" style="text-align:left;">
                                    <span>BLAUS</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Blauer See Delikatessen</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>Hanna Moos</span>
                                </td>
                                <td class="X65" style="text-align:left;">
                                    <span>0621-08460</span>
                                </td>
                            </tr>

                        </table>
                    </div>
                    </div>
                    <br />
                    <input name="tableHTML" id="tbl" hidden="hidden"/>
                    <div class="rowdiv">
                        <div class="celldiv">
                            <input id="ExportTbl" class="buttonStyle" type="submit" name="button" value="Export" onclick="exportHTML()" style="width:125px;" />
                        </div>
                    </div>
                </div>
        </div>
        Html.EndForm();
    } }

        <script type="text/javascript">
                function exportHTML() {
                    let value = "<html>" + document.getElementById("cssStyle").innerHTML + "<body>" + document.getElementById("Grid").innerHTML + "</body></html>";
                    value = value.replace("<tbody>", "");
                    value = value.replace("</tbody>", "");
                    document.getElementById("tbl").setAttribute("value", value);
            }
        </script>

This code will create the webpage shown in the following screenshot.

Input webpage with HTML table
Input webpage with HTML table

Step 3: Create an Excel document in the controller class ExportHtmlTableController.cs.

Step 4: The HTML string parsed into the hidden field is now passed to the click event. Then, convert the HTML string into a stream and load it using the ImportHtmlTable method, then export it to an Excel worksheet. The start row and column must be specified while exporting them. After exporting to a worksheet, auto fit the rows and columns and save the output Excel file.

The following code example shows how to export a webpage with an HTML table to an Excel worksheet in C# using XlsIO.

public ActionResult ImportHtmlTable(string button, string tableHTML)
{
    if (button == null)
        return View();

    MemoryStream ms = new MemoryStream();

    // The instantiation process consists of two steps.
    // Step 1: Instantiate the spreadsheet creation engine.
    using (ExcelEngine excelEngine = new ExcelEngine())
    {

        // Step 2 : Instantiate the Excel application object.
        IApplication application = excelEngine.Excel;
        application.DefaultVersion = ExcelVersion.Excel2016;

        // A workbook is created.
        IWorkbook workbook = application.Workbooks.Create(1);

        // The first worksheet object in the worksheets collection is accessed.
        IWorksheet worksheet = workbook.Worksheets[0];

        byte[] byteArray = Encoding.UTF8.GetBytes(tableHTML);

        MemoryStream file = new MemoryStream(byteArray);

        // Imports HTML table into the worksheet from first row and first column.
        worksheet.ImportHtmlTable(file, 1, 1);

        worksheet.UsedRange.AutofitColumns();
        worksheet.UsedRange.AutofitRows();

        workbook.SaveAs(ms);
        ms.Position = 0;
    }

    return File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx");
}

Note: You can also use this ImportHtmlTable method to load an HTML file from a disk to export the HTML tables to an Excel worksheet. For that, you need to specify the starting row and column values while importing the HTML file. Refer to the following code example.

//Exports HTML tables to the worksheet from first row and first column.
worksheet.ImportHtmlTable("Import-HTML-Table.html", 1, 1);

The following screenshot is the output of the Excel file exported from the webpage with the HTML table.

Excel document with HTML table
Excel document with HTML table

GitHub samples

You can download the example of exporting HTML tables to Excel worksheets in C# on this GitHub page.

Wrapping up

As you can see, Syncfusion Excel (XlsIO) Library provides support to easily export HTML tables to Excel in C#. Take a moment to peruse the documentation, where you’ll find other importing options and features like data tables, collection objects, a grid view, data column, and CSV, all with accompanying code samples.

Using the XlsIO library, you can also export Excel data to PDF, image, data table, CSV, TSV, HTML, collections of objects, ODS , JSON, and more file formats.

If you are new to our Excel Library, it is highly recommended to follow our Getting Started guide.

Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a free, 30-day trial here.

If you have any questions about these features, please let us know in the comments section below. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are always happy to assist you!

Top comments (0)