JavaでExcelシートを画像に変換する方法

このクイックチュートリアルでは、ExcelシートをJavaで画像に変換する方法について説明します。これには、環境を構成するためのガイダンスと、XLSXからJPEGへのシートの変換を示す実行可能なサンプルコードとともに詳細な手順が含まれています。 ** Java Excelからイメージ**への変換は、システムにインストールされている他のサードパーティのツールやソフトウェアを使用せずに、数行のコードで実行できます。

JavaでExcelシートを画像に変換する手順

  1. MavenリポジトリからAspose.Cellsへの参照を追加して、シートを画像に変換します
  2. Workbookクラスインスタンスを使用して画像に変換するためにソースExcelファイルをロードします
  3. ImageOrPrintOptionsクラスオブジェクトをインスタンス化して、出力画像をカスタマイズします
  4. セルの内容と画像タイプに基づいて列を自動調整するようにフラグを設定します
  5. 画像にレンダリングするシートを選択します
  6. 構成されたImageOrPrintOptions設定を使用して、選択したシートのSheetRenderクラスオブジェクトを作成します
  7. 印刷プレビューのすべてのページを解析し、各ページを画像としてレンダリングします

これらの手順では、特定のワークシートの各ページが個別の画像にレンダリングされるように、ExcelファイルをJavaで画像に変換するために必要な操作の順序について説明します。すべてのワークシートを1つずつ解析して、ワークブック全体を画像に変換できます。ソースExcelファイルを読み込んだ後、ImageOrPrintOptionsクラスオブジェクトを使用して、画像タイプの設定などの出力画像を構成したり、列を自動調整して各セルのコンテンツ全体を表示したりして、シートのレンダリング中にこの構成を使用できます。 SheetRenderクラスオブジェクト。

JavaでExcelワークシート画像を作成するためのコード

import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.ImageType;
import com.aspose.cells.License;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class ConvertExcelSheetToImageInJava {
public static void main(String[] args) throws Exception {//main function to convert Excel sheet to image
// Instantiate the license to avoid trial version watermark in the output images
License licenseForExcelToImage = new License();
licenseForExcelToImage.setLicense("Aspose.Cells.lic");
// Load the Excel file required to be converted to images
Workbook bookToImages = new Workbook("MyTestBook1.xlsx");
// Create an instance of ImageOrPrintOptions to customize the output images
ImageOrPrintOptions exportedImgOptions = new ImageOrPrintOptions();
// Set the flag to auto-fit column width of each cell according to the size of contents
exportedImgOptions.setCellAutoFit(true);
// Set the image type to JPEG exported from the Excel worksheet
exportedImgOptions.setImageType(ImageType.JPEG);
// Select the sheet from the collection that is to be rendered to images
Worksheet sheetToImage = bookToImages.getWorksheets().get(0);
// Create and initialize an instance of SheetRender with target sheet and image configurations
SheetRender sheetRenderToImage = new SheetRender(sheetToImage, exportedImgOptions);
// Parse through all the pages in sheet to render as image
for (int j = 0; j < sheetRenderToImage.getPageCount(); j++)
{
// Save each image to file generated by the SheetRender class object
sheetRenderToImage.toImage(j, "ToImage-out" + j + ".jpg");
}
System.out.println("Done");
}
}

ImageOrPrintOptionsクラスオブジェクトは、setAllColumnsInOnePagePerSheet(true)を使用してすべての列を1ページに表示したり、setDefaultFont(fontName)を使用してExcelファイルの文字がUnicodeの場合にフォントを設定したりするなど、他の多くのプロパティを含む出力画像を構成するために使用されます。setHorizontalResolution ()およびsetVerticalResolution()を使用して画像の解像度を設定し、setTextCrossType()を使用してテキストの長さがセル幅を超える場合のスタイルを定義します。同様に、Javaで* Excelから画像へのコンバーター*を記述している場合、メソッドsetDesiredSize()を使用して、パラメーターとして幅と高さを必要とする出力画像のサイズを設定できます。

JavaExcelを使用して画像変換を実行する方法を学習しました。 ExcelからHTMLへの変換について知りたい場合は、JavaでExcelをHTMLに変換する方法の記事を参照してください。

 日本語