100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java语言Word excel ppt转pdf

java语言Word excel ppt转pdf

时间:2022-07-23 09:06:23

相关推荐

java语言Word excel ppt转pdf

引入依赖

具体

<dependency><groupId>net.sf.jacob-project</groupId><artifactId>jacob</artifactId><version>1.14.3</version></dependency>

如果不能够自动引入,需要手动下载(可以直接百度进行下载),将下载好的jar包存放在项目resource中,修改pom文件如下:

<dependency><groupId>net.sf.jacob-project</groupId><artifactId>jacob</artifactId><version>1.17</version><scope>system</scope><systemPath>${basedir}/src/main/resources/lib/jacob.jar</systemPath></dependency>

Word转PDF工具类

public class WordUtils {// public static void main(String[] args) throws IOException {//// String source3 = "C:\\Users\\楚\\Desktop\\能源管理系统上线试运行方案.doc";// String target3 = "C:\\Users\\楚\\Desktop\\能源管理系统上线试运行方案.pdf";// WordUtils pdf = new WordUtils();// pdf.word2pdf(source3, target3);// }public static void word2pdf( String wordFile, String pdfFile) {// 开始时间long start = System.currentTimeMillis();ComThread.InitSTA(true);// 打开wordActiveXComponent app = new ActiveXComponent("Word.Application");app.setProperty("Visible", new Variant(false));app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏System.out.println("开始转换...");try {// 设置word不可见,很多博客下面这里都写了这一句话,其实是没有必要的,因为默认就是不可见的,如果设置可见就是会打开一个word文档,对于转化为pdf明显是没有必要的//app.setProperty("Visible", false);// 获得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的格式宏的值是17Dispatch.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 {// 关闭officeapp.invoke("Quit", 0);ComThread.Release();ComThread.quitMainSTA();}}}

Excel转PDF工具类

public class ExcelToPDFUtil {// public static void main(String[] args) {// int time = Ex2PDF("C:\\Users\\楚\\Desktop\\16318606956952405cba5-7峰谷能耗分析.xlsx", "C:\\Users\\楚\\Desktop\\峰谷能耗分析.pdf");// System.out.println(time);// }public static void Ex2PDF(String inputFile, String pdfFile) {// 0=标准 (生成的PDF图片不会变模糊) 1=最小文件(生成的PDF图片糊的一塌糊涂)final int xlTypePDF = 0;// try {// User32.SetWinEventHook(new UINT(0x0003), new UINT(0x0003), 0, 0, new DWORD(0), new DWORD(0), new UINT(0));// } catch (IllegalAccessException e1) {// e1.printStackTrace();// } catch (NativeException e1) {// e1.printStackTrace();// }ComThread.InitSTA(true);ActiveXComponent ax = new ActiveXComponent("Excel.Application");long date = new Date().getTime();ax.setProperty("Visible", new Variant(false));ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏try {Dispatch excels = ax.getProperty("Workbooks").toDispatch();Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{inputFile, new Variant(false), new Variant(false)},new int[9]).toDispatch();File tofile = new File(pdfFile);if (tofile.exists()) {tofile.delete();}// 转换格式Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{new Variant(0), // PDF格式=0pdfFile, new Variant(xlTypePDF), Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_MISSING,new Variant(false), Variant.VT_MISSING}, new int[1]);long date2 = new Date().getTime();int time = (int) ((date2 - date) / 1000);Dispatch.call(excel, "Close", new Variant(false));} catch (Exception e) {e.printStackTrace();} finally {ax.invoke("Quit");ax = null;ComThread.Release();ComThread.quitMainSTA();}}}

ppt转PDF工具类

public class PoiUtils {static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。static final int ppSaveAsPDF = 32;// ppt 转PDF 格式// public static void main(String[] args) throws IOException {//// String source3 = "C:\\Users\\Ray\\Desktop\\eam\\企业资产管理-EAM系统介绍.pptx";// String target3 = "C:\\Users\\Ray\\Desktop\\eam\\企业资产管理-EAM系统介绍.pdf";// PoiUtils pdf = new PoiUtils();// pdf.ppt2pdf(source3, target3);// }//PPT文档转PDFpublic static void ppt2pdf( String source, String target) {System.out.println("启动PPT");long start = System.currentTimeMillis();ComThread.InitSTA(true);ActiveXComponent app = new ActiveXComponent("Powerpoint.Application"); //创建office的一个应用,比如你操作的是word还是ppt等// app.setProperty("Visible", new Variant(false));// app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏try {//Dispatch 调度处理类,封装了一些操作来操作officeDispatch presentations = app.getProperty("Presentations").toDispatch();System.out.println("打开文档" + source);Dispatch presentation = Dispatch.call(presentations, "Open",source,// FileNametrue,// ReadOnlytrue,// Untitled 指定文件是否有标题。false // WithWindow 指定文件是否可见。).toDispatch();System.out.println("转换文档到PDF " + target);File tofile = new File(target);if (tofile.exists()) {tofile.delete();}Dispatch.call(presentation,//"SaveAs", //target, // FileNameppSaveAsPDF);Dispatch.call(presentation, "Close");long end = System.currentTimeMillis();System.out.println("转换完成."+source+".用时:" + (end - start) + "ms.");} catch (Exception e) {System.out.println("========Error:文档转换失败:" + e.getMessage());} finally {if (app != null)app.invoke("Quit");ComThread.Release();ComThread.quitMainSTA();}}}

使用时直接调用即可,两个参数分别是源文件的绝对地址(包括文件名)和转换后的文件绝对地址(包括文件名)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。