将工作表呈现到图形上下文
Contents
[
Hide
]
Aspose.Cells现在可以将工作表渲染到图形上下文。图形上下文可以是图像文件、屏幕或打印机等。请使用以下方法将工作表渲染到图形上下文。
- SheetRender.toImage(int pageIndex, Graphics2D graphic)
将工作表渲染到图形上下文
以下代码演示了如何使用Aspose.Cells将工作表渲染到图形上下文。执行代码后,将打印整个工作表,并将剩余的空白区域填充为蓝色,并保存图像为test.png文件。您可以尝试此代码使用任何源Excel文件。还请阅读代码内的评论以便更好地理解。
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.getDataDir(ReleaseUnmanagedResources.class); | |
// Create workbook object from source file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create empty image and fill it with blue color | |
int width = 800; | |
int height = 800; | |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g = image.createGraphics(); | |
g.setColor(java.awt.Color.blue); | |
g.fillRect(0, 0, width, height); | |
// Set one page per sheet to true in image or print options | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.setOnePagePerSheet(true); | |
// Render worksheet to graphics context | |
SheetRender sr = new SheetRender(worksheet, opts); | |
sr.toImage(0, g); | |
// Save the graphics context image in Png format | |
File outputfile = new File(dataDir + "test.png"); | |
ImageIO.write(image, "png", outputfile); |