Com convertir una taula de treball en imatge en C#
Com convertir una taula de treball en imatge en C#
L’exportació d’una sola taula de treball de Excel a un format de imatge (per exemple, PNG, JPEG) és útil en generar previstes, exportar gràfics, o compartir representacions visuals de contingut de taules de reproducció. Aquest guia t’ha mostrat com convertir una tauleta de feina d’un tauler de treballar de l’Excel a una imatge utilitzant Aspose.Cells per .NET.
Utilitza els casos
- Generar una previsió d’un full de treball específic
- Exportació informes formatats per correu electrònic o documentació
- Incorporar un sol full en una pàgina web o PDF
Guia de pas a pas
Pas 1: Instal·lar Aspose.Cells per a .NET
dotnet add package Aspose.Cells
Pas 2: Carregar el fitxer d’Excel
Workbook workbook = new Workbook("SalesData.xlsx");
Worksheet sheet = workbook.Worksheets["Q1 Report"]; // Access specific worksheet
Pas 3: Definició d’opcions de rendiment d’imatge
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
OnePagePerSheet = true,
HorizontalResolution = 200,
VerticalResolution = 200,
PrintingPageType = PrintingPageType.Default
};
Pas 4: Creació d’objectes SheetRender
SheetRender renderer = new SheetRender(sheet, options);
Pas 5: Renderar cada pàgina a una imatge
for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
{
string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
renderer.ToImage(pageIndex, imageName);
}
Pas 6: Salvar les imatges
Aquest codi emmagatzema automàticament una imatge per pàgina impresible a la taula de treball.
Pas 7: millores opcionals
Podeu aplicar configuracions de layout addicionals:
// Show gridlines in the output image
options.ShowGridLines = true;
// Fit all content on a single page
options.AllColumnsInOnePagePerSheet = true;
Codi d’exemple complet
using System;
using Aspose.Cells;
class Program
{
static void Main()
{
// Load the Excel workbook
Workbook workbook = new Workbook("SalesData.xlsx");
// Access a specific worksheet
Worksheet sheet = workbook.Worksheets["Q1 Report"];
// Define image rendering options
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
OnePagePerSheet = true,
HorizontalResolution = 200,
VerticalResolution = 200,
PrintingPageType = PrintingPageType.Default
};
// Enable gridlines if desired
options.ShowGridLines = true;
// Render the sheet to image(s)
SheetRender renderer = new SheetRender(sheet, options);
for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
{
string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
renderer.ToImage(pageIndex, imageName);
Console.WriteLine($"Saved: {imageName}");
}
Console.WriteLine("Worksheet successfully rendered to image(s).");
}
}
Escenaris comuns i solució de problemes
qüestió | Solució |
---|---|
Cut-off de continguts | Use AllColumnsInOnePagePerSheet = true |
La producció és de baixa qualitat | Augmenta la resolució de la imatge |
Les xarxes desaparegudes | Set ShowGridLines = true |