Muss man eine HTML-Serie oder eine Webseite in Excel laden, um zu verarbeiten, zu visualisieren oder zu speichern?Dieser Leitfaden erläutert, wie man HTML-Inhalte direkt in ein Excel-Workshop konvertiert, indem Aspose.Cells für .NET verwendet wird.

Einführung

Umwandeln von HTML-Daten in Excel kann eine herausfordernde Aufgabe sein, da die Komplexität der HTML Strukturen und die Notwendigkeit für präzise Formatierung im Excel. jedoch mit Aspose.Cells für .NET, wird dieser Prozess einfach und effizient.

Dieser Artikel wird Sie durch die Schritte gehen, die erforderlich sind, um eine HTML-Serie oder Datei in ein Excel-Workshop mit C# zu konvertieren .xlsx Das Datei.

Verwenden Sie Fälle für HTML zu Excel Konvertierung

  • Konvertieren von E-Mails oder CMS-Daten in Excel
  • Verarbeitung von HTML-Berichten oder Exporte von Drittanbietern
  • Importieren von Webtabellen in strukturierte Spreadsheets

Schritt für Schritt Guide

Schritt 1: Installieren Sie Aspose.Cells für .NET

$ dotnet add package Aspose.Cells

Schritt 2: HTML als String vorbereiten

Bereiten Sie Ihre HTML-Inhalte entweder aus einer externen Quelle oder direkt innerhalb des Codes vor.

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

Schritt 3: Umwandeln von String in Stream

Umwandeln Sie die HTML-Serie in eine MemoryStream Objekt, das erforderlich ist, um HTML-Inhalte zu laden.

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

Schritt 4: Laden Sie HTML Stream mit HtmlLoadOptions

Use HtmlLoadOptions um die HTML-Inhalte von der MemoryStream und schafft eine neue Workbook Das Objekt.

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

Schritt 5: Arbeiten mit dem Arbeitsbuch (optional)

Sobald die HTML-Inhalte in eine Workbook, Sie können es wie nötig manipulieren. Zum Beispiel möchten Sie möglicherweise Formeln oder Stile hinzufügen.

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
}

Schritt 6: Speichern Sie die resultierende Excel-Datei

Schließlich wird die modifizierte Workbook Eine Datei auf dem Disk.

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);
}

Vollständiges Code Beispiel

Hier ist ein vollständiges Beispiel, das zeigt, wie man eine HTML-Serie in eine Excel-Datei mit Aspose.Cells für .NET umwandelt.

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.");
    }
}

Beste Praktiken

PraxisWordsile
Streams für die Web-Integration verwendenLeichter in APIs zu handhaben
Use HtmlLoadOptionsAnpassung oder Verarbeitung fortschrittlicher HTML-Inhalte
Auto-Fit KolumneVerbesserung der Lesbarkeit der Produktion

Abschnitt: Handeln mit komplexen HTML-Strukturen

Beim Umgang mit komplexen HTML-Strukturen ist es wichtig, die HtmlLoadOptions Klasse, um zu personalisieren, wie Aspose.Cells die HTML verpasst. Dies kann Setup-Optionen wie ignorieren bestimmte Elemente oder Handeln mit bestimmten Tags anders umfassen.

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.");
    }
}

Schlussfolgerungen

Konvertieren HTML-Inhalte in Excel mit Aspose.Cells für .NET ist eine leistungsfähige Möglichkeit, Webdaten in Ihre Excel-Workflows zu integrieren.

More in this category