Convert Part of Excel to Image Using Free Excel API

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

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

However, in this article, I'll introduce you top 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.

Main procedure

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

Entire Code

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