Rendre la feuille de calcul dans le contexte graphique
Aspose.Cells peut désormais rendre la feuille de calcul dans un contexte graphique. Le contexte graphique peut être quelque chose comme un fichier image, un écran ou une imprimante, etc. Veuillez utiliser la méthode suivante pour rendre la feuille de calcul dans le contexte graphique.
- SheetRender.toImage(int pageIndex, graphique Graphics2D)
Rendre la feuille de calcul dans le contexte graphique
Le code suivant illustre comment utiliser Aspose.Cells pour afficher la feuille de calcul dans un contexte graphique. Une fois que vous aurez exécuté un code, il imprimera toute la feuille de calcul et remplira l’espace vide restant avec une couleur bleue dans le contexte graphique et enregistrera l’image soustest.png dossier. Vous pouvez utiliser n’importe quel fichier Excel source pour essayer ce code. Veuillez également lire les commentaires à l’intérieur du code pour une meilleure compréhension.
// 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); |