C#/VB.NET: Convert Excel Worksheets to Images (PNG, JPEG, EMF, SVG)

Alice Yang
4 min readJan 20, 2022

--

In certain circumstances, you may need to convert Excel worksheets to images, for example, generating thumbnails for the worksheets. In this article, I will demonstrate how to convert Excel worksheet to image using C# and VB.NET. The following topics will be covered:

  • Convert Worksheet to PNG or JPEG in C# and VB.NET
  • Convert Worksheet to EMF in C# and VB.NET
  • Convert Worksheet to SVG in C# and VB.NET
  • Convert Specific Cells of a Worksheet to Image in C# and VB.NET

Installation

In order to achieve the conversion, I will be using Spire.XLS for .NET, which is an easy to use and feature-rich library for creating, editing, converting and printing Excel files.

The DLL files of Spire.XLS for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package Spire.XLS

Convert Worksheet to PNG or JPEG in C# and VB.NET

The following are the steps to convert an Excel worksheet to PNG or JPEG images:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Convert the worksheet to PNG/JPEG using XlsWorksheet.SaveToImage() method.

C#

using Spire.Xls;

namespace WorksheetToPngOrJpeg
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Sample.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Save to jpeg
sheet.SaveToImage("ToJPEG.jpg");
//Save to png
//sheet.SaveToImage("ToPNG.png");
workbook.Dispose();
}
}
}

VB.NET

Imports Spire.Xls

Namespace WorksheetToPngOrJpeg
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()
'Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

'Get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'Save to jpeg
sheet.SaveToImage("ToJPEG.jpg")
'Save to png
'sheet.SaveToImage("ToPNG.png");
workbook.Dispose()
End Sub
End Class
End Namespace

Convert Worksheet to EMF in C# and VB.NET

The following are the steps to convert an Excel worksheet to EMF image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Create a FileStream instance for the converted EMF image.
  • Convert the worksheet to EMF using XlsWorksheet.ToEMFStream(Stream stream, int firstRow, int firstColumn, int lastRow, int lastColumn, EmfType emfType) method.

C#

using Spire.Xls;
using System.IO;

namespace WorksheetToEMF
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Sample.xlsx");

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

//Save the worksheet to EMF
FileStream fs = new FileStream("ToEMF.emf", FileMode.Create);
sheet.ToEMFStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn, System.Drawing.Imaging.EmfType.EmfOnly);
fs.Flush();
fs.Close();
workbook.Dispose();
}
}
}

VB.NET

Imports Spire.Xls
Imports System.IO

Namespace WorksheetToEMF
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()
'Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

'Get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)

'Save the worksheet to EMF
Dim fs As FileStream = New FileStream("ToEMF.emf", FileMode.Create)
sheet.ToEMFStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn, Drawing.Imaging.EmfType.EmfOnly)
fs.Flush()
fs.Close()
workbook.Dispose()
End Sub
End Class
End Namespace

Convert Worksheet to SVG in C# and VB.NET

The following are the steps to convert an Excel worksheet to SVG image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Create a FileStream instance for the converted SVG image.
  • Convert the worksheet to SVG using XlsWorksheet.ToSVGStream(Stream stream, int firstRow, int firstColumn, int lastRow, int lastColumn) method.

C#

using Spire.Xls;
using System.IO;

namespace WorksheetToSVG
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Sample.xlsx");

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

//Save the worksheet to SVG
FileStream fs = new FileStream("ToSVG.svg", FileMode.Create);
sheet.ToSVGStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn);
fs.Flush();
fs.Close();
workbook.Dispose();
}
}
}

VB.NET

Imports Spire.Xls
Imports System.IO

Namespace WorksheetToSVG
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()
'Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

'Get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)

'Save the worksheet to SVG
Dim fs As FileStream = New FileStream("ToSVG.svg", FileMode.Create)
sheet.ToSVGStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn)
fs.Flush()
fs.Close()
workbook.Dispose()
End Sub
End Class
End Namespace

Convert Specific Cells of a Worksheet to Image in C# and VB.NET

The following are the steps to convert specific cells of a worksheet to image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Convert specific cells of the worksheet to PNG image using XlsWorksheet.ToImage(int firstRow, int firstColumn, int lastRow, int lastColumn).Save(string filename, ImageFormat format) method.

C#

using Spire.Xls;

namespace SpecificCellsToImage
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Sample.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Save specific cells to PNG image
sheet.ToImage(1, 1, 6, 3).Save("SpecificCellsToImage.png", System.Drawing.Imaging.ImageFormat.Png);

workbook.Dispose();
}
}
}

VB.NET

Imports Spire.Xls

Namespace SpecificCellsToImage
Friend Class Program
Private Shared Sub Main(ByVal args As String())

'Create a Workbook instance
Dim workbook As Workbook = New Workbook()
'Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

'Get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'Save specific cells to PNG image
sheet.ToImage(1, 1, 6, 3).Save("SpecificCellsToImage.png", Drawing.Imaging.ImageFormat.Png)
workbook.Dispose()
End Sub
End Class
End Namespace

Output after converting specific cells to image:

See More

Product Page | Documentation | Examples | Temporary License | Forum |

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.