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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsand set its format toXMLSpreadSheet2003. - Call
document.save(outputFile.toString(), saveOptions)so the loaded PDF is serialized in the Excel 2003 XML schema. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsand set its format toXLSX. - Call
document.save(outputFile.toString(), saveOptions)so the PDF layout is exported as an Office Open XML workbook. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsforXLSXoutput. - Enable
setInsertBlankColumnAtFirst(true)when an extra leading column is needed to improve the worksheet layout produced from the PDF. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsforXLSXexport. - Enable
setMinimizeTheNumberOfWorksheets(true)so multiple PDF pages are consolidated into fewer worksheets. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsand set the format toXLSM. - Call
document.save(outputFile.toString(), saveOptions)so the PDF content is exported to a macro-enabled workbook container. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsand set the format toCSV. - Call
document.save(outputFile.toString(), saveOptions)so the PDF content is flattened to comma-separated text output. - 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.
- Open the source PDF in a
Documentinstance. - Create
ExcelSaveOptionsand set the format toODS. - Call
document.save(outputFile.toString(), saveOptions)so the PDF is exported in OpenDocument spreadsheet format. - 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);
}