| | |
| | | public static final String PPT = "ppt"; |
| | | public static final String PPTX = "pptx"; |
| | | |
| | | // 8 代表word保存成html |
| | | // 8 代表word保存成html |
| | | public static final int WORD2HTML = 8; |
| | | // 17代表word保存成pdf |
| | | // 17代表word保存成pdf |
| | | public static final int WD2PDF = 17; |
| | | public static final int PPT2PDF = 32; |
| | | public static final int XLS2PDF = 0; |
| | | |
| | | public static void main(String[] args) { |
| | | String pptfile = "D:/upload/新员工入职向导及信息201605.xlsx"; |
| | | String pptfile = "D:/upload/新员工入职向导及信息201605.xlsx"; |
| | | //String pdffile = "D:/upload/b.pdf"; |
| | | String htmlfile="D:/upload/excel.pdf"; |
| | | //ppt2pdf(pptfile,pdffile); |
| | |
| | | } |
| | | |
| | | /** |
| | | * @param resourceType 资源类型 |
| | | * @param path 资源路径 |
| | | * @param resourceType 资源类型 |
| | | * @param path 资源路径 |
| | | * @return |
| | | * TODO 文件转换 |
| | | * TODO 文件转换 |
| | | */ |
| | | public static Integer formatConvert(String resourceType, String resourcePath) { |
| | | Integer pages = 0; |
| | | String resource = resourcePath.substring(0, resourcePath.lastIndexOf(".")); |
| | | if(resourceType.equalsIgnoreCase(DOC)||resourceType.equalsIgnoreCase(DOCX)){ |
| | | //word转成pdf和图?? |
| | | //word转成pdf和图?? |
| | | word2pdf(resourcePath, resource+".pdf"); |
| | | }else if(resourceType.equalsIgnoreCase(XLS)||resourceType.equalsIgnoreCase(XLSX)){ |
| | | //excel文件转成图片 |
| | | //excel文件转成图片 |
| | | excel2pdf(resourcePath, resource+".pdf"); |
| | | }else if(resourceType.equalsIgnoreCase(PPT)||resourceType.equalsIgnoreCase(PPTX)){ |
| | | ppt2pdf(resourcePath, resource+".pdf"); |
| | | }else if(resourceType.equalsIgnoreCase(MP4)){ |
| | | //视频文件不转?? |
| | | //视频文件不转?? |
| | | pages = 0; |
| | | } |
| | | return pages; |
| | |
| | | |
| | | public static void word2pdf(String docfile, String pdffile) |
| | | { |
| | | // 启动word应用程序(Microsoft Office Word 2003) |
| | | // 启动word应用程序(Microsoft Office Word 2003) |
| | | ActiveXComponent app = null; |
| | | try{ |
| | | ComThread.InitSTA(); |
| | | app = new ActiveXComponent("Word.Application"); |
| | | app.setProperty("Visible", false); |
| | | System.out.println("*****正在转换...*****"); |
| | | // 设置word应用程序不可?? |
| | | System.out.println("*****正在转换...*****"); |
| | | // 设置word应用程序不可?? |
| | | // app.setProperty("Visible", new Variant(false)); |
| | | // documents表示word程序的所有文档窗口,(word是多文档应用程序??? |
| | | // documents表示word程序的所有文档窗口,(word是多文档应用程序??? |
| | | Dispatch docs = app.getProperty("Documents").toDispatch(); |
| | | // 打开要转换的word文件 |
| | | // 打开要转换的word文件 |
| | | /* Dispatch doc = Dispatch.invoke( |
| | | docs, |
| | | "Open", |
| | |
| | | docfile, |
| | | false, |
| | | true).toDispatch(); |
| | | // 调用Document对象的saveAs方法,将文档保存为pdf格式 |
| | | // 调用Document对象的saveAs方法,将文档保存为pdf格式 |
| | | /*Dispatch.invoke(doc, "ExportAsFixedFormat", Dispatch.Method, new Object[] { |
| | | pdffile, new Variant(wdFormatPDF) }, new int[1]);*/ |
| | | |
| | | Dispatch.call(doc, "ExportAsFixedFormat", pdffile, WD2PDF); |
| | | // 关闭word文件 |
| | | // 关闭word文件 |
| | | Dispatch.call(doc, "Close", false); |
| | | } |
| | | catch (Exception e) |
| | |
| | | } |
| | | finally |
| | | { |
| | | //关闭word应用程序 |
| | | //关闭word应用程序 |
| | | app.invoke("Quit", 0); |
| | | ComThread.Release(); |
| | | } |
| | | System.out.println("*****转换完毕********"); |
| | | System.out.println("*****转换完毕********"); |
| | | } |
| | | |
| | | |
| | |
| | | ComThread.InitSTA(true); |
| | | app = new ActiveXComponent("Excel.Application"); |
| | | app.setProperty("Visible", false); |
| | | app.setProperty("AutomationSecurity", new Variant(3));//禁用?? |
| | | app.setProperty("AutomationSecurity", new Variant(3));//禁用?? |
| | | Dispatch excels = app.getProperty("Workbooks").toDispatch(); |
| | | /*Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{ |
| | | excelfile, |
| | |
| | | },new int[9]).toDispatch();*/ |
| | | Dispatch excel = Dispatch.call(excels, "Open", |
| | | excelfile,false,true).toDispatch(); |
| | | //转换格式ExportAsFixedFormat |
| | | //转换格式ExportAsFixedFormat |
| | | /*Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{ |
| | | new Variant(0),//pdf格式=0 |
| | | new Variant(0),//pdf格式=0 |
| | | pdffile, |
| | | new Variant(0)//0=标准(生成的pdf图片不会变模??) 1=??小文??(生成的pdf图片模糊的一塌糊??) |
| | | new Variant(0)//0=标准(生成的pdf图片不会变模??) 1=??小文??(生成的pdf图片模糊的一塌糊??) |
| | | }, new int[1]);*/ |
| | | Dispatch.call(excel, "ExportAsFixedFormat",XLS2PDF, |
| | | pdffile); |