Java 把 excel 转化为 pdf

Posted 多言

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 把 excel 转化为 pdf相关的知识,希望对你有一定的参考价值。


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExcelToPdf {
public static void excel2pdf(String source, String target) {
System.out.println("启动Excel");
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println("打开文档" + source);
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println("转换文档到PDF " + target);
Dispatch.call(workbook, "Close", f);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
}finally {
if (app != null){
app.invoke("Quit", new Variant[] {});
}
}
}
}

 

public static void main(String[] args){
String source2 = "D:\abc.xls";
String target2 = "D:\def.pdf";
ExcelToPdf.excel2pdf(source2,target2);
}

 

jacob.jar

需要的包来自  https://blog.csdn.net/ordinaryprogrammerc/article/details/84141472 

链接:https://pan.baidu.com/s/1n_FDqcQLoBUYWDKLn2ibOw 
提取码:62yt 

链接:https://pan.baidu.com/s/1uT2bW1GfW_f_eQ4TUva5rg
提取码:4kcw

1.将jacob-1.17-M2-x64.dll 放到java安装目录下: jdk1.X.Xjrein,即你的JDK安装路径下的jre中的bin中;

2.将jacob-1.17-M2-x64.dll放到tomcat的bin下面;

3.将jacob-1.17-M2-x64.dll 放到C:WindowsSystem32

接着将jacob.jar文件放在eclipse中项目的lib文件夹下,具体为找到项目的目录,进入web-inf目录下的lib目录下,将jacob.jar放进去;

 

以上是关于Java 把 excel 转化为 pdf的主要内容,如果未能解决你的问题,请参考以下文章

怎么把excel和word结合在一起转化为PDF WordExcel转换PDF

用vba把excel文件发布转化为pdf格式文件

怎么把pdf转化成Excel格式?

如何将HTML文件转化成excel的文件,

pdf表格转化为excel表格

java 获取当天日期,并将日期转化成数值。