Renderice la hoja de trabajo y el libro de trabajo a la imagen usando ImageOrPrintOptions

Descripción general

veces, es posible que necesite presentar sus hojas de trabajo como una representación pictórica. Debe presentar las imágenes de la hoja de trabajo en sus aplicaciones o páginas web. Es posible que deba insertar las imágenes en un documento de Word, un archivo PDF, una presentación PowerPoint o usarlas en algún otro escenario. Simplemente desea una hoja de trabajo representada como una imagen para poder usarla en otro lugar. Aspose.Cells admite la conversión de hojas de trabajo en archivos de Excel a imágenes. Además, Aspose.Cells admite la configuración de diferentes opciones como formato de imagen, resolución (tanto vertical como horizontal), calidad de imagen y otras opciones de imagen e impresión.

El API proporciona varias clases valiosas, por ejemplo,HojaRenderizar, ImageOrPrintOptions, WorkbookRender, etc.

ÉlHojaRenderizar La clase se encarga de la tarea de renderizar imágenes para la hoja de trabajo, mientras que laWorkbookRenderhace lo mismo para un libro de trabajo. Ambas clases mencionadas tienen varias versiones sobrecargadas dela la imagenmétodo que puede convertir directamente una hoja de trabajo o un libro de trabajo en archivos de imagen especificados con los atributos u opciones deseados. Puede guardar el archivo de imagen en el disco/secuencia. Se admiten varios formatos de imagen, por ejemplo, BMP, PNG, GIFF, JPEG, TIFF, EMF, etc.

Convertir hoja de trabajo en imagen

// 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(ConvertWorksheettoImage.class) + "TechnicalArticles/";
//Instantiate a new Workbook object
//Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
//Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
//Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
//Set Horizontal Resolution
options.setHorizontalResolution(300);
//Set Vertical Resolution
options.setVerticalResolution(300);
//Set TiffCompression
options.setTiffCompression(TiffCompression.COMPRESSION_LZW);
//Set Image Format
options.setImageType(ImageType.TIFF);
//Set printing page type
options.setPrintingPage(PrintingPageType.DEFAULT);
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
//Render/save the image for the sheet
sr.toImage(0, dataDir + "CWorksheettoImage_out.tiff");

Opciones de conversión

Es posible guardar páginas específicas en la imagen. El siguiente código convierte la primera y la segunda hoja de trabajo de un libro de trabajo en imágenes JPG.

// 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(ConversionOptions.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
// Render/save the image for the sheet
sr.toImage(0, dataDir + "ConversionOptions_out.jpg");

O puede recorrer el libro de trabajo y representar cada hoja de trabajo en una imagen separada:

// 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(WorksheetToSeparateImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Iterate over all worksheets in the workbook
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
Worksheet sheet = book.getWorksheets().get(i);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
SheetRender sr = new SheetRender(sheet, options);
sr.toImage(0, dataDir + "WSheetToSImage_out-" + sheet.getName() + ".jpg");
}

Convertir libro de trabajo en imagen:

Para convertir el libro de trabajo completo en formato de imagen, puede usar el enfoque anterior o simplemente usar elWorkbookRender clase que acepta una instancia deLibro de trabajo así como el objeto deImageOrPrintOptions.

// 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(ConvertWorkbooktoImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Image Format
options.setImageType(ImageType.TIFF);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
WorkbookRender render = new WorkbookRender(book, options);
render.toImage(dataDir + "CWorkbooktoImage_out.tiff");

Artículos relacionados