| Worksheets contain data that you want to analyze. For example, a worksheet can contain parameters, totals, percentages, exceptions, and calculations.
As a developer, you might need to present worksheets as images. For example, you might need to use an image of a worksheet in an application or web page. You might want to insert an image into a Word document, a PDF file, a PowerPoint presentation or some other document type. Simply put, you want a worksheet rendered as an image so that you can use it somewhere else. Aspose.Cells supports converting Excel worksheets to images. To use this feature, you need to import the Aspose.Cells.Rendering namespace to your program or project. It has several valuable classes for rendering and printing, for example SheetRender, ImageOrPrintOptions, WorkbookRender and others. The Aspose.Cells.Rendering.SheetRender class represents a worksheet to render as images. It has an overloaded metheod, ToImage, that can convert a worksheet to image file(s) with different attributes or options. It returns a System.Drawing.Bitmap object and you can save an image file to disk or stream. Several image formats are supported, for example BMP, PNG, GIF, JPG, JPEG, TIFF, EMF. |
Code Example
The following code snippet shows how to convert a worksheet in an Excel file to an image file.
//Create a new Workbook object and //open a template Excel file. Workbook book = new Workbook("d:\\test\\Book1.xls"); //Get the first worksheet. Worksheet sheet = book.Worksheets[0]; //Define ImageOrPrintOptions ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); //Specify the image format imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; //Only one page for the whole sheet would be rendered imgOptions.OnePagePerSheet = true; //Render the sheet with respect to specified image/print options SheetRender sr = new SheetRender(sheet, imgOptions); //Render the image for the sheet Bitmap bitmap = sr.ToImage(0); //Save the image file specifying its image format. bitmap.Save(@"d:\test\SheetImage.jpg");
'Create a new Workbook object and 'open a template Excel file. Dim book As New Workbook("d:\test\Book1.xls") 'Get the first worksheet. Dim sheet As Worksheet = book.Worksheets(0) 'Define ImageOrPrintOptions Dim imgOptions As New ImageOrPrintOptions() 'Specify the image format imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg 'Only one page for the whole sheet would be rendered imgOptions.OnePagePerSheet = True 'Render the sheet with respect to specified image/print options Dim sr As New SheetRender(sheet, imgOptions) 'Render the image for the sheet Dim bitmap As Bitmap = sr.ToImage(0) 'Save the image file specifying its image format. bitmap.Save("d:\test\SheetImage.jpg")
Additional Resources
You may find the Converting Worksheet to Image & Worksheet to Image by Page and Converting Worksheet to Image using ImageOrPrint Options articles useful for more information.
