異なるフォーマットにワークブックを変換する

ExcelをXPSに変換

XPS文書形式は、文書のレイアウト、各ページの視覚的な外観、ページの配布、アーカイブ化、レンダリング、処理、および印刷に関するレンダリング規則を定義する構造化されたXMLマークアップから構成されています。

XPSのマークアップ言語は、XPSでベクターグラフィック要素を組み込むことを可能にするXAMLのサブセットであり、Windows Presentation Foundation(WPF)プリミティブをマークアップするためにXAMLを使用します。使用される要素はパスおよびその他の幾何学的プリミティブとして記述されます。

XPSファイルは、Open Packaging Conventionsを使用したUnicode ZIPアーカイブで実際には構成されており、これにはドキュメントを構成するファイルが含まれています。これには各ページのXMLマークアップファイル、テキスト、埋め込みフォント、ラスタイメージ、2Dベクターグラフィックなどが含まれます。また、デジタル著作権管理情報も含まれます。XPSファイルの内容は、ZIPファイルをサポートするアプリケーションで開くことで簡単に確認できます。

Aspose.Cells 6.0.0から、 Microsoft ExcelからXPSへの変換がサポートされています。

単一のワークシートをXPSに変換する

次の例は、ExcelファイルのワークシートをXPSに変換する方法を示しています。

// 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(ConvertingsingleWorksheetToXPS.class) + "loading_saving/";
//Open an Excel file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
//Set the format
options.setSaveFormat(SaveFormat.XPS);
//Render the sheet with respect to specified printing options
SheetRender render = new SheetRender(sheet, options);
render.toImage(0, dataDir + "CSingleWorksheetToXPS_out.xps");

XPSへのワークブック全体のエクスポート

次の例は、ワークブック全体をXPS形式に変換する方法を示しています。

// 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(ExportWholeWorkbookToXPS.class) + "loading_saving/";
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set the format
options.setSaveFormat(SaveFormat.XPS);
// Render the workbook with respect to specified printing options
WorkbookRender render = new WorkbookRender(workbook, options);
render.toImage(dataDir + "ExportWholeWorkbookToXPS_out.xps");

クイックExcelからXPSへの変換

次の例は、Excelファイルを直接XPS形式に変換する簡単な方法を示しています。

// 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(ExportWholeWorkbookToXPS.class) + "loading_saving/";
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Save in XPS format
workbook.save("QEToXPSConversion_out.xps", SaveFormat.XPS);

ExcelをMHTMLファイルに変換する

MHTMLは通常HTMLと外部リソース(画像、アニメーション、オーディオなど)を1つのファイルに結合します。.mhtファイル拡張子を持つメールで使用されます。

スプレッドシートをMHTMLに変換することは、以下に示すように迅速な操作です。

以下のコード例は、ワークブックをMHTMLファイルとして保存する方法を示しています。

// 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(ConvertingToMHTMLFiles.class) + "loading_saving/";
// Specify the file path
String filePath = dataDir + "Book1.xlsx";
// Specify the HTML saving options
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.M_HTML);
// Instantiate a workbook and open the template XLSX file
Workbook wb = new Workbook(filePath);
// Save the MHT file
wb.save(dataDir + "CToMHTMLFiles_out.mht", sv);
// Print message
System.out.println("Excel to MHTML conversion performed successfully.");

ExcelファイルをHTMLに変換する

Aspose.Cells APIは、スプレッドシートをHTML形式にエクスポートするためのサポートを提供します。この目的において、Aspose.CellsはHtmlSaveOptionsクラスを使用し、出力HTMLの複数の側面を制御することができます。

以下のコードは、追加パラメータを指定せずにMicrosoft ExcelファイルをHTML形式にエクスポートするためにHtmlSaveOptionsクラスを使用する方法を示しています。

// 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(ConvertingToHTMLFiles.class) + "loading_saving/";
// Specify the file path
String filePath = dataDir + "Book1.xlsx";
// Specify the HTML saving options
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.HTML);
// Instantiate a workbook and open the template XLSX file
Workbook wb = new Workbook(filePath);
// Save the HTML file
wb.save(dataDir + "CToHTMLFiles_out.html", sv);
// Print message
System.out.println("Excel to HTML conversion performed successfully.");

HTMLの画像設定

Aspose.Cells 8.0.2から、スプレッドシートをHTML形式で保存する際に画像の設定を指定するためのHtmlSaveOptionsクラス用にImageOptionsが公開されています。

適用可能な画像設定は次の通りです:

  • ImageType: 画像タイプを取得または設定します。すべての形状(チャートを含む)は、出力HTMLで画像としてレンダリングされます。
  • Quality: JpegとしてImageFormatが指定されている場合、画像の品質を0~100で取得または設定します。
  • VerticalResolution: ImageFormatが指定されている場合の画像の垂直方向解像度を取得または設定します。
  • HorizontalResolution: ImageFormatが指定されている場合の画像の水平方向解像度を取得または設定します。
  • TiffCompression: TiffとしてImageFormatが指定されている場合の画像の圧縮タイプを取得または設定します。
  • Transparent: PngとしてImageFormatが指定されている場合、画像の背景を透明にするかどうかを示します。

以下のコードは、異なる設定を指定するためにHtmlSaveOptions.ImageOptionsを使用する方法を示しています。

エクスポート前のスプレッドシート表示 エクスポート後のHTML表示
エクスポート前のスプレッドシート表示 エクスポート後のHTML表示
// 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(SettingImagePrefrencesforHTML.class) + "loading_saving/";
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
System.out.println("Set PDF Creation Time successfully.");

ExcelをPDFファイルに変換する

PDF文書は、組織、政府部門、個人間で文書を交換する標準的な形式として広く使用されています。ソフトウェア開発者は、Microsoft Excelファイルを簡単にPDF文書に変換する方法を考案するように求められることがあります。Aspose.Cellsはこれらの機能をサポートしています。この記事では、その方法を示しています。

ExcelをPDFに変換する

Microsoft ExcelからPDFへの変換はAspose.Cells for Java 2.3.0で導入されました。そのリリースから、Aspose.Cellsは直接スプレッドシートをPDFに変換PDF/Aを含む)することができます。以前のバージョンのAspose.Cellsでスプレッドシートを変換するには、[Aspose.PDFを使用します](#asposepdf-asposecells 230より前のバージョンでの変換)。

Aspose.Cellsは高い精度と忠実度でスプレッドシートをPDFに変換します。ただし、この記事の最後に制限事項がいくつかリストされています。

直接変換

最も効率的な変換方法である、Workbook.saveメソッドを使用してExcelファイルを直接PDFに保存し、SaveFormat.PDFインターフェースメンバーを提供します。これにより、データや書式を失うことなく、出力されるPDFが入力のExcelファイルに似た外観になります。

PDFに保存する際にセキュリティオプションを指定するには、PdfSaveOptions を使用してください。

// 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(Excel2PDFConversion.class) + "loading_saving/";
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Save the document in PDF format
workbook.save(dataDir + "E2PDFC-out.pdf", SaveFormat.PDF);
// Print message
System.out.println("Excel to PDF conversion performed successfully.");

高度な変換

変換の別の属性を設定するためにPdfSaveOptionsクラスを使用することも選択できます。PdfSaveOptionsクラスの異なるプロパティを設定すると、出力されるPDFファイルの印刷、フォント、セキュリティ、圧縮設定をコントロールできます。最も注目すべきプロパティはComplianceで、ExcelファイルをPDF/Aに準拠したPDFファイルに保存できるようにします。

ExcelスプレッドシートをPDF/A適合ファイルに保存

次に示すコードスニペットは、PdfSaveOptionsクラスを使用してExcelファイルをPDF/Aに準拠したPDF形式で保存する方法を示しています。

// 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(AdvancedConversiontoPdf.class) + "loading_saving/";
// Create a new Workbook.
Workbook workbook = new Workbook();
Cells cell = workbook.getWorksheets().get(0).getCells();
cell.get("A12").setValue("Test PDF");
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setCompliance(PdfCompliance.PDF_A_1_B);
workbook.save(dataDir + "ACToPdf_out.pdf", pdfOptions);
// Print message
System.out.println("Advanced Conversion performed successfully.");

Aspose.Pdfを使用した変換:Aspose.Cells 2.3.0以前

Aspose.Cellsバージョン2.3.0より前のバージョンでは、Aspose.PDF for Javaのようなコンポーネントを使用してスプレッドシートをPDFファイルに変換する必要があります。 Aspose.CellsとAspose.PDFは中間ステップを経てスプレッドシートをPDFに変換します。

Aspose.CellsとAspose.PDFを使用してスプレッドシートをPDFに変換するには、次のようにします。

  1. 空のコンストラクタを呼び出してWorkbookクラスのオブジェクトをインスタンス化します。
  2. Aspose.Cells APIを使用して、スプレッドシートで必要な作業を行います。
  3. Workbook.saveメソッドを呼び出して、スプレッドシートを保存します。
    1. ファイル形式をXMLに設定します。
    2. FileFormatTypeインタフェースからAspose_Pdf(事前定義値)を選択します。これにより、保存メソッドがAspose.PDF for Javaと互換性のあるXML形式のスプレッドシートを生成します。
  4. XMLファイルが作成されたら、aspose.pdfパッケージのPdfクラスのオブジェクトを作成します。
  5. PdfクラスのbindXMLメソッドを呼び出し、出力XMLファイルの名前を渡します。
  6. Pdfクラスのsaveメソッドを呼び出し、PDFドキュメントを生成します。

上記の手順を以下の例で実装します。

変換属性

Aspose.Cellsの変換およびその他の側面を改善するために、我々は毎リリース努力しています。 ExcelからPDFへの変換にはいくつかの制約があります。スプレッドシートで指定された一部のフォーマット設定が失われる場合があり、すべての描画オブジェクトがサポートされているわけではありません。

以下の表は、Aspose.Cellsを使用してPDFにエクスポートする際に完全または部分的にサポートされるすべての機能をリスト表示しています。この表は最終的なものではなく、すべてのスプレッドシート属性をカバーしているわけではありません。また、変換に対応していない機能や部分的にサポートされる機能を特定できます。