Convert PDF to Excel in Java

Aspose.PDF for Java can export PDF content to multiple spreadsheet formats with different layout options. Use ExcelSaveOptions to choose the target workbook format and control how page content is mapped into worksheets and columns.

Convert PDF to Excel 2003 XML

Use this example when PDF content should be exported to the Excel 2003 XML spreadsheet format.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions and set its format to XMLSpreadSheet2003.
  3. Call document.save(outputFile.toString(), saveOptions) so the loaded PDF is serialized in the Excel 2003 XML schema.
  4. Save the converted output file.
public static void convertPdfToExcelSpreadSheet2003(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.XMLSpreadSheet2003);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to XLSX

Use this example when PDF content should be converted into the Excel 2007+ XLSX format.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions and set its format to XLSX.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF layout is exported as an Office Open XML workbook.
  4. Save the output spreadsheet file.
public static void convertPdfToExcel2007(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to XLSX with column control

Use this example when column handling should be adjusted during PDF-to-Excel conversion.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions for XLSX output.
  3. Enable setInsertBlankColumnAtFirst(true) when an extra leading column is needed to improve the worksheet layout produced from the PDF.
  4. Call document.save(outputFile.toString(), saveOptions) and write the converted XLSX file.
public static void convertPdfToExcel2007ControlColumn(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
        saveOptions.setInsertBlankColumnAtFirst(true);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to a single Excel worksheet

Use this example when all PDF pages should be exported into one worksheet.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions for XLSX export.
  3. Enable setMinimizeTheNumberOfWorksheets(true) so multiple PDF pages are consolidated into fewer worksheets.
  4. Call document.save(outputFile.toString(), saveOptions) and save the XLSX output file.
public static void convertPdfToExcel2007SingleExcelWorksheet(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
        saveOptions.setMinimizeTheNumberOfWorksheets(true);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to XLSM

Use this example when the PDF output should be saved as a macro-enabled Excel workbook.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions and set the format to XLSM.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF content is exported to a macro-enabled workbook container.
  4. Save the XLSM file.
public static void convertPdfToExcel2007Macro(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.XLSM);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to CSV

Use this example when PDF tabular content should be exported as CSV.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions and set the format to CSV.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF content is flattened to comma-separated text output.
  4. Save the generated CSV file.
public static void convertPdfToExcel2007Csv(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.CSV);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to ODS

Use this example when PDF content should be exported to the OpenDocument spreadsheet format.

  1. Open the source PDF in a Document instance.
  2. Create ExcelSaveOptions and set the format to ODS.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF is exported in OpenDocument spreadsheet format.
  4. Save the converted ODS file.
public static void convertPdfToOds(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ExcelSaveOptions saveOptions = new ExcelSaveOptions();
        saveOptions.setFormat(ExcelSaveOptions.ExcelFormat.ODS);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}