jacob java excel_Java 使用jacob 工具 实现excel 转 pdf (仅适用于windows)

private static final Integer WORD_TO_PDF_OPERAND = 17;private static final Integer PPT_TO_PDF_OPERAND = 32;private static final Integer EXCEL_TO_PDF_OPERAND = 0;private void doc2pdf(String srcFilePath, String pdfFilePath) throwsException {

ActiveXComponent app= null;

Dispatch doc= null;try{

ComThread.InitSTA();

app= new ActiveXComponent("Word.Application");

app.setProperty("Visible", false);

Dispatch docs= app.getProperty("Documents").toDispatch();

Object[] obj= newObject[]{

srcFilePath,new Variant(false),new Variant(false),//是否只读

new Variant(false),new Variant("pwd")

};

doc= Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();//Dispatch.put(doc, "Compatibility", false);//兼容性检查,为特定值false不正确

Dispatch.put(doc, "RemovePersonalInformation", false);

Dispatch.call(doc,"ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); //word保存为pdf格式宏,值为17

}catch(Exception e) {

e.printStackTrace();throwe;

}finally{if (doc != null) {

Dispatch.call(doc,"Close", false);

}if (app != null) {

app.invoke("Quit", 0);

}

ComThread.Release();

}

}private void ppt2pdf(String srcFilePath, String pdfFilePath) throwsException {

ActiveXComponent app= null;

Dispatch ppt= null;try{

ComThread.InitSTA();

app= new ActiveXComponent("PowerPoint.Application");

Dispatch ppts= app.getProperty("Presentations").toDispatch();/** call

* param 4: ReadOnly

* param 5: Untitled指定文件是否有标题

* param 6: WithWindow指定文件是否可见

**/ppt= Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch();

Dispatch.call(ppt,"SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); //ppSaveAsPDF为特定值32

}catch(Exception e) {

e.printStackTrace();throwe;

}finally{if (ppt != null) {

Dispatch.call(ppt,"Close");

}if (app != null) {

app.invoke("Quit");

}

ComThread.Release();

}

}private static boolean excel2Pdf(String inFilePath, String outFilePath) throwsException {

ActiveXComponent ax= null;

Dispatch excel= null;try{

ComThread.InitSTA();

ax= new ActiveXComponent("Excel.Application");

ax.setProperty("Visible", new Variant(false));

ax.setProperty("AutomationSecurity", new Variant(3)); //禁用宏

Dispatch excels= ax.getProperty("Workbooks").toDispatch();

Object[] obj= newObject[]{

inFilePath,new Variant(false),new Variant(false)

};

excel= Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();//将excel表格 设置成A3的大小

Dispatch sheets = Dispatch.call(excel, "Worksheets").toDispatch();

Dispatch sheet= Dispatch.call(sheets, "Item", new Integer(1)).toDispatch();

Dispatch pageSetup= Dispatch.call(sheet, "PageSetup").toDispatch();

Dispatch.put(pageSetup,"PaperSize", new Integer(8));//A3是8,A4是9,A5是11等等//转换格式

Object[] obj2 = newObject[]{new Variant(EXCEL_TO_PDF_OPERAND), //PDF格式=0

outFilePath,new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件

};

Dispatch.invoke(excel,"ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]);

}catch(Exception es) {

es.printStackTrace();return false;//throw es;

} finally{if (excel != null) {

Dispatch.call(excel,"Close", new Variant(false));

}if (ax != null) {

ax.invoke("Quit", newVariant[] {});

ax= null;

}

ComThread.Release();

}return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jacob-1.18 包含jacob-1.18-x64.dll jacob-1.18-x86.dll 亲测可用: 下面为pdf使用方法 package com.pdf.doctopdf.pdf; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import java.io.File; public class TestJacob { public static void main(String args[]) { ActiveXComponent app = null; String wordFile = "C:\\Users\\admin\\Desktop\\jar\\年会系统优化_20210820_V0.2.docx"; String pdfFile = "C:\\Users\\admin\\Desktop\\jar\\测试pdf.pdf"; System.out.println("开始换..."); // 开始时间 long start = System.currentTimeMillis(); try { // 打开word app = new ActiveXComponent("Word.Application"); // 获得word中所有打开的文档 Dispatch documents = app.getProperty("Documents").toDispatch(); System.out.println("打开文件: " + wordFile); // 打开文档 Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); // 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在 File target = new File(pdfFile); if (target.exists()) { target.delete(); } System.out.println("另存为: " + pdfFile); // 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17 Dispatch.call(document, "SaveAs", pdfFile, 17); // 关闭文档 Dispatch.call(document, "Close", false); // 结束时间 long end = System.currentTimeMillis(); System.out.println("换成功,用时:" + (end - start) + "ms"); }catch(Exception e) { e.getMessage(); System.out.println("换失败"+e.getMessage()); }finally { // 关闭office app.invoke("Quit", 0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值