Convert Part of Excel to Image Using Free Excel API

In MS Excel, it is pretty easy to export Excel range as image with following steps:

  • Select a section that you need to export as image.
  • Click Home>Copy>Copy as Picture, then picture will be save in clipboard.
  • Paste the copied picture to your Paint tool, then save the picture to the specified folder in specified format.

However, in this post, I’ll introduce how to convert section(s) to image programmatically using Free Spire.XLS in C#. As a matter of fact, Spire.XLS has provide a Worksheet.SaveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn) method for programmers to save selected range of cells to image easily, now let’s check the detailed steps as below.

Main steps:

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

Step 2: Get the first sheet from the workbook.

Step 3: Save specific cells range to image via Worksheet.SaveToImage().

Test File:

 test

Entire Code:

//Initialize a new Workbook object

Workbook workbook = new Workbook();

//Open Template Excel file

workbook.LoadFromFile(“Sample.xlsx”);

//Get the first wirksheet 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:

Image1.png

image1

Image2.jpeg

image2

Image3.bmp

image3

Advertisements