ワークシート内のセルの範囲をイメージにエクスポート
Contents
[
Hide
]
Aspose.Cellsを使用してワークシートのイメージを作成できます。ただし、ワークシート内のセルの範囲をイメージにエクスポートする必要がある場合があります。この記事では、これをどのように行うかについて説明します。
範囲の画像を取得するには、印刷範囲を所望の範囲に設定し、すべての余白を0に設定し、ImageOrPrintOptions.setOnePagePerSheet() を true に設定します。
次のコードは、範囲 E8:H10 の画像を取得します。以下はコードで使用されるソースワークブックのスクリーンショットです。任意のワークブックでコードを試すことができます。
入力ファイル
コードを実行すると、範囲 E8:H10 の画像が作成されます。
出力画像
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"); |
この記事 ワークシートを異なる画像形式に変換 も参考になるでしょう。