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

一、开发工具: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

三、编写代码:

package dh.hongyi.wed.asset;

import java.io.BufferedInputStream;
import java.io.File;





import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;


public class ToPdf {
    private static final int wdFormatPDF = 17;
    private static final int xlTypePDF = 0;
    private static final int ppSaveAsPDF = 32;
    private static final int msoTrue = -1;
    private static final int msofalse = 0;
	public static  void main(String[] args) {
		// TODO Auto-generated method stub
		//excelTohtml001();
		//convert2PDF("F:/googledowload/1564733032856.xlsx","F:/googledowload/1564733032856.pdf");
		//convert2PDF("http://testgq1.yuhong.com.cn/resource/accountStatement/20190802/1564733052458.xlsx","F:/googledowload/w.pdf");
//		HttpServletResponse response = new HttpServletResponse();
//		downloadFile();
 
	}
    /*jacob配置
     * 把jacob.dll放入 Java\jdk1.5.0_06\jre\bin目录下.
        把jacob.jar放入 Java\jdk1.5.0_0\jre\lib\ext*/  
   //直接调用这个方法即可
       public static boolean convert2PDF(String inputFile, String pdfFile) {
           String suffix =  getFileSufix(inputFile);
           String suffixF =  getFileSufixF(inputFile);
           File file = new File(inputFile);
           if(!file.exists()){
               System.out.println("文件不存在!");
               return false;
           }
           if(suffix.equals("pdf")){
               System.out.println("PDF not need to convert!");
               return false;
           }
           if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){
               return word2PDF(inputFile,pdfFile);
           }else if(suffix.equals("ppt")||suffix.equals("pptx")){
               return ppt2PDF(inputFile,pdfFile);
           }else if(suffix.equals("xls")||suffix.equals("xlsx")||suffix.equals("XLSX")){
               return excel2PDF(inputFile,pdfFile);
           }else{
               System.out.println("文件格式不支持转换!");
               return false;
           }
       }
       public static String getFileSufix(String fileName){
           int splitIndex = fileName.lastIndexOf(".");
           return fileName.substring(splitIndex + 1);
       }
       public static String getFileSufixF(String fileName){
           int splitIndex = fileName.lastIndexOf("resource")+7;
           int splitIndexF = fileName.lastIndexOf(".");
           int splitIndexM = fileName.lastIndexOf("/");
           System.out.println(fileName.substring(splitIndex + 1));
           System.out.println(fileName.substring(splitIndex));
           System.out.println(fileName.substring(splitIndexF));
           System.out.println(fileName.substring(splitIndexM+1, splitIndexF));
           return fileName.substring(splitIndex + 1);
       }
       public static boolean word2PDF(String inputFile,String pdfFile){
            ActiveXComponent app = null;  
            Dispatch doc = null;
            boolean result=true;
           try{
           //打开word应用程序
            app = new ActiveXComponent("Word.Application");
           //设置word不可见
           app.setProperty("Visible", false);
           //获得word中所有打开的文档,返回Documents对象
           Dispatch docs = app.getProperty("Documents").toDispatch();
           //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            doc = Dispatch.call(docs,
                                       "Open",
                                       inputFile,
                                       false,
                                       true
                                       ).toDispatch();
           
           Dispatch.call(doc,
                   "ExportAsFixedFormat",
                   pdfFile,
                   wdFormatPDF     //word保存为pdf格式宏,值为17
                   );
           
           result= true;
       }catch(Exception e){
           result= false;
       }finally {  
           if (doc != null) {  
               Dispatch.call(doc, "Close");  
           }  
           if (app != null) {  
               app.invoke("Quit");  
           }
       }
           return result;
       }
       
       public static boolean excel2PDF(String inputFile,String pdfFile){
            ActiveXComponent app = null;  
            Dispatch excel = null;
            boolean result=true;
           try{
               app = new ActiveXComponent("Excel.Application");
               app.setProperty("Visible", false);
               Dispatch excels = app.getProperty("Workbooks").toDispatch();
               excel = Dispatch.call(excels,
                                       "Open",
                                       inputFile,
                                       false,
                                       true
                                       ).toDispatch();
           Dispatch.call(excel,
                       "ExportAsFixedFormat",
                       xlTypePDF,      
                       pdfFile
                       );
           result= true;
       }catch(Exception e){
           result= false;
       }finally {  
           if (excel != null) {  
               Dispatch.call(excel, "Close");  
           }  
           if (app != null) {  
               app.invoke("Quit");  
           }  
       }  
           System.out.println("excel转pdf结束");
            return result;
       }
       
       public static boolean ppt2PDF(String srcFilePath, String pdfFilePath){
            ActiveXComponent app = null;  
            Dispatch ppt = null;  
            boolean result=true;
                try {  
                    ComThread.InitSTA();  
                    app = new ActiveXComponent("PowerPoint.Application");  
                    Dispatch ppts = app.getProperty("Presentations").toDispatch();  
      
                    // 因POWER.EXE的发布规则为同步,所以设置为同步发布  
                    ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly  
                            true,// Untitled指定文件是否有标题  
                            false// WithWindow指定文件是否可见  
                            ).toDispatch();  
      
                    Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); //ppSaveAsPDF为特定值32  
                    
                    result=true; // set flag true;  
                } catch (ComFailException e) {  
                    result=false;  
                } catch (Exception e) {  
                    result=false;  
                } finally {  
                    if (ppt != null) {  
                        Dispatch.call(ppt, "Close");  
                    }  
                    if (app != null) {  
                        app.invoke("Quit");  
                    }  
                    ComThread.Release();  
                }  
                return result;
       }
       
       /*文件下载*/
       public static void downloadFile(HttpServletResponse response,String fileName,String path){
           if (fileName != null) {
               //设置文件路径
               File file = new File(path);
               if (file.exists()) {
                   response.setHeader("content-type", "application/octet-stream");
                   response.setContentType("application/octet-stream");
                   try {
                       response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
                   } catch (UnsupportedEncodingException e) {
                       e.printStackTrace();
                   }
                   byte[] buffer = new byte[1024];
                   FileInputStream fis = null;
                   BufferedInputStream bis = null;
                   try {
                       fis = new FileInputStream(file);
                       bis = new BufferedInputStream(fis);
                       OutputStream os = response.getOutputStream();
                       int i = bis.read(buffer);
                       while (i != -1) {
                           os.write(buffer, 0, i);
                           i = bis.read(buffer);
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   } finally {
                       if (bis != null) {
                           try {
                               bis.close();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                       if (fis != null) {
                           try {
                               fis.close();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                   }
               }
           }
       }

}

附件:
jacob-1.18.M2.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值