将工作表转换为图像 删除数据周围的空白

删除数据周围的空白

SheetRender API 可将工作表转换为带有任何指定属性(例如图像格式、分页工作表等)的图像文件。支持多种图像格式,包括 BMP、GIF、JPG、TIFF 和 EMF。

使用工作表转图像功能时,默认情况下输出图像具有周围的空白(即边框)。您可以通过将源工作表的顶部、底部、左侧和右侧的页面设置边距设置为 0,并相应地指定 ImageOrPrintOptions 属性来删除这些空白。

以下代码片段删除输出图像中的数据周围的空白。

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")