Conversion de Feuille de Calcul dans Différents Formats d Image

Conversion de la feuille de calcul en image

Parfois, il est utile de sauvegarder une image d’une feuille de calcul. Les images peuvent être partagées en ligne, insérées dans d’autres documents (rapports rédigés dans Microsoft Word, par exemple, ou présentations PowerPoint).

Aspose.Cells fournit l’exportation d’images via la classe SheetRender. Cette classe représente la feuille de calcul qui sera rendue sous forme d’image. La classe SheetRender fournit la méthode toImage() pour convertir une feuille de calcul en un fichier image. Les formats BMP, PNG, JPEG, TIFF et EMF sont pris en charge.

Le code ci-dessous montre comment convertir une feuille de calcul dans un fichier Microsoft Excel en un fichier PNG.

// 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(WorksheetToImage.class) + "LoadingSavingConvertingAndManaging/";
// Instantiate a new workbook with path to an Excel file
Workbook book = new Workbook(dataDir + "MyTestBook1.xlsx");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageType(ImageType.PNG);
// Get the first worksheet.
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
// Generate an image for the worksheet
sr.toImage(j, dataDir + "WToImage-out" + j + ".png");
}

Conversion de feuille de calcul en SVG

SVG signifie Scalable Vector Graphics. SVG est une spécification basée sur des normes XML pour les graphiques vectoriels en deux dimensions. Il s’agit d’une norme ouverte qui est en cours de développement par le World Wide Web Consortium (W3C) depuis 1999.

Depuis la version 7.1.0, Aspose.Cells for Java peut convertir des feuilles de calcul en images SVG.

Pour utiliser cette fonctionnalité, vous devez importer l’espace de noms com.aspose.cells dans votre programme ou projet. Il contient plusieurs classes précieuses pour le rendu et l’impression, par exemple, SheetRender, ImageOrPrintOptions, WorkbookRender, et d’autres.

La classe com.aspose.cells.ImageOrPrintOptions spécifie que la feuille de calcul sera enregistrée au format SVG.

La classe SheetRender prend comme paramètre l’objet de ImageOrPrintOptions qui définit le format d’enregistrement au format SVG.

Le code suivant montre comment convertir une feuille de calcul dans un fichier Excel en un fichier image SVG.

// 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(ConvertingWorksheetToSVG.class) + "loading_saving/";
String path = dataDir + "Book1.xlsx";
// Create a workbook object from the template file
Workbook workbook = new Workbook(path);
// Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setSaveFormat(SaveFormat.SVG);
imgOptions.setOnePagePerSheet(true);
// Convert each worksheet into svg format
int sheetCount = workbook.getWorksheets().getCount();
for (int i = 0; i < sheetCount; i++) {
Worksheet sheet = workbook.getWorksheets().get(i);
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int k = 0; k < sr.getPageCount(); k++) {
// Output the worksheet into Svg image format
sr.toImage(k, path + sheet.getName() + k + "_out.svg");
}
}
// Print message
System.out.println("Excel to SVG conversion completed successfully.");

Rendre la feuille de calcul active dans un classeur

Un moyen simple de convertir la feuille de calcul active dans un classeur est de définir l’index de la feuille active, puis d’enregistrer le classeur au format SVG. Cela rendra la feuille active au format SVG. Le code d’exemple suivant démontre cette fonctionnalité :

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String outputDir = Utils.Get_OutputDirectory();
// Instantiate a workbook
Workbook workbook = new Workbook();
// Put sample text in the first cell of first worksheet in the newly created workbook
workbook.getWorksheets().get(0).getCells().get("A1").setValue("DEMO TEXT ON SHEET1");
// Add second worksheet in the workbook
workbook.getWorksheets().add(SheetType.WORKSHEET);
// Set text in first cell of the second sheet
workbook.getWorksheets().get(1).getCells().get("A1").setValue("DEMO TEXT ON SHEET2");
// Set currently active sheet index to 1 i.e. Sheet2
workbook.getWorksheets().setActiveSheetIndex(1);
// Save workbook to SVG. It shall render the active sheet only to SVG
workbook.save(outputDir + "ConvertActiveWorksheetToSVG_out.svg");

Articles Connexes