在将数据呈现为图像之前从数据中删除空白格
Contents
[
Hide
]
有时候,您需要在应用程序或网页中呈现工作表图像。例如,您可能需要将图像插入到 Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档中。基本上,您想要将工作表呈现为图像,以便将其粘贴到其他应用程序中。Aspose.Cells API 允许您将 Microsoft Excel 工作表转换为图像。
SheetRender 类能够将工作表转换为带有任何指定属性的图像文件,例如,图像格式、分页工作表等。支持多种图像格式,包括 BMP、GIF、JPG、TIFF 和 EMF。
在使用工作表转图像功能时,默认情况下,输出图像周围会有白色/空白空间,即边框。您可以移除此空白空间。设置源工作表的顶部、左侧、底部和右侧页面设置边距为 0 并相应地指定 ImageOrPrintOptions 属性。
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(RemoveWhitespaceAroundData.class) + "TechnicalArticles/"; | |
// Instantiate a workbook | |
// Open the template file | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Specify your print area if you want | |
// sheet.PageSetup.PrintArea = "A1:H8"; | |
// To remove the white border around the image. | |
sheet.getPageSetup().setLeftMargin(0); | |
sheet.getPageSetup().setRightMargin(0); | |
sheet.getPageSetup().setTopMargin(0); | |
sheet.getPageSetup().setBottomMargin(0); | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.setImageType(ImageType.EMF); | |
// Set only one page would be rendered for the image | |
imgOptions.setOnePagePerSheet(true); | |
imgOptions.setPrintingPage(PrintingPageType.IGNORE_BLANK); | |
// Create the SheetRender object based on the sheet with its | |
// ImageOrPrintOptions attributes | |
SheetRender render = new SheetRender(sheet, imgOptions); | |
// Convert the image | |
render.toImage(0, dataDir + "RWhitespaceAroundData_out.emf"); |