Conversion d'une feuille de calcul en différents formats d'image

Conversion d’une feuille de calcul en image

Parfois, il est utile d’enregistrer 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 en Microsoft Word, par exemple, ou PowerPoint présentations).

Aspose.Cells fournit l’exportation d’images via le**SheetRender** classe. Cette classe représente la feuille de calcul qui sera rendue à une image. Le**SheetRender** la classe fournit la**toImage()**méthode 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 Excel Microsoft 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 la feuille de calcul en SVG

SVG signifieImage VectorielleSVG est une spécification basée sur les normes XML pour les graphiques vectoriels bidimensionnels. Il s’agit d’un standard ouvert développé par le World Wide Web Consortium (W3C) depuis 1999.

Depuis la sortie de la v7.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 a plusieurs classes utiles pour le rendu et l’impression, par exemple,SheetRender, ImageOrPrintOptions, WorkbookRender, et d’autres.

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

Le**SheetRender**la classe prend l’objet de**ImageOrPrintOptions** comme paramètre qui définit le format de sauvegarde au format SVG.

L’extrait de 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.");

Afficher la feuille de calcul active dans un classeur

Un moyen simple de convertir une feuille de calcul active dans un classeur consiste à définir l’index de la feuille active, puis à enregistrer le classeur sous SVG. Il rendra la feuille active sous SVG. L’exemple de code suivant illustre 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 Liés