ワークシートを異なる画像形式に変換

ワークシートをイメージに変換

時々、ワークシートの画像を保存することが役立ちます。画像はオンラインで共有したり、他のドキュメントに挿入したり(例えば、Microsoft Wordで書かれたレポートやPowerPointプレゼンテーションに)、利用できます。

Aspose.Cells は toImage() メソッドを提供する SheetRender クラスを介して画像のエクスポートを提供します。BMP、PNG、JPEG、TIFF、およびEMF形式がサポートされています。

以下のコードは、Microsoft Excelファイル内のワークシートをPNGファイルに変換する方法を示しています。

// 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(WorksheetToImage.class) + "LoadingSavingConvertingAndManaging/";
// Instantiate a new workbook with path to an Excel file
Workbook book = new Workbook(dataDir + "MyTestBook1.xlsx");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageType(ImageType.PNG);
// Get the first worksheet.
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
// Generate an image for the worksheet
sr.toImage(j, dataDir + "WToImage-out" + j + ".png");
}

ワークシートをSVGに変換

SVG は Scalable Vector Graphics の略です。SVG は2次元ベクターグラフィックスに関するXML標準に基づいた仕様です。1999年以来、世界中でW3C(World Wide Web Consortium)によって開発されているオープン標準です。

v7.1.0以降のリリースでは、Aspose.Cells for Java でワークシートをSVG画像に変換できます。

この機能を使用するには、プログラムまたはプロジェクトに com.aspose.cells ネームスペースをインポートする必要があります。描画および印刷には、例えば SheetRenderImageOrPrintOptionsWorkbookRenderなどの貴重なクラスがいくつか含まれています。

com.aspose.cells.ImageOrPrintOptions クラスはSVG形式で保存されることを指定します。

SheetRender クラスは SVG 形式への保存形式を設定する ImageOrPrintOptions オブジェクトをパラメータとして受け取ります。

次のコードスニペットは、Excelファイル内のワークシートをSVGイメージファイルに変換する方法を示しています。

// 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(ConvertingWorksheetToSVG.class) + "loading_saving/";
String path = dataDir + "Book1.xlsx";
// Create a workbook object from the template file
Workbook workbook = new Workbook(path);
// Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setSaveFormat(SaveFormat.SVG);
imgOptions.setOnePagePerSheet(true);
// Convert each worksheet into svg format
int sheetCount = workbook.getWorksheets().getCount();
for (int i = 0; i < sheetCount; i++) {
Worksheet sheet = workbook.getWorksheets().get(i);
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int k = 0; k < sr.getPageCount(); k++) {
// Output the worksheet into Svg image format
sr.toImage(k, path + sheet.getName() + k + "_out.svg");
}
}
// Print message
System.out.println("Excel to SVG conversion completed successfully.");

ブック内のアクティブなワークシートをレンダリング

ブック内のアクティブなワークシートを変換する簡単な方法は、アクティブシートのインデックスを設定し、その後ブックをSVGとして保存することです。アクティブシートはSVGにレンダリングされます。次のサンプルコードは、この機能を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String outputDir = Utils.Get_OutputDirectory();
// Instantiate a workbook
Workbook workbook = new Workbook();
// Put sample text in the first cell of first worksheet in the newly created workbook
workbook.getWorksheets().get(0).getCells().get("A1").setValue("DEMO TEXT ON SHEET1");
// Add second worksheet in the workbook
workbook.getWorksheets().add(SheetType.WORKSHEET);
// Set text in first cell of the second sheet
workbook.getWorksheets().get(1).getCells().get("A1").setValue("DEMO TEXT ON SHEET2");
// Set currently active sheet index to 1 i.e. Sheet2
workbook.getWorksheets().setActiveSheetIndex(1);
// Save workbook to SVG. It shall render the active sheet only to SVG
workbook.save(outputDir + "ConvertActiveWorksheetToSVG_out.svg");

関連記事