Cara Mengonversi Excel ke HTML di C#
Cara Mengonversi Excel ke HTML di C#
Mengekspor data Excel ke format HTML adalah penting ketika menampilkan konten spreadsheet dalam browser, template email, atau aplikasi web. panduan ini menunjukkan bagaimana untuk menukar buku kerja Excel ke HTML menggunakan Aspose.Cells untuk .NET.
Kapan Menggunakan Excel untuk Konversi HTML
- Menghasilkan preview dari data Excel di situs web
- Memungkinkan web-based spreadsheet viewing
- Mengintegrasikan data tabel ke CMS atau blog
Panduan Langkah-Langkah
Langkah 1: Instal Aspose.Cells untuk .NET
dotnet add package Aspose.Cells
Langkah 2: Mengisi buku kerja
Workbook workbook = new Workbook("path/to/excel.xlsx");
Langkah 3: Setkan HTML Save Options (Optional)
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
Langkah 4: Ekspor Excel ke File HTML
workbook.Save("path/to/output.html", options);
Langkah 5: Simpan ke MemoryStream bukannya File
using (MemoryStream outputStream = new MemoryStream())
{
workbook.Save(outputStream, SaveFormat.Html);
outputStream.Position = 0;
// Use the stream in a web response, save to disk, etc.
}
Contoh kode lengkap
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.");
}
}
Tips dan Praktik Terbaik
Tips | deskripsi |
---|---|
Penggunaan HtmlSaveOptions | Fine-tune output struktur HTML, karakter koding, atau image embedding |
Simpan untuk Stream | berguna untuk APIs atau fungsi tanpa server |
Simpan styling | Aspose.Cells memastikan bahwa sebagian besar elemen styling dan layout disimpan |