Converting Worksheet to Image using ImageOrPrint Options

Saving Worksheets to Images - Different Approaches

Sometimes, you might require presenting your worksheets as a pictorial representation. You do need to present the worksheet images into your applications or web pages. You might need to insert the images into a Word document, a PDF file, a PowerPoint presentation, or use them in some other scenario. Simply you want a worksheet rendered as an image so that you can use it elsewhere. Aspose.Cells for Python via .NET supports converting worksheets in Excel files to images. Also, Aspose.Cells for Python via .NET supports setting different options like image format, resolution (both vertical and horizontal), image quality, and other image and print options.

You might try Office Automation but Office automation has its own drawbacks. There are several reasons and issues involved: for example, security, stability, scalability and speed, price, and features. In Short, there are many reasons, with the top one being that Microsoft themselves strongly recommends against Office automation from software solutions.

This article shows how to create a console application in Visual Studio .NET, perform the conversion of a worksheet to image using different image and print options with a few and simplest lines of code using Aspose.Cells for Python via .NET API.

You need to import aspose.cells.rendering namespace to your program/project. It has several valuable classes, for example, SheetRender, ImageOrPrintOptions, WorkbookRender etc.

The SheetRender class represents a worksheet to render images for the worksheet, it has an overloaded to_image method that can directly convert a worksheet to image file(s) specified with your desired attributes or options. It can return System.Drawing.Bitmap object and you can Save an image file to the disk/stream. There are several image formats supported, e.g BMP, PNG, GIFF, JPEG, TIFF, EMF and so on.

Using Aspose.Cells to Convert Worksheet to Image using ImageOrPrint options

Creating a template workbook in Microsoft Excel

I created a new workbook in MS Excel and added some data in the first worksheet. Now, I will convert the template file’s worksheet “Sheet1” to an image file “SheetImage.tiff” and will apply different image options like horizontal and vertical resolutions, TiffCompression etc.

Convert Worksheet to an Image file

from aspose.cells import PrintingPageType, Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender, TiffCompression
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Open template
book = Workbook(sourceDir + "sampleWorksheetToAnImage.xlsx")
# Get the first worksheet
sheet = book.worksheets[0]
# Apply different Image and Print options
options = ImageOrPrintOptions()
# Set Horizontal Resolution
options.horizontal_resolution = 300
# Set Vertical Resolution
options.vertical_resolution = 300
# Set TiffCompression
options.tiff_compression = TiffCompression.COMPRESSION_LZW
# Set Image Format
options.image_type = ImageType.TIFF
# Set printing page type
options.printing_page = PrintingPageType.DEFAULT
# Render the sheet with respect to specified image/print options
sr = SheetRender(sheet, options)
# Render/save the image for the sheet
pageIndex = 3
sr.to_image(pageIndex, outputDir + r"outputWorksheetToAnImage_" + str(pageIndex + 1) + ".tiff")

Image conversion using WorkbookRender

A TIFF image can contian more than one frames. You can save the whole workbook to a single TIFF image with multiply frames or pages:

from aspose.cells import Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, WorkbookRender
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
wb = Workbook(sourceDir + "sampleUseWorkbookRenderForImageConversion.xlsx")
opts = ImageOrPrintOptions()
opts.image_type = ImageType.TIFF
wr = WorkbookRender(wb, opts)
wr.to_image(outputDir + "outputUseWorkbookRenderForImageConversion.tiff")