java web 开发之 office(excel、doc等)文件转pdf

允我心安 提交于 2020-03-01 22:10:18

一、开发工具:office 16、jacob-1.18-M2、jboss 1.6

二、开发配置:

       1、解压缩---》

       2、配置jacob:

                  A C:\Windows\System32 jacob-1.18-M2-x64.dll
                  B C:\Program Files\Java\jdk1.6.0_43\jre\bin jacob-1.18-M2-x64.dll
                  C D:\jboss-6.0.0.Final\server\default\lib jacob.jar

三、编写代码:

      

  1 package dh.hongyi.wed.asset;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.File;
  5 
  6 
  7 
  8 
  9 
 10 import java.io.FileInputStream;
 11 import java.io.IOException;
 12 import java.io.OutputStream;
 13 import java.io.UnsupportedEncodingException;
 14 
 15 import javax.servlet.http.HttpServletResponse;
 16 
 17 import com.jacob.activeX.ActiveXComponent;
 18 import com.jacob.com.ComFailException;
 19 import com.jacob.com.ComThread;
 20 import com.jacob.com.Dispatch;
 21 
 22 
 23 public class ToPdf {
 24     private static final int wdFormatPDF = 17;
 25     private static final int xlTypePDF = 0;
 26     private static final int ppSaveAsPDF = 32;
 27     private static final int msoTrue = -1;
 28     private static final int msofalse = 0;
 29     public static  void main(String[] args) {
 30         // TODO Auto-generated method stub
 31         //excelTohtml001();
 32         //convert2PDF("F:/googledowload/1564733032856.xlsx","F:/googledowload/1564733032856.pdf");
 33         //convert2PDF("http://testgq1.yuhong.com.cn/resource/accountStatement/20190802/1564733052458.xlsx","F:/googledowload/w.pdf");
 34 //        HttpServletResponse response = new HttpServletResponse();
 35 //        downloadFile();
 36  
 37     }
 38 
 39    //直接调用这个方法即可
 40        public static boolean convert2PDF(String inputFile, String pdfFile) {
 41            String suffix =  getFileSufix(inputFile);
 42            String suffixF =  getFileSufixF(inputFile);
 43            File file = new File(inputFile);
 44            if(!file.exists()){
 45                System.out.println("文件不存在!");
 46                return false;
 47            }
 48            if(suffix.equals("pdf")){
 49 
 50                return false;
 51            }
 52            if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){
 53                return word2PDF(inputFile,pdfFile);
 54            }else if(suffix.equals("ppt")||suffix.equals("pptx")){
 55                return ppt2PDF(inputFile,pdfFile);
 56            }else if(suffix.equals("xls")||suffix.equals("xlsx")||suffix.equals("XLSX")){
 57                return excel2PDF(inputFile,pdfFile);
 58            }else{
 59 
 60                return false;
 61            }
 62        }
 63        public static String getFileSufix(String fileName){
 64            int splitIndex = fileName.lastIndexOf(".");
 65            return fileName.substring(splitIndex + 1);
 66        }
 67        public static String getFileSufixF(String fileName){
 68            int splitIndex = fileName.lastIndexOf("resource")+7;
 69            int splitIndexF = fileName.lastIndexOf(".");
 70            int splitIndexM = fileName.lastIndexOf("/");
 71            System.out.println(fileName.substring(splitIndex + 1));
 72            System.out.println(fileName.substring(splitIndex));
 73            System.out.println(fileName.substring(splitIndexF));
 74            System.out.println(fileName.substring(splitIndexM+1, splitIndexF));
 75            return fileName.substring(splitIndex + 1);
 76        }
 77        public static boolean word2PDF(String inputFile,String pdfFile){
 78             ActiveXComponent app = null;  
 79             Dispatch doc = null;
 80             boolean result=true;
 81            try{
 82            //打开word应用程序
 83             app = new ActiveXComponent("Word.Application");
 84            //设置word不可见
 85            app.setProperty("Visible", false);
 86            //获得word中所有打开的文档,返回Documents对象
 87            Dispatch docs = app.getProperty("Documents").toDispatch();
 88            //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
 89             doc = Dispatch.call(docs,
 90                                        "Open",
 91                                        inputFile,
 92                                        false,
 93                                        true
 94                                        ).toDispatch();
 95            
 96            Dispatch.call(doc,
 97                    "ExportAsFixedFormat",
 98                    pdfFile,
 99                    wdFormatPDF     //word保存为pdf格式宏,值为17
100                    );
101            
102            result= true;
103        }catch(Exception e){
104            result= false;
105        }finally {  
106            if (doc != null) {  
107                Dispatch.call(doc, "Close");  
108            }  
109            if (app != null) {  
110                app.invoke("Quit");  
111            }
112        }
113            return result;
114        }
115        
116        public static boolean excel2PDF(String inputFile,String pdfFile){
117             ActiveXComponent app = null;  
118             Dispatch excel = null;
119             boolean result=true;
120            try{
121                app = new ActiveXComponent("Excel.Application");
122                app.setProperty("Visible", false);
123                Dispatch excels = app.getProperty("Workbooks").toDispatch();
124                excel = Dispatch.call(excels,
125                                        "Open",
126                                        inputFile,
127                                        false,
128                                        true
129                                        ).toDispatch();
130            Dispatch.call(excel,
131                        "ExportAsFixedFormat",
132                        xlTypePDF,      
133                        pdfFile
134                        );
135            result= true;
136        }catch(Exception e){
137            result= false;
138        }finally {  
139            if (excel != null) {  
140                Dispatch.call(excel, "Close");  
141            }  
142            if (app != null) {  
143                app.invoke("Quit");  
144            }  
145        }  
146            System.out.println("excel转pdf结束");
147             return result;
148        }
149        
150        public static boolean ppt2PDF(String srcFilePath, String pdfFilePath){
151             ActiveXComponent app = null;  
152             Dispatch ppt = null;  
153             boolean result=true;
154                 try {  
155                     ComThread.InitSTA();  
156                     app = new ActiveXComponent("PowerPoint.Application");  
157                     Dispatch ppts = app.getProperty("Presentations").toDispatch();  
158       
159                     // 因POWER.EXE的发布规则为同步,所以设置为同步发布  
160                     ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly  
161                             true,// Untitled指定文件是否有标题  
162                             false// WithWindow指定文件是否可见  
163                             ).toDispatch();  
164       
165                     Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); //ppSaveAsPDF为特定值32  
166                     
167                     result=true; // set flag true;  
168                 } catch (ComFailException e) {  
169                     result=false;  
170                 } catch (Exception e) {  
171                     result=false;  
172                 } finally {  
173                     if (ppt != null) {  
174                         Dispatch.call(ppt, "Close");  
175                     }  
176                     if (app != null) {  
177                         app.invoke("Quit");  
178                     }  
179                     ComThread.Release();  
180                 }  
181                 return result;
182        }
183        
184        /*文件下载*/
185        public static void downloadFile(HttpServletResponse response,String fileName,String path){
186            if (fileName != null) {
187                //设置文件路径
188                File file = new File(path);
189                if (file.exists()) {
190                    response.setHeader("content-type", "application/octet-stream");
191                    response.setContentType("application/octet-stream");
192                    try {
193                        response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
194                    } catch (UnsupportedEncodingException e) {
195                        e.printStackTrace();
196                    }
197                    byte[] buffer = new byte[1024];
198                    FileInputStream fis = null;
199                    BufferedInputStream bis = null;
200                    try {
201                        fis = new FileInputStream(file);
202                        bis = new BufferedInputStream(fis);
203                        OutputStream os = response.getOutputStream();
204                        int i = bis.read(buffer);
205                        while (i != -1) {
206                            os.write(buffer, 0, i);
207                            i = bis.read(buffer);
208                        }
209                    } catch (Exception e) {
210                        e.printStackTrace();
211                    } finally {
212                        if (bis != null) {
213                            try {
214                                bis.close();
215                            } catch (IOException e) {
216                                e.printStackTrace();
217                            }
218                        }
219                        if (fis != null) {
220                            try {
221                                fis.close();
222                            } catch (IOException e) {
223                                e.printStackTrace();
224                            }
225                        }
226                    }
227                }
228            }
229        }
230 
231 }
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!