When exporting Excel worksheets or ranges to images for printing, presentation, or embedding purposes, you often need precise control over how the content looks. This article demonstrates how to customize image rendering using Aspose.Cells for .NET, a powerful library that allows you to fine-tune every aspect of your exported images.
Introduction
Customizing Excel worksheets and ranges when converting them into images can significantly enhance their visual appeal and utility. Whether it’s for printing, embedding in presentations, or sharing online, the ability to control layout, resolution, color mode, and content visibility is crucial.
Aspose.Cells for .NET offers extensive options to customize how your Excel files are rendered as images. This article will guide you through the process of installing Aspose.Cells, loading an Excel workbook, configuring advanced rendering settings, and saving the output with desired quality settings.
Step-by-Step Guide
Step 1: Install Aspose.Cells for .NET
To get started, install the Aspose.Cells package via NuGet Package Manager:
Install-Package Aspose.Cells
Step 2: Load the Workbook and Worksheet
Load your Excel workbook and select a specific worksheet to work with:
Workbook workbook = new Workbook("Template.xlsx");
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Step 3: Configure Advanced Rendering Options
Set up advanced rendering options such as image type, resolution, transparency, and more:
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
HorizontalResolution = 300,
VerticalResolution = 300,
PrintWithStatusDialog = false,
Transparent = false,
OnePagePerSheet = true
};
Step 4: Enable Gridlines and Headings (Optional)
Show gridlines or row/column headings in the output:
options.ShowGridLines = true;
options.ShowRowColumnHeaders = true;
Step 5: Customize Page Settings
Set options to render entire sheets as single pages if needed:
options.AllColumnsInOnePagePerSheet = true;
options.AllRowsInOnePagePerSheet = true;
Step 6: Render to Image with SheetRender
Use the SheetRender
class to convert your worksheet or range into an image:
SheetRender renderer = new SheetRender(worksheet, options);
renderer.ToImage(0, "custom_output.png");
Step 7: Save and Verify Output
Ensure that the resulting image reflects your custom layout and visual choices.
Complete Example Code
Here’s a complete example demonstrating how to customize Excel rendering:
using System;
using Aspose.Cells;
class Program
{
static void Main()
{
// Load Excel file
Workbook workbook = new Workbook("Template.xlsx");
Worksheet worksheet = workbook.Worksheets["Sheet1"];
// Set up advanced rendering options
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
HorizontalResolution = 300,
VerticalResolution = 300,
PrintWithStatusDialog = false,
Transparent = false,
OnePagePerSheet = true,
ShowGridLines = true,
ShowRowColumnHeaders = true,
AllColumnsInOnePagePerSheet = true,
AllRowsInOnePagePerSheet = true
};
// Render to image
SheetRender renderer = new SheetRender(worksheet, options);
renderer.ToImage(0, "custom_output.png");
Console.WriteLine("Custom-rendered worksheet saved as image.");
}
}
Tips for Better Control
Setting | Description |
---|---|
ImageType | Choose PNG, JPEG, BMP, or TIFF |
Transparent | Set true for transparent background |
SmoothingMode | Improve visuals using anti-aliasing |
ShowFormulas | Show formulas instead of calculated values |
ChartImageWidth / ChartImageHeight | Set output size for charts |