ワークシートを画像に変換し、ページごとにワークシートを画像に変換

Aspose.Cellsを使用してワークシートを画像ファイルに変換する方法

この記事では、Aspose.Cells for Java APIを使用してワークシートをイメージに変換する方法について説明します。APIは、SheetRenderImageOrPrintOptionsWorkbookRenderなど、いくつかの貴重なクラスを提供しています。SheetRender クラスは、ワークシートを描画し、そのワークシートのイメージを生成するためのtoImageメソッドをオーバーロードされたものを提供しています。

// 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(ConvertWorksheettoImageFile.class) + "TechnicalArticles/";
// Create a new Workbook object
// Open a template excel file
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Specify the image format
imgOptions.setImageType(ImageType.JPEG);
// Render the sheet with respect to specified image/print options
SheetRender render = new SheetRender(sheet, imgOptions);
// Render the image for the sheet
render.toImage(0, dataDir + "CWToImageFile.jpg");

結果

上記のコードを実行した後、Sheet1というワークシートがSheetImage.jpgという画像ファイルに変換されます。

生成されたJPG

todo:image_alt_text

Aspose.Cellsを使用して、ワークシートを画像ファイルにページごとに変換する

この例では、Aspose.Cellsを使用して、複数のページを持つテンプレートワークブックからワークシートを1つの画像ファイルに変換する方法を示します。

// 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(ConvertWorksheetToImageByPage.class) + "TechnicalArticles/";
// Create a new Workbook object
// Open a template excel file
Workbook book = new Workbook(dataDir + "ConvertWorksheetToImageByPage.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Define ImageOrPrintOptions
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Resolution
options.setHorizontalResolution(200);
options.setVerticalResolution(200);
options.setImageType(ImageType.TIFF);
// Sheet2Image by page conversion
SheetRender render = new SheetRender(sheet, options);
for (int j = 0; j < render.getPageCount(); j++) {
render.toImage(j, dataDir + sheet.getName() + " Page" + (j + 1) + ".tif");
}

結果

上記のコードを実行した後、Sheet1というワークシートが1ページごとに1つのイメージファイル(Sheet 1 Page 1.TiffおよびSheet 1 Page 2.Tiff)に変換されます。

生成されたイメージファイル(Sheet 1 Page 1.Tiff)

todo:image_alt_text

生成されたイメージファイル(Sheet 1 Page 2.Tiff)

todo:image_alt_text

関連記事