Miten muuntaa Excel HTML C#
Miten muuntaa Excel HTML C#
Excel-tietojen vienti HTML-muotoon on välttämätöntä, kun selaimissa, sähköpostin malleissa tai web-sovelluksissa näytetään levytiedostoa.Tämä opas näyttää, miten muuntaa Excelin työpöytä HTML:een käyttämällä Aspose.Cells for .NET.
Milloin käyttää Excelia HTML-muuntamiseen
- Luo Excel-tietojen ennakkoluuloja verkkosivustoilla
- Mahdollistaa web-pohjaisen levyn katselu
- Sisällytä taulukon tiedot CMS: ssä tai blogeihin
Vaiheittainen opas
Vaihe 1: Asenna Aspose.Cells .NET
dotnet add package Aspose.Cells
Vaihe 2: Lataa työpöytä
Workbook workbook = new Workbook("path/to/excel.xlsx");
Vaihe 3: Aseta HTML Save -vaihtoehdot (vaihtoehtoinen)
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
Vaihe 4: Export Excelin HTML-tiedostoon
workbook.Save("path/to/output.html", options);
Vaihe 5: Tallenna muistiinpanoon tiedoston sijasta
using (MemoryStream outputStream = new MemoryStream())
{
workbook.Save(outputStream, SaveFormat.Html);
outputStream.Position = 0;
// Use the stream in a web response, save to disk, etc.
}
Täydellinen esimerkki koodista
using System;
using System.IO;
using Aspose.Cells;
class Program
{
static void Main()
{
Workbook workbook = new Workbook("input.xlsx");
// Option 1: Save to HTML file
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
workbook.Save("output.html", options);
// Option 2: Save to stream for web applications
using (MemoryStream stream = new MemoryStream())
{
workbook.Save(stream, SaveFormat.Html);
stream.Position = 0;
// Use the stream as needed (e.g., send in API response)
}
Console.WriteLine("Excel exported to HTML.");
}
}
Vinkkejä ja parhaita käytäntöjä
Tyyppi | kuvaus |
---|---|
Use HtmlSaveOptions | Html-rakenne, merkki koodaus tai kuvan sisällyttäminen |
Säästä virtaa | Hyödyllinen API: lle tai palvelimattomiin toimintoihin |
Säilytä styling | Aspose.Cells varmistaa, että useimmat tyylit ja asennuksen elementit säilytetään |