Ako premeniť jednu Excel bunku na obrázok v C#
Ako premeniť jednu Excel bunku na obrázok v C#
Niekedy všetko, čo potrebujete, je jediná hodnota - cena, štítok, kód - a chcete vizuálne exportovať túto bunku.Tento tutoriál vám ukáže, ako izolovať a renderovať jednu bunku Excel do obrazu pomocou Aspose.Cells pre .NET.
Reálne svetové prípady
- Vývozné ceny alebo sumy pre produktové displeje
- Izolácia kľúčových metrík pre dashboards
- Vytvorenie obrázkov pre jednotlivé hodnoty
Krok za krokom sprievodca
Krok 1: Inštalácia Aspose.Cells pre .NET
dotnet add package Aspose.Cells
Krok 2: Nahrať pracovnú knihu a pracovnú dosku
Workbook workbook = new Workbook("KPIReport.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Krok 3: Vyberte cieľovú bunku
// Example: Cell B5
Cell cell = sheet.Cells["B5"];
Krok 4: Nastaviť tlačovú oblasť do bunky
// Print only that one cell
sheet.PageSetup.PrintArea = "B5";
Krok 5: Nastavenie možnosti renderovania obrazu
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
OnePagePerSheet = true,
HorizontalResolution = 300,
VerticalResolution = 300
};
Krok 6: Render pomocou SheetRender
SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "cell_b5_output.png");
Krok 7: Uložiť a preskúmať výstup
Dostanete čistý PNG, ktorý zobrazuje iba jednu bunku s formátovaním intaktné.
Kompletný príklad kódu
using System;
using Aspose.Cells;
class Program
{
static void Main()
{
// Load workbook
Workbook workbook = new Workbook("KPIReport.xlsx");
// Access the worksheet and target cell
Worksheet sheet = workbook.Worksheets[0];
Cell cell = sheet.Cells["B5"];
// Set print area to that cell
sheet.PageSetup.PrintArea = "B5";
// Image export settings
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
OnePagePerSheet = true,
HorizontalResolution = 300,
VerticalResolution = 300
};
// Render and save
SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "cell_b5_output.png");
Console.WriteLine("Cell B5 exported successfully as image.");
}
}
Užitočné tipy
Tipy | Popis |
---|---|
Zlepšenie čitateľnosti | Zvýšiť rozlíšenie alebo veľkosť písma |
Pridajte pozadie alebo hranice | Formát buniek pred renderovaním |
Align obsah | Use cell.GetStyle() to tweak alignment or padding |