Exportar rango de Cells en una hoja de trabajo a imagen
Contents
[
Hide
]
Puede crear una imagen de una hoja de trabajo usando Aspose.Cells. Sin embargo, a veces necesita exportar solo un rango de celdas en una hoja de trabajo a una imagen. Este artículo explica cómo lograr esto.
Para tomar una imagen de un rango, configure el área de impresión en el rango deseado y luego configure todos los márgenes en 0. Configure tambiénImageOrPrintOptions.setOnePagePerSheet() averdadero.
El siguiente código toma una imagen del rango E8:H10. A continuación se muestra una captura de pantalla del libro de trabajo de origen utilizado en el código. Puede probar el código con cualquier libro de trabajo.
Fichero de entrada
Ejecutar el código crea una imagen del rango E8: H10 solamente.
Imagen de salida
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(ExportRangeofCells.class) + "TechnicalArticles/"; | |
// Create workbook from source file. | |
Workbook workbook = new Workbook(dataDir + "book1.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Set the print area with your desired range | |
worksheet.getPageSetup().setPrintArea("E8:H10"); | |
// Set all margins as 0 | |
worksheet.getPageSetup().setLeftMargin(0); | |
worksheet.getPageSetup().setRightMargin(0); | |
worksheet.getPageSetup().setTopMargin(0); | |
worksheet.getPageSetup().setBottomMargin(0); | |
// Set OnePagePerSheet option as true | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.setOnePagePerSheet(true); | |
options.setImageType(ImageType.JPEG); | |
// Take the image of your worksheet | |
SheetRender sr = new SheetRender(worksheet, options); | |
sr.toImage(0, dataDir + "ERangeofCells_out.jpg"); |
También puede encontrar el artículoConversión de hoja de trabajo a diferentes formatos de imagen servicial.