Table of Contents
- C# Library to Convert Excel to Images
- Convert an Excel Worksheet to JPG, PNG and BMP Images
- Convert an Excel Worksheet to an EMF Image
- Convert an Excel Worksheet to an SVG Image
- Convert an Excel Worksheet to an Image with a Specific Resolution
- Convert an Excel Worksheet to an Image without White Margins
- Convert a Specific Cell Range of an Excel Worksheet to an Image
- Get a Free License
- Conclusion
- See Also
Installed via NuGet
PM> Install-Package Spire.XLS
Related Links
Sharing Excel files with others can sometimes be inconvenient due to various reasons. For instance, recipients may not have Excel installed on their devices, or they may be using a different operating system that is not compatible with Excel. This can lead to compatibility issues and difficulties in accessing and viewing the shared files. Furthermore, Excel files can be large in size, making them challenging to send via email or other file-sharing platforms with size restrictions.
Converting Excel to images addresses these challenges by providing a simplified and visually appealing way to present data. With images, you can capture the essence of the data in a single snapshot, making it easier for recipients to grasp the information at a glance. Whether it's a chart, graph, or table, converting Excel to images allows you to transform complex data into a visually engaging format.
In this blog, we will elaborate on how to convert Excel to various image formats such as JPG, PNG, BMP, EMF, and SVG using C#.
- Convert an Excel Worksheet to JPG, PNG and BMP Images in C#
- Convert an Excel Worksheet to an EMF Image in C#
- Convert an Excel Worksheet to an SVG Image in C#
- Convert an Excel Worksheet to an Image with a Specific Resolution in C#
- Convert an Excel Worksheet to an Image without White Margins in C#
- Convert a Specific Cell Range of an Excel Worksheet to an Image in C#
C# Library to Convert Excel to Images
To convert Excel to images using C#, you can use Spire.XLS for .NET, which is a professional Excel API that supports creating, reading, writing, printing, and converting Excel files in .NET applications without relying on Microsoft Office.
To install Spire.XLS for .NET, you can execute the following command in the NuGet Package Manager Console:
PM> Install-Package Spire.XLS
Convert an Excel Worksheet to JPG, PNG and BMP Images in C#
JPG, PNG, and BMP are popular image file formats used to store and display digital images. Here's a concise description of each format:
JPG (or JPEG):
JPG (or JPEG) is a widely used image format known for its efficient compression algorithm. It is commonly used for storing photographs and complex images with many colors.
PNG (Portable Network Graphics):
PNG is a lossless image format designed as a replacement for GIF (Graphics Interchange Format). It supports high-quality images with transparency and a wide range of colors. PNG files are often used for graphics, logos, and images with sharp edges or text.
BMP (Bitmap):
BMP is a basic image format that stores graphics in an uncompressed form. It was developed by Microsoft and is often used when precise pixel-level control is required or when compatibility with older software or systems is necessary.
Here is a code example that shows how to convert an Excel worksheet to JPG, PNG, and BMP images using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.Drawing.Imaging; namespace ConvertExcelToImage { internal class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Open an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Save the worksheet to JPEG, PNG, and BMP images worksheet.SaveToImage("Output//SheetToJPEG.jpeg", ImageFormat.Jpeg); worksheet.SaveToImage("Output//SheetToPNG.png", ImageFormat.Png); worksheet.SaveToImage("Output//SheetToBMP.bmp", ImageFormat.Bmp); workbook.Dispose(); } } }
Convert an Excel Worksheet to an EMF Image in C#
EMF is a vector-based image format that retains the scalability and high-quality resolution of the original worksheet. Converting Excel to EMF allows for further editing and manipulation of the image using compatible software, making it suitable for professional graphics and design purposes.
Here is a code example that shows how to convert an Excel worksheet to an EMF image using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace ConvertExcelToEMF { internal class Program { static void Main(string[] args) { //Create a Workbook object and load the Excel file Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Create a memory stream using (MemoryStream stream = new MemoryStream()) { //Save the worksheet to the memory stream as EMF worksheet.ToEMFStream(stream, worksheet.FirstRow, worksheet.FirstColumn, worksheet.LastRow, worksheet.LastColumn, EmfType.EmfPlusDual); //Create an Image object from the memory stream using (Image img = Image.FromStream(stream)) { //Save the image as an EMF file img.Save("Output//SheetToEMF.emf", ImageFormat.Emf); } } workbook.Dispose(); } } }
Convert an Excel Worksheet to an SVG Image in C#
SVG is an XML-based vector image format that supports interactivity and can be scaled without loss of quality. Converting Excel to SVG allows for dynamic and responsive rendering of the worksheet, making it ideal for web applications, data visualizations, and responsive design workflows.
Here is a code example that shows how to convert an Excel worksheet to an SVG image using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.IO; namespace ConvertExcelToSVG { internal class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Open an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Save the worksheet to SVG using (FileStream fs = new FileStream("Output//SheetToSVG.svg", FileMode.Create)) { worksheet.ToSVGStream(fs, worksheet.FirstRow, worksheet.FirstColumn, worksheet.LastRow, worksheet.LastColumn); } workbook.Dispose(); } } }
Convert an Excel Worksheet to an Image with a Specific Resolution in C#
When converting an Excel worksheet to an image, Spire.XLS for .NET does not offer a direct method to specify the image resolution. However, you can use the methods of the Bitmap and Graphics classes to adjust the resolution of the resulting images effortlessly.
Here is a code example that shows how to convert an Excel worksheet to an image with a specific resolution using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace ConvertExcelToImageWithCustomResolution { internal class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Open an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Convert the worksheet to EMF stream using (MemoryStream ms = new MemoryStream()) { worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn); //Create an image from the EMF stream Image image = Image.FromStream(ms); //Call the ResetResolution() method to change the resolution of the image Bitmap images = ResetResolution(image as Metafile, 300); //Save the image in JPG file format images.Save("Output//SheetToImageWithCustomResolution.jpg", ImageFormat.Jpeg); } workbook.Dispose(); } //Create a custom method to change image resolution private static Bitmap ResetResolution(Metafile mf, float resolution) { int width = (int)(mf.Width * resolution / mf.HorizontalResolution); int height = (int)(mf.Height * resolution / mf.VerticalResolution); Bitmap bmp = new Bitmap(width, height); bmp.SetResolution(resolution, resolution); Graphics g = Graphics.FromImage(bmp); g.DrawImage(mf, 0, 0); g.Dispose(); return bmp; } } }
Convert an Excel Worksheet to an Image without White Margins in C#
Converting an Excel worksheet to an image without white margins is useful for eliminating unnecessary white space and focusing on the content of the spreadsheet. By removing white margins, the resulting images will contain only the actual data and visuals from the Excel file, making them more visually appealing and suitable for certain use cases.
Here is a code example that shows how to convert an Excel worksheet to an image without white margins using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.Drawing.Imaging; namespace ConvertExcelToImage { internal class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Open an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Set the margin of the worksheet to 0 worksheet.PageSetup.LeftMargin = 0; worksheet.PageSetup.BottomMargin = 0; worksheet.PageSetup.TopMargin = 0; worksheet.PageSetup.RightMargin = 0; //Save the worksheet to an image worksheet.SaveToImage("Output//SheetToImageWithoutMargins.png", ImageFormat.Png); workbook.Dispose(); } } }
Convert a Specific Cell Range of an Excel Worksheet to an Image in C#
In addition to converting an entire worksheet to an image, Spire.XLS for .NET allows you to convert a specific cell range to an image. This can be useful for sharing or presenting a portion of data within a worksheet.
Here is a code example that shows how to convert a specific cell range of an Excel worksheet to an image using C# and Spire.XLS for .NET:
- Python
using Spire.Xls; using System.IO; namespace ConvertCellRangeToImage { internal class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Open an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Save a specific range of cells within the worksheet to an image worksheet.ToImage(5, 1, 16, 5).Save("Output//CellRangeToImage.png"); workbook.Dispose(); } } }
Get a Free License
To fully experience the capabilities of Spire.XLS for .NET without any evaluation limitations, you can request a free 30-day trial license.
Conclusion
This blog provides a comprehensive overview of how to convert Excel Worksheets to images using Spire.XLS for .NET. However, it's important to highlight that Spire.XLS supports converting Excel files to a variety of file formats beyond images. These additional conversion options include Excel to PDF, Excel to HTML, Excel to Text, Excel to XPS, and more.