Aspose.PDF for Java系列6-转化PDF文档为Excel
创始人
2024-05-05 15:09:17

Aspose.PDF for Java系列6-转化PDF文档为Excel

Aspose.PDF for Java API允许您将PDF文件呈现为Excel XLS和XLSX文件格式

转化为Excel XLS

Aspose.PDF for Java提供了ExcelSaveOptions类来将PDF转化为XLS格式。将ExcelSaveOptions对象传递给Dodument.save()方法的第二个参数即可。

具体代码如下:

package com.aspose.pdf.examples;import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;import com.aspose.pdf.*;public final class ConvertPDFtoXLSX {private ConvertPDFtoXLSX() {}// The path to the documents directory.private static Path _dataDir = Paths.get("/home/admin1/pdf-examples/Samples");public static void main(String[] args) throws IOException {ConvertPDFtoExcelSimple();ConvertPDFtoExcelAdvanced_InsertBlankColumnAtFirst();ConvertPDFtoExcelAdvanced_MinimizeTheNumberOfWorksheets();ConvertPDFtoExcelAdvanced_SaveXLSX();}public static void ConvertPDFtoExcelSimple() {// Load PDF documentDocument pdfDocument = new Document(_dataDir + "input.pdf");// Instantiate ExcelSave Option objectExcelSaveOptions excelsave = new ExcelSaveOptions();// Save the output in XLS formatpdfDocument.save("PDFToXLS_out.xls", excelsave);}
}

转化为Excel XLS(控制列)

将PDF转化为XLS格式时,会输出第一列为空白列。ExcelSaveOptions类中的InsertBlankColumnAtFirst选项用于控制该列,默认值是true。

具体代码参考如下:

    public static void ConvertPDFtoExcelAdvanced_InsertBlankColumnAtFirst() {// Load PDF documentDocument pdfDocument = new Document(_dataDir + "input.pdf");// Instantiate ExcelSave Option objectExcelSaveOptions excelsave = new ExcelSaveOptions();excelsave.setInsertBlankColumnAtFirst(false);// Save the output in XLS formatpdfDocument.save("PDFToXLS_out.xls", excelsave);}

转化PDF到单个Excel WorkSheet

将PDF转化为XLS时,PDF的每一页会被转化成Excel不同的WorkSheet。原因是MinimizeTheNumberOfWorksheets属性设置的默认值是false。如果转化时要转化为单个WorkSheet,需要设置
MinimizeTheNumberOfWorksheets为true。

具体代码参考如下:

    public static void ConvertPDFtoExcelAdvanced_MinimizeTheNumberOfWorksheets() {// Load PDF documentDocument pdfDocument = new Document(_dataDir + "input.pdf");// Instantiate ExcelSave Option objectExcelSaveOptions excelsave = new ExcelSaveOptions();excelsave.setMinimizeTheNumberOfWorksheets(true);// Save the output in XLS formatpdfDocument.save("PDFToXLS_out.xls", excelsave);}

转化为Excel XLSX

Aspose.PDF默认使用的Excel 2003格式存储数据。如果需要将PDF转化为XLSX格式,Aspose.PDF有一个ExcelSaveOptions的类。需要做XLSX格式的设置。

具体代码参考如下:

    public static void ConvertPDFtoExcelAdvanced_SaveXLSX() {// Load PDF documentDocument pdfDocument = new Document(_dataDir + "input.pdf");// Instantiate ExcelSave Option objectExcelSaveOptions excelSave = new ExcelSaveOptions();excelSave.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);// Save the output in XLS formatpdfDocument.save("PDFToXLS_out.xlsx", excelSave);}

相关内容

热门资讯

刷屏了!吴清最新发声,信息量很... 【导读】中国证券投资基金业协会第四届会员代表大会在京举行,中国证监会主席吴清出席并发表讲话中国基金报...
规划的人行天桥被驳回?深圳一小... (来源:奥一新闻)一座规划已久的人行天桥居民盼了又盼却等来“紧迫性不足”的驳回通知?奥一新闻记者注意...
吴清:加强供需适配,重视含权低...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会!   6月6日,中国证...
星火燎原记(伟大征程) 井冈山红旗广场上举行思政实践活动。黄旭斌摄井冈山革命博物馆第三展厅的一处展柜中,柔和的灯光落在《中国...