派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.qxueyou.scc.base.util;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
 
public class JacobUtil {  
    
    public static final String DOC = "doc";  
    public static final String DOCX = "docx";  
    public static final String PDF = "pdf";  
    public static final String XLS = "xls";  
    public static final String XLSX = "xlsx";  
    public static final String MP4 = "mp4";  
    public static final String PPT = "ppt";  
    public static final String PPTX = "pptx";  
   
     // 8 代表word保存成html    
     public static final int WORD2HTML = 8;    
     // 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 pdffile = "D:/upload/b.pdf";    
     String htmlfile="D:/upload/excel.pdf";
     //ppt2pdf(pptfile,pdffile);    
     excel2pdf(pptfile,htmlfile);
 }    
     
/** 
  * @param resourceType 资源类型 
  * @param path 资源路径 
  * @return 
  * 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和图??  
         word2pdf(resourcePath, resource+".pdf");  
     }else if(resourceType.equalsIgnoreCase(XLS)||resourceType.equalsIgnoreCase(XLSX)){  
         //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)    
     ActiveXComponent app = null;    
     try{  
         ComThread.InitSTA();  
         app = new ActiveXComponent("Word.Application");       
         app.setProperty("Visible", false);  
         System.out.println("*****正在转换...*****");    
         // 设置word应用程序不可??  
        // app.setProperty("Visible", new Variant(false));      
         // documents表示word程序的所有文档窗口,(word是多文档应用程序???   
         Dispatch docs = app.getProperty("Documents").toDispatch();      
         // 打开要转换的word文件    
        /* Dispatch doc = Dispatch.invoke(      
                 docs,      
                 "Open",      
                 Dispatch.Method,      
                 new Object[] { docfile, new Variant(false),    
                         new Variant(true) }, new int[1]).toDispatch(); */  
           
         Dispatch doc = Dispatch.call(  
                 docs,       
                 "Open",  
                 docfile,  
                 false,  
                 true).toDispatch();       
         // 调用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文件    
         Dispatch.call(doc, "Close", false);   
     }       
     catch (Exception e)       
     {   ComThread.Release();    
         e.printStackTrace();      
     }       
     finally      
     {       
         //关闭word应用程序    
         app.invoke("Quit", 0);     
         ComThread.Release();  
     }     
     System.out.println("*****转换完毕********");    
 }  
      
   
   
 public static void ppt2pdf(String pptfile,String pdffile){  
     ActiveXComponent app = null;  
     //app.setProperty("Visible", new Variant(false));  
     try {  
         ComThread.InitSTA();  
         app = new ActiveXComponent("PowerPoint.Application");  
         Dispatch files = app.getProperty("Presentations").toDispatch();  
         Dispatch file = Dispatch.call(files, "open", pptfile, false, true).toDispatch();  
         Dispatch.call(file, "SaveAs", pdffile, PPT2PDF);  
         Dispatch.call(file,"Close");  
     } catch (Exception e) {  
         ComThread.Release();  
         e.printStackTrace();  
         //throw e;  
     }finally{  
         app.invoke("Quit");  
         ComThread.Release();  
     }  
 }  
   
 public static void excel2pdf(String excelfile,String pdffile){  
     ActiveXComponent app = null;  
     try{  
         ComThread.InitSTA(true);  
         app = new ActiveXComponent("Excel.Application");  
         app.setProperty("Visible", false);  
         app.setProperty("AutomationSecurity", new Variant(3));//禁用??  
         Dispatch excels = app.getProperty("Workbooks").toDispatch();  
         /*Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{ 
                 excelfile, 
                 new Variant(false), 
                 new Variant(false), 
         },new int[9]).toDispatch();*/  
         Dispatch excel = Dispatch.call(excels, "Open",   
                 excelfile,false,true).toDispatch();  
         //转换格式ExportAsFixedFormat  
         /*Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{ 
                 new Variant(0),//pdf格式=0 
                 pdffile, 
                 new Variant(0)//0=标准(生成的pdf图片不会变模??) 1=??小文??(生成的pdf图片模糊的一塌糊??) 
         }, new int[1]);*/  
         Dispatch.call(excel, "ExportAsFixedFormat",XLS2PDF,  
                 pdffile);  
         Dispatch.call(excel, "Close", false);  
         if(app!=null){  
             app.invoke("Quit");  
             app=null;  
         }  
     }catch(Exception e){  
         ComThread.Release();  
         e.printStackTrace();  
     }finally{  
         ComThread.Release();  
     }  
 }  
 
}