How to convert entire Excel workbook into TIFF image?
Syncfusion® WinForms XlsIO supports converting an entire Excel workbook to a TIFF image with the help of MagickImage.NET, a third-party library that supports reading and writing multi-page TIFF images.
The following code snippet explains this.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
application.XlsIORenderer = new XlsIORenderer();
FileStream fileStream = new FileStream("../../../Data/ExportData.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(fileStream);
fileStream.Dispose();
var outputStream = new MemoryStream();
using (MagickImageCollection magicImageCollection = new MagickImageCollection())
{
foreach (IWorksheet sheet in workbook.Worksheets)
{
Stream stream = new MemoryStream();
IRange used = sheet.UsedRange;
ExportImageOptions exportImageOptions = new ExportImageOptions();
exportImageOptions.ScalingMode = ScalingMode.Normal;
exportImageOptions.ImageFormat = ExportImageFormat.Png;
sheet.ConvertToImage(used.Row, used.Column, used.LastRow, used.LastColumn, exportImageOptions, stream);
stream.Position = 0;
MagickImage magickImage = new MagickImage(stream);
magickImage = CompressImage(magickImage, 10);
magicImageCollection.Add(magickImage);
}
var outputData = magicImageCollection.ToByteArray(MagickFormat.Tif);
outputStream.Write(outputData, 0, outputData.Length);
}
File.WriteAllBytes("Output.tiff", outputStream.ToArray());
}
static MagickImage CompressImage(MagickImage image, int quality)
{
MagickImage newMagickImage= null;
using (MemoryStream memoryStream1 = new MemoryStream())
{
image.Quality = quality;
image.Write(memoryStream1, MagickFormat.Png);
memoryStream1.Position = 0;
ImageOptimizer imageOptimizer = new ImageOptimizer();
imageOptimizer.LosslessCompress(memoryStream1);
memoryStream1.Position = 0;
newMagickImage = new MagickImage(memoryStream1);
}
return newMagickImage;
}
A complete working sample can be downloaded from Excel-TIFF-Conversion.zip
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to convert entire Excel workbook into TIFF image.
You can refer to our WinForms XIsIo's feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms XIsIo documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms XIsIo and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!