PDF を Excel に変換

Aspose.PDF for Android via Java API を使用すると、PDF ファイルを Excel にレンダリングできます。 XLS そして XLSX ファイル形式です。すでに別の API があり、その名前は Aspose.Cells for Java, 既存のExcelワークブックを作成および操作する機能を提供します。\u00A0ExcelワークブックをPDF形式に変換する機能も提供します。

PDFをExcel XLSに変換

PDFファイルをXLS形式に変換するには、Aspose.PDFにクラスがあります ExcelSaveOptions. オブジェクトは ExcelSaveOptions クラスは、 Document.Save(..) コンストラクタへの第2引数として渡されます。

PDF ファイルを XLSX 形式に変換することは、Aspose.PDF for Java 18.6 バージョンのライブラリの一部です。PDF ファイルを XLSX 形式に変換するには、setFormat() メソッドを使用して形式を XLSX に設定する必要があります。 ExcelSaveOptions クラス。

以下のコードスニペットは、PDFファイルをxlsおよび.xlsx形式に変換する方法を示しています:

public void convertPDFtoExcelSimple() {
        // Open the source PDF document
        try {
            document = new Document(inputStream);
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }

        // Instantiate ExcelSave Option object
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();

        File xlsFileName = new File(fileStorage, "PDF-to-Excel.xlsx");
        try {
            // Save the file into MS document format
            document.save(xlsFileName.toString(), SaveFormat.Excel);
        }
        catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }

Control Column を使用して PDF を XLS に変換

PDF を XLS 形式に変換する際、出力ファイルの最初の列として空白列が追加されます。The in ExcelSaveOptions class InsertBlankColumnAtFirst オプションはこの列を制御するために使用されます。そのデフォルト値は true です。

public void convertPDFtoExcelAdvanced_InsertBlankColumnAtFirst() {
        // Open the source PDF document
        try {
            document = new Document(inputStream);
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }

        // Instantiate ExcelSave Option object
        ExcelSaveOptions excelSaveOptions = new ExcelSaveOptions();
        excelSaveOptions.setInsertBlankColumnAtFirst(false);

        File xlsFileName = new File(fileStorage, "PDF-to-Excel.xlsx");
        try {
            // Save the file into MS document format
            document.save(xlsFileName.toString(), excelSaveOptions);
        }
        catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }

PDF を単一の Excel ワークシートに変換

ページ数の多い PDF ファイルを XLS にエクスポートする場合、各ページが Excel ファイル内の別々のシートにエクスポートされます。これは、デフォルトで MinimizeTheNumberOfWorksheets プロパティが false に設定されているためです。すべてのページを出力 Excel ファイルの 1 つのシートにエクスポートするには、MinimizeTheNumberOfWorksheets プロパティを true に設定してください。

 public void convertPDFtoExcelAdvanced_MinimizeTheNumberOfWorksheets() {
        // Open the source PDF document
        try {
            document = new Document(inputStream);
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }

        // Instantiate ExcelSave Option object
        ExcelSaveOptions excelSaveOptions = new ExcelSaveOptions();
        excelSaveOptions.setMinimizeTheNumberOfWorksheets(true);

        // Save the output in XLSX
        File xlsFileName = new File(fileStorage, "PDF-to-Excel.xlsx");
        try {
            // Save the file into MS Excel format
            document.save(xlsFileName.toString(), excelSaveOptions);
        }
        catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }

XLSX 形式に変換

既定では Aspose.PDF はデータの保存に XML Spreadsheet 2003 を使用します。PDF ファイルを XLSX 形式に変換するために、Aspose.PDF には Format プロパティを持つ ExcelSaveOptions というクラスがあります。そのクラスのオブジェクトは ExcelSaveOptions クラスは Document.Save(..) メソッドの第二引数として渡されます。

 public void convertPDFtoExcelAdvanced_SaveCSV() {
        // Load PDF document
        try {
            document = new Document(inputStream);
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }

        // Instantiate ExcelSave Option object
        ExcelSaveOptions excelSaveOptions = new ExcelSaveOptions();
        excelSaveOptions.setFormat(ExcelSaveOptions.ExcelFormat.CSV);

        // Save the output in CSV
        File xlsFileName = new File(fileStorage, "PDF-to-Excel.csv");
        try {
            // Save the file into CSV format
            document.save(xlsFileName.toString(), excelSaveOptions);
        }
        catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }