ImageOrPrintOptions を使用してワークシートとワークブックをイメージにレンダリングする
概要
場合によっては、ワークシートを絵で表したものとして提示する必要がある場合があります。ワークシート イメージをアプリケーションまたは Web ページに表示する必要があります。画像を Word 文書、PDF ファイル、PowerPoint プレゼンテーションに挿入したり、他のシナリオで使用したりする必要がある場合があります。他の場所で使用できるように、ワークシートを画像としてレンダリングしたいだけです。 Aspose.Cells は、Excel ファイルのワークシートを画像に変換することをサポートしています。また、Aspose.Cells は、画像形式、解像度 (縦と横の両方)、画質、その他の画像および印刷オプションなど、さまざまなオプションの設定をサポートしています。
API は、いくつかの価値のあるクラスを提供します。たとえば、シートレンダリング, ImageOrPrintOptions, WorkbookRenderなど
のシートレンダリングクラスは、ワークシートの画像をレンダリングするタスクを処理しますが、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"); |
変換オプション
特定のページを画像として保存することができます。次のコードは、ワークブックの 1 番目と 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のインスタンスを受け入れるクラスワークブックの対象だけでなく、ImageOrPrintOptions.
// 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"); |