画像またはImageOrPrintOptionsを使用してワークシートおよびブックを画像にレンダリング
概要
時々、ワークシートを図解的に表示する必要があります。ワークシートの画像をアプリケーションやWebページに挿入する必要があります。画像をWord文書、PDFファイル、PowerPointプレゼンテーションに挿入したり、他のシナリオで使用する必要があります。別の場所で使用するためにワークシートを画像としてレンダリングしたいと思うだけです。Aspose.CellsはExcelファイルのワークシートを画像に変換することをサポートしています。また、Aspose.Cellsは画像形式、解像度(縦と横の両方)、画像品質、およびその他の画像および印刷オプションを設定することをサポートしています。
APIには、例えばSheetRender、ImageOrPrintOptions、WorkbookRenderなどの貴重なクラスがいくつかあります。
SheetRenderクラスは、ワークシートのイメージをレンダリングするタスクを処理し、WorkbookRenderクラスはワークブックのイメージを処理します。両前述のクラスには、ワークシートまたはワークブックをイメージファイルに直接変換するtoImageメソッドのオーバーロードされたバージョンがいくつかあり、指定した属性やオプションでイメージファイルを保存できます。BMP、PNG、GIFF、JPEG、TIFF、EMFなどのさまざまな画像形式がサポートされています。
ワークシートを画像に変換
// 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"); |
変換オプション
特定のページを画像に保存することが可能です。次のコードは、ワークブック内の最初と2番目のワークシートを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"); |
またはワークブックをサイクルして、それぞれのワークシートを別々の画像にレンダリングすることができます。
// 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"); | |
} |
ワークブックを画像に変換
完全なワークブックを画像形式にレンダリングするためには、上記の方法を使用するか、単純にWorkbookRenderクラスを使用し、WorkbookのインスタンスとImageOrPrintOptionsのオブジェクトを受け入れるようにします。
ホールワークブックを複数のフレームまたはページでTIFF画像単体で保存することができます。
// 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"); |