将工作表转换为图像 删除数据周围的空白
Contents
[
Hide
]
有时,需要在应用程序或网页中显示工作表图像。例如,将图像插入 Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档。基本上,你希望将工作表渲染成图像,便于粘贴到其他应用中。Aspose.Cells for Python via .NET 允许你将 Microsoft Excel 工作表转换为图像。
删除数据周围的空白
SheetRender API 可将工作表转换为带有任何指定属性(例如图像格式、分页工作表等)的图像文件。支持多种图像格式,包括 BMP、GIF、JPG、TIFF 和 EMF。
使用工作表转图像功能时,默认情况下输出图像具有周围的空白(即边框)。您可以通过将源工作表的顶部、底部、左侧和右侧的页面设置边距设置为 0,并相应地指定 ImageOrPrintOptions 属性来删除这些空白。
以下代码片段删除输出图像中的数据周围的空白。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import LoadDataFilterOptions, LoadFilter, LoadOptions, PrintingPageType, 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 the template file | |
book = Workbook(sourceDir + "Book1.xlsx") | |
# Get the first worksheet | |
sheet = book.worksheets[0] | |
options = LoadOptions() | |
options.load_filter = LoadFilter(LoadDataFilterOptions.ALL) | |
# Specify your print area if you want | |
# Sheet.PageSetup.PrintArea = "A1:H8"; | |
# To remove the white border around the image. | |
sheet.page_setup.left_margin = 0.0 | |
sheet.page_setup.right_margin = 0.0 | |
sheet.page_setup.bottom_margin = 0.0 | |
sheet.page_setup.top_margin = 0.0 | |
# Define ImageOrPrintOptions | |
imgOptions = ImageOrPrintOptions() | |
imgOptions.image_type = ImageType.EMF | |
# Set only one page would be rendered for the image | |
imgOptions.one_page_per_sheet = True | |
imgOptions.printing_page = PrintingPageType.IGNORE_BLANK | |
# Create the SheetRender object based on the sheet with its | |
# ImageOrPrintOptions attributes | |
sr = SheetRender(sheet, imgOptions) | |
# Convert the image | |
sr.to_image(0, outputDir + "outputRemoveWhitespaceAroundData.emf") |