How To Convert An Excel Worksheet To A High Quality Image

This blog offers a solution for saving an Excel worksheet as an image with high resolution in a .NET application or web page.

Converting an Excel file into an image is one of the main features offered by Free Spire.XLS, which supports converting Excel files into PDF, XPS, SVG, HTML, and XML etc.

I will introduce my whole solution of converting Excel to an image in two parts. The first part describes how to save Excel to an image with default image quality. The second part shows how to convert Excel to a high-quality image by setting the image resolution. Then, you will find that the resulting image becomes very clear and the quality is very high. Here we go!

Below is the source code for converting the Excel worksheet to an image. Most image formats are supported, such as BMP, PNG, GIF, JPG, JPEG, TIFF and I will use JPG for example. It is very simple and I only need four lines of code.

  1. Workbook workbook = newWorkbook();  
  2. workbook.LoadFromFile(@"..\..\SheetToImage.xlsx");  
  3. Worksheet worksheet = workbook.Worksheets[0];  
  4. worksheet.SaveToImage("sample.jpg", ImageFormat.Jpeg);  

How To Convert Excel To Image With High Quality

The properties of this image are given below.

How To Convert Excel To Image With High Quality

Generally speaking, it meets most of the requirements but when we need a high quality image, the above performance is poor. So now, it comes to the second part of converting Excel to a high quality image by setting the image resolution, when saving Excel as an image.

  1. private void button1_Click(object sender, EventArgs e) {  
  2.     Workbook workbook = newWorkbook();  
  3.     workbook.LoadFromFile(@ "..\..\SheetToImage.xlsx");  
  4.     Worksheet worksheet = workbook.Worksheets[0];  
  5.     //convert the worksheet to EMF stream  
  6.     using(MemoryStream ms = newMemoryStream()) {  
  7.         worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);  
  8.         //create an image from the EMF stream  
  9.         Image image = Image.FromStream(ms);  
  10.         //call ResetResolution to reset the resolution for the image.  
  11.         Bitmap images = ResetResolution(image asMetafile, 300);  
  12.         images.Save("Result.jpg", ImageFormat.Jpeg);  
  13.     }  
  14.     this.Close();  
  15. }  
  16. private static Bitmap Reset Resolution(Metafile mf, float resolution) {  
  17.     int width = (int)(mf.Width * resolution / mf.HorizontalResolution);  
  18.     int height = (int)(mf.Height * resolution / mf.VerticalResolution);  
  19.     Bitmap bmp = newBitmap(width, height);  
  20.     bmp.SetResolution(resolution, resolution);  
  21.     Graphics g = Graphics.FromImage(bmp);  
  22.     g.DrawImage(mf, 0, 0);  
  23.     g.Dispose();  
  24.     return bmp;  
  25. }   

The properties of this image are mentioned below.

From the resulting page and comparing the differences between them, I hope you have a clear idea of how to save Excel as an image in C#.