C#/VB.NET: Convert Excel to Images

2022-03-25 07:19:00 Written by  support iceblue
Rate this item
(1 Vote)

In daily work, you may come across some situations where you need to convert Excel to images, such as attaching a cell range to a PowerPoint presentation or safely sending your spreadsheet data via email. This article will show you how to programmatically convert Excel to images from the following two aspects using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert a Whole Excel Worksheet to an Image

The following are steps to convert a whole Excel worksheet to an image.

  • Create a Workbook instance.
  • Load an Excel sample document using Workbook.LoadFromFile() method.
  • Get a specific worksheet of the document using Workbook.Worksheets[] property.
  • Save the worksheet as an image using Worksheet.SaveToImage() method.
  • C#
  • VB.NET
using Spire.Xls;
namespace Xls2Image

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel sample document
            workbook.LoadFromFile(@"sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Save the worksheet as an image
            sheet.SaveToImage("XlsToImage.jpg");
        }
    }
}

C#/VB.NET: Convert Excel to Images

Convert a Specific Cell Range to an Image

In addition to converting a whole worksheet to an image, Spire.XLS for .NET also supports converting a specific cell range of a worksheet to an image. Detailed steps are listed below.

  • Create a Workbook instance.
  • Load an Excel sample document using Workbook.LoadFromFile() method.
  • Get a specific worksheet of the document using Workbook.Worksheets[] property.
  • Specify a cell range and save it as the Image object using Worksheet.ToImage() method, and then save the object as a certain image format using Image.Save() method.
  • C#
  • VB.NET
using Spire.Xls;
using System.Drawing.Imaging;

namespace SpecificCellsToImage
{

    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel sample document
            workbook.LoadFromFile(@"sample.xlsx");

            //Get the first worksheet 
            Worksheet sheet = workbook.Worksheets[0];

            //Specify a cell range and save it to a certain image format
            sheet.ToImage(1, 1, 6, 4).Save("CellRangeToImage.png", ImageFormat.Png);          
        }
        }
    }

C#/VB.NET: Convert Excel to Images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 25 March 2022 02:05