Cum să convertiți o singură celulă Excel în imagine în C#

Cum să convertiți o singură celulă Excel în imagine în C#

Uneori, tot ce aveți nevoie este o valoare unică – un preț, o etichetă, un cod – și doriți să exportați această celulă vizual. Acest tutorial vă arată cum să izolați și să faceți o singură celula Excel într-o imagine folosind Aspose.Cells pentru .NET.

Casele de utilizare reală

  • Prețuri de export sau total pentru afișările de produse
  • Metricile cheie izolate pentru dashboards
  • Generați miniere de imagine pentru valori individuale

Ghidul pas cu pas

Pasul 1: Instalați Aspose.Cells pentru .NET

dotnet add package Aspose.Cells

Pasul 2: Încărcați cartea de lucru și cartea de lucru

Workbook workbook = new Workbook("KPIReport.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Pasul 3: Selectați celulă țintă

// Example: Cell B5
Cell cell = sheet.Cells["B5"];

Pasul 4: Setarea spațiului de imprimare în celulă

// Print only that one cell
sheet.PageSetup.PrintArea = "B5";

Pasul 5: Configurați opțiunile de renderizare a imaginii

ImageOrPrintOptions options = new ImageOrPrintOptions
{
    ImageType = ImageType.Png,
    OnePagePerSheet = true,
    HorizontalResolution = 300,
    VerticalResolution = 300
};

Pasul 6: Render folosind SheetRender

SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "cell_b5_output.png");

Pasul 7: Salvați și revizuiți rezultatul

Veți obține un PNG curat care arată doar aceea celulă cu formatarea intactă.

Cod complet de exemplu

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

Sfaturi utile

TipuriDescriere
Îmbunătățirea readabilitățiiCreșterea rezoluției sau a dimensiunii fontului
Adăugați fundal sau frontierăFormatul celulei înainte de renderare
Alinierea conținutuluiUse cell.GetStyle() to tweak alignment or padding
 Română