需要将 HTML 字符串或 Web 页面加载到 Excel 处理、视图或存储? 本指南解释了如何将 HTML 内容直接转换为 Excel 工作簿,使用 Aspose.Cells for .NET.

引入

将 HTML 数据转换为 Excel 可能是一个具有挑战性的任务,因为HTML 结构的复杂性和需要在 Excel 中精确格式化.

本文将通过转换一个HTML行或文件到一个Excel工作簿使用C#所需的步骤 .xlsx 文件.

使用 HTML 到 Excel 转换案例

  • 将电子邮件或CMS数据转换为Excel
  • 处理来自第三方平台的HTML报告或出口
  • 将网页图导入结构化分布表

步骤指南

步骤 1: 安装 Aspose.Cells 为 .NET

$ dotnet add package Aspose.Cells

步骤2:准备HTML作为线条

准备您的 HTML 内容,无论是从外部来源,还是直接在代码中.

string htmlString = "<html><body><table><tr><td>Item</td><td>Price</td></tr><tr><td>Book</td><td>20</td></tr></table></body></html>";

步骤3:将线路转换为流

将 HTML 字符串转换为 A MemoryStream 对象,需要加载 HTML 内容.

using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)))
{
    // Proceed with loading the stream into an Excel workbook
}

步骤4:使用HtmlLoadOptions加载HTML流

使用 HtmlLoadOptions 将 HTML 内容从 MemoryStream 创造一个新的 Workbook 对象.

using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)))
{
    Workbook workbook = new Workbook(htmlStream, new HtmlLoadOptions());
}

步骤5:与工作簿工作(可选)

一旦 HTML 内容被加载到 A Workbook, 例如,你可能想添加公式或风格.

using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)))
{
    Workbook workbook = new Workbook(htmlStream, new HtmlLoadOptions());
    Worksheet sheet = workbook.Worksheets[0];
    // Add formulas, styles, or modify data
}

步骤6:保存结果的Excel文件

最后,保存已修改的 Workbook 在磁盘上的文件.

using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)))
{
    Workbook workbook = new Workbook(htmlStream, new HtmlLoadOptions());
    // Optional: Modify the data or format
    Worksheet sheet = workbook.Worksheets[0];
    sheet.AutoFitColumns();
    workbook.Save("converted.xlsx", SaveFormat.Xlsx);
}

完整代码示例

下面是一个完整的例子,表明如何将 HTML 字符串转换为 Excel 文件,使用 Aspose.Cells for .NET.

using System;
using System.IO;
using System.Text;
using Aspose.Cells;

public class HtmlToExcelConverter
{
    public static void Main()
    {
        string html = "<html><body><table><tr><td>Name</td><td>Score</td></tr><tr><td>Alice</td><td>92</td></tr></table></body></html>";

        using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
        {
            Workbook workbook = new Workbook(htmlStream, new HtmlLoadOptions());
            Worksheet sheet = workbook.Worksheets[0];
            sheet.AutoFitColumns();
            workbook.Save("html_to_excel.xlsx", SaveFormat.Xlsx);
        }

        Console.WriteLine("HTML converted to Excel.");
    }
}

最佳实践

实践福利
使用流用于网页集成更容易在API中处理
使用 HtmlLoadOptions定制或处理先进的HTML内容
自定义列提高生产的可读性

主条目:处理复杂的HTML结构

在处理复杂的HTML结构时,重要的是要使用 HtmlLoadOptions 可自定义 ASPOSE.CELL 如何分配 HTML. 这可能包括设置选项,如忽略某些元素或以不同的方式处理特定标签.

using System;
using System.IO;
using System.Text;
using Aspose.Cells;

public class ComplexHtmlToExcelConverter
{
    public static void Main()
    {
        string complexHtml = @"
            <html>
                <body>
                    <table border='1'>
                        <tr>
                            <th>Product</th>
                            <th>Price</th>
                            <th>Quantity</th>
                        </tr>
                        <tr>
                            <td>Laptop</td>
                            <td>$999.99</td>
                            <td>5</td>
                        </tr>
                        <tr>
                            <td>Smartphone</td>
                            <td>$699.99</td>
                            <td>10</td>
                        </tr>
                    </table>
                </body>
            </html>";

        // Create a MemoryStream from the HTML string
        using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(complexHtml)))
        {
            // Use HtmlLoadOptions to customize parsing if needed
            HtmlLoadOptions loadOptions = new HtmlLoadOptions();
            loadOptions.AutoFilterColumns = true;  // Enable auto-filter for columns

            // Load the HTML content into a Workbook
            Workbook workbook = new Workbook(htmlStream, loadOptions);

            // Access the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            // Auto-fit all columns to improve readability
            sheet.AutoFitColumns();

            // Save the resulting Excel file
            workbook.Save("complex_html_to_excel.xlsx", SaveFormat.Xlsx);
        }

        Console.WriteLine("Complex HTML converted to Excel.");
    }
}

结论

将 HTML 内容转换为 Excel 使用 Aspose.Cells for .NET 是一个强大的方式,将 Web 数据集成到 Excel 的工作流中.

More in this category