DEV Community

jelizaveta
jelizaveta

Posted on

Convert Excel to High-Quality JPG Using C#

In everyday office development, there is often a need to convert Excel spreadsheets into images. Whether for report previews, data presentation, or preventing formatting issues, converting Excel to JPG is a practical solution. Today, we’ll show how to use the Spire.XLS library with C# to achieve high-quality Excel-to-JPG conversion.

Why High-Quality Conversion Matters

Taking screenshots or using basic conversion methods often results in blurry images and unclear text. This becomes especially problematic when printing or zooming in, as low-resolution images cannot meet quality requirements. By setting the resolution to 300 DPI, you can ensure the generated JPG images reach print-level clarity.

Implementation Steps

First, install the Spire.XLS library. You can search for Spire.XLS in the NuGet Package Manager and install it.

The core process consists of three parts:

  1. Load the Excel file : Use the Workbook class to load the target worksheet
  2. Convert to EMF stream : Export the specified range as an EMF memory stream
  3. Adjust resolution and save : Set the resolution to 300 DPI using the ResetResolution method, then save as JPG

Complete Code

using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Convert
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx", ExcelVersion.Version2013);
            Worksheet worksheet = workbook.Worksheets[0];

            using (MemoryStream ms = new MemoryStream())
            {
                worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);
                Image image = Image.FromStream(ms);
                Bitmap images = ResetResolution(image as Metafile, 300);
                images.Save("Result.jpg", ImageFormat.Jpeg);
            }
        }

        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;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Code Explanation

  • The ToEMFStream method exports a specified worksheet range as an EMF (Enhanced Metafile) format, which is a vector format that preserves quality when scaled
  • The ResetResolution method takes a Metafile object and a target resolution, returning a resized Bitmap
  • Using MemoryStream avoids creating temporary files and allows the entire process to run in memory

Use Cases

  • Reporting systems : Convert data tables into images for embedding in Word, PowerPoint, or web pages
  • Data presentation : Ensure accessibility even when users don’t have Excel installed
  • Archiving and backup : Save important spreadsheets as images for long-term preservation without format changes

With this approach, you can easily convert Excel spreadsheets into high-resolution JPG images suitable for most office scenarios. If you need batch conversion, simply iterate through multiple worksheets in the workbook.

Top comments (0)