Cómo convertir una sola celda de Excel a imagen en C#

Cómo convertir una sola celda de Excel a imagen en C#

A veces todo lo que necesitas es un valor único - un precio, una etiqueta, un código - y quieres exportar esa célula visualmente. este tutorial te muestra cómo aislar y render una única célula de Excel a una imagen utilizando Aspose.Cells para .NET.

Casos de uso del mundo real

  • Precios de exportación o totales para las pantallas de productos
  • Metricas clave para dashboards
  • Generar miniaturas de imagen para valores individuales

Guía paso a paso

Paso 1: Instalar Aspose.Cells para .NET

dotnet add package Aspose.Cells

Paso 2: Cargar el libro de trabajo y la hoja de trabajo

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

Paso 3: Seleccione la célula de destino

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

Paso 4: Configure el área de impresión en la célula

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

Paso 5: Configure opciones de renderización de imágenes

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

Paso 6: Render con SheetRender

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

Paso 7: Salva y revisa la salida

Usted obtendrá un PNG limpio que muestra sólo que una célula con la formatación está intacta.

Código de ejemplo completo

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

Consejos útiles

TipoDescripción
Mejora la lecturaAumentar la resolución o el tamaño de la letra
Adicionar fondo o fronterasFormato de la célula antes de renderizar
Contenido alineadoUso cell.GetStyle() para ajustar el alineamiento o el padding
 Español