How to Convert a Single Excel Cell to Image in C#
How to Convert a Single Excel Cell to Image in C#
Sometimes all you need is a single value — a price, a label, a code — and you want to export that cell visually. This tutorial shows you how to isolate and render a single Excel cell to an image using Aspose.Cells for .NET.
Real-World Use Cases
- Export prices or totals for product displays
- Isolate key metrics for dashboards
- Generate image thumbnails for individual values
Step-by-Step Guide
Step 1: Install Aspose.Cells for .NET
dotnet add package Aspose.Cells
Step 2: Load the Workbook and Worksheet
Workbook workbook = new Workbook("KPIReport.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Step 3: Select the Target Cell
// Example: Cell B5
Cell cell = sheet.Cells["B5"];
Step 4: Set the Print Area to the Cell
// Print only that one cell
sheet.PageSetup.PrintArea = "B5";
Step 5: Configure Image Rendering Options
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
OnePagePerSheet = true,
HorizontalResolution = 300,
VerticalResolution = 300
};
Step 6: Render Using SheetRender
SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "cell_b5_output.png");
Step 7: Save and Review the Output
You will get a clean PNG showing just that one cell with formatting intact.
Complete Example Code
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.");
}
}
Helpful Tips
Tip | Description |
---|---|
Enhance readability | Increase resolution or font size |
Add background or border | Format cell before rendering |
Align content | Use cell.GetStyle() to tweak alignment or padding |