Converting Worksheet to Image and Worksheet to Image by Page
This document is designed to provide the developers with a detailed understanding of how to convert a worksheet to an image file & worksheet with multiple pages to an image file per page.
Sometimes, you might need to present worksheets as images, for example, to use them in 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 to render the worksheet as an image. Aspose.Cells for Python via .NET supports converting worksheets in Microsoft Excel files to images. Also, Aspose.Cells for Python via .NET supports converting a workbook to multiple image files, one per page.
You might use Office Automation to achieve this, but Office automation has its own drawbacks. There are several reasons and issues involved: for example security, stability, scalability/Speed, price, and features. In short, there are many reasons, but the main one is that Microsoft themselves strongly recommends against Office automation.
Using Aspose.Cells to Convert Worksheet to Image File
This article shows how to create a console application in Visual Studio, convert a worksheet to an image, and convert a worksheet into one image for each worksheet with a few and simplest lines of code using the Aspose.Cells for Python via .NET API.
You need to import the aspose.cells.rendering namespace to your program/project. It has several valuable classes, such as SheetRender, ImageOrPrintOptions, WorkbookRender, and so on. The SheetRender class represents a worksheet to render images for the worksheet and has an overloaded to_image method that can convert a worksheet to image files directly with any attributes or options set. It can return a System.Drawing.Bitmap object and you can save an image file to the disk/stream. Several image formats are supported, for example, BMP, PNG, GIF, JPG, JPEG, TIFF, EMF, and others.
This article explains how to convert a worksheet to an image. This task shows how to use Aspose.Cells for Python via .NET to convert a worksheet from a template workbook to an image file.
Convert Worksheet to Image File
I created a new workbook in Microsoft Excel and added some data in the first worksheet: Testbook.xlsx (1 worksheet). Next, convert the template file’s worksheet Sheet1 to an image file called SheetImage.jpg.
Following is the code used by the component to accomplish the task. It converts Sheet1 in Testbook.xlsx to an image file to explain how easy this conversion is.
from aspose.cells import Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender | |
# 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 a template excel file | |
book = Workbook(sourceDir + "sampleConvertWorksheettoImageFile.xlsx") | |
# Get the first worksheet. | |
sheet = book.worksheets[0] | |
# Define ImageOrPrintOptions | |
imgOptions = ImageOrPrintOptions() | |
imgOptions.one_page_per_sheet = True | |
# Specify the image format | |
imgOptions.image_type = ImageType.JPEG | |
# Render the sheet with respect to specified image/print options | |
sr = SheetRender(sheet, imgOptions) | |
sr.to_image(0, outputDir + "outputConvertWorksheettoImageFile.jpg") |
Using Aspose.Cells to Convert Worksheet to Image File by Page
This example shows how to use Aspose.Cells for Python via .NET to convert a worksheet from a template workbook that has several pages to one image file per page.
Convert Worksheet to Image by page
I created a new workbook in Microsoft Excel and added some data in the first worksheet: Testbook2.xlsx (1 worksheet).
Now, convert the template file’s worksheet Sheet1 to image files (one file per page). As I already created the console application to perform the copy task, I will skip those console application creation steps and directly move to the worksheet conversion steps.
Following is the code used by the component to accomplish the task. It converts Sheet1 in Testbook2.xls to image files by page.
from aspose.cells import Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender | |
# 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() | |
book = Workbook(sourceDir + "sampleConvertWorksheetToImageByPage.xlsx") | |
sheet = book.worksheets[0] | |
options = ImageOrPrintOptions() | |
options.horizontal_resolution = 200 | |
options.vertical_resolution = 200 | |
options.image_type = ImageType.TIFF | |
# Sheet2Image By Page conversion | |
sr = SheetRender(sheet, options) | |
for j in range(sr.page_count): | |
sr.to_image(j, outputDir + "outputConvertWorksheetToImageByPage_" + str(j + 1) + ".tif") |