Introduction

In Microsoft Excel, it is pretty easy to export an Excel range as an image with the following procedure.

However, in this article, I'll introduce you to how to convert section(s) to an image programmatically using the free Spire.XLS in C#. As a matter of fact, Spire.XLS has provided a Worksheet.SaveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn) method for programmers to save a selected range of cells to an image easily. Now let's check the detailed procedure as in the following.

Step 1. Create a new Excel document and load the test file.

Step 2. Get the first sheet from the workbook

Step 3: Save a specific cell's range to an image via Worksheet.SaveToImage().

Test File

test file

Example

// Initialize a new Workbook object
Workbook workbook = new Workbook();
// Open Template Excel file
workbook.LoadFromFile("Sample.xlsx");
// Get the first worksheet in Excel file
Worksheet sheet = workbook.Worksheets[0];
// Specify Cell Ranges and Save to certain Image formats
sheet.SaveToImage(1, 1, 6, 6).Save("Image1.png", ImageFormat.Png);
sheet.SaveToImage(7, 1, 13, 6).Save("Image2.jpeg", ImageFormat.Jpeg);
sheet.SaveToImage(14, 1, 19, 6).Save("Image3.bmp", ImageFormat.Bmp);

Output

output 1

output 2nd range

output 3 range