Convert Excel to High-Quality JPG Using C#
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:
- Load the Excel file : Use the
Workbookclass to load the target worksheet - Convert to EMF stream : Export the specified range as an EMF memory stream
- Adjust resolution and save : Set the resolution to 300 DPI using the
ResetResolutionmethod, 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;
}
}
}
Key Code Explanation
- The
ToEMFStreammethod exports a specified worksheet range as an EMF (Enhanced Metafile) format, which is a vector format that preserves quality when scaled - The
ResetResolutionmethod takes aMetafileobject and a target resolution, returning a resizedBitmap - Using
MemoryStreamavoids 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.
评论
发表评论