Konvertera arbetsblad till olika bildformat
Konvertera arbetsblad till bild
Ibland är det användbart att spara en bild av ett kalkylblad. Bilder kan delas online, infogas i andra dokument (rapporter skrivna i Microsoft Word, till exempel, eller PowerPoint presentationer).
Aspose.Cells tillhandahåller bildexport via**SheetRender** klass. Den här klassen representerar kalkylbladet som kommer att renderas till en bild. De**SheetRender** klass ger**toImage()**metod för att konvertera ett kalkylblad till en bildfil. Formaten BMP, PNG, JPEG, TIFF och EMF stöds.
Koden nedan visar hur man konverterar ett kalkylblad i en Microsoft Excel-fil till en PNG-fil.
// 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"); | |
} |
Konvertera arbetsblad till SVG
SVG står förSkalbar vektorgrafikSVG är en specifikation baserad på XML-standarder för tvådimensionell vektorgrafik. Det är en öppen standard som har varit under utveckling av World Wide Web Consortium (W3C) sedan 1999.
Sedan versionen av v7.1.0,Aspose.Cells for Java kan konvertera kalkylblad till SVG bilder.
För att använda den här funktionen måste du importera namnutrymmet com.aspose.cells till ditt program eller projekt. Den har flera värdefulla klasser för rendering och utskrift, till exempel,SheetRender, ImageOrPrintOptions, WorkbookRender, och andra.
De**com.aspose.cells.ImageOrPrintOptions** class anger att kalkylbladet kommer att sparas i formatet SVG.
De**SheetRender**klass tar föremålet för**ImageOrPrintOptions** som en parameter som ställer in spara-formatet till SVG-formatet.
Följande kodavsnitt visar hur man konverterar ett kalkylblad i en Excel-fil till en SVG-bildfil.
// 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."); |
Gör aktivt kalkylblad i en arbetsbok
Ett enkelt sätt att konvertera aktivt kalkylblad till en arbetsbok är att ställa in det aktiva arbetsbladets index och sedan spara arbetsboken som SVG. Det återger det aktiva bladet till SVG. Följande exempelkod visar denna funktion:
// 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"); |