david-PC\david
2018-06-12 f240ac3ccd37c541cab2c21cfc433d3510999a3c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package frame.file.office.excel;
 
import java.io.File;
 
import org.apache.log4j.Logger;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
 
public class ExcelTranslator {
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(ExcelTranslator.class);
    }
    
    public synchronized static boolean testComActive() {
        ComThread.InitSTA();
        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        xl.invoke("Quit", new Variant[] {});
        return true;
    }
    
    public synchronized static void saveAs07(File sourceFile, File targetFile) throws Exception {
        if (targetFile.exists() && targetFile.canRead()) {
            targetFile.delete();
        }
        
        ComThread.InitSTA();
        try {
            ActiveXComponent xl = new ActiveXComponent("Excel.Application");
            try {
                Dispatch workbooks = null; Dispatch workbook = null;
                
                try {
                    // 不显示Excel
                    xl.setProperty("Visible", new Variant(false));
                    // 不提示警告等信息
                    xl.setProperty("DisplayAlerts", new Variant(false));
 
                    workbooks = xl.getProperty("Workbooks").toDispatch();
                    workbook = Dispatch.call(workbooks, "Open", sourceFile.getAbsolutePath()).toDispatch();
                    
                    // 另保存为Excel, 18-2003, 51-2007
                    Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { targetFile.getAbsolutePath(), new Variant(51) }, new int[1]);
                }
                finally {
                    try {
                        Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
                    }
                    catch (Exception ex) {
                    }                    
                }
            }
            finally {
                try {
                    xl.invoke("Quit", new Variant[] {});
                }
                catch (Exception ex) {
                }
            }
        }
        catch (Exception e) {
            logger.debug(e);
            throw e;
        }
        finally {
            ComThread.Release();
        }
    }
    
    public synchronized static void saveAs03(File sourceFile, File targetFile) throws Exception {
        if (targetFile.exists() && targetFile.canRead()) {
            targetFile.delete();
        }
        
        ComThread.InitSTA();
        try {
            ActiveXComponent xl = new ActiveXComponent("Excel.Application");
            try {
                Dispatch workbooks = null; Dispatch workbook = null;
                try {
                    // 不显示Excel
                    xl.setProperty("Visible", new Variant(false));
                    // 不提示警告等信息
                    xl.setProperty("DisplayAlerts", new Variant(false));
 
                    workbooks = xl.getProperty("Workbooks").toDispatch();
                    workbook = Dispatch.call(workbooks, "Open", sourceFile.getAbsolutePath()).toDispatch();
                    
                    // 另保存为Excel, 18-2003, 51-2007
                    Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { targetFile.getAbsolutePath(), new Variant(18) }, new int[1]);
                    
                }
                finally {
                    try {
                        Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
                    }
                    catch (Exception ex) {
                    }
                }
            }
            finally {
                try {
                    xl.invoke("Quit", new Variant[] {});
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                    logger.error(ex.getMessage());
                }                
            }
        }
        catch (Exception e) {
            logger.debug(e);
            throw e;
        }
        finally {
            ComThread.Release();
        }
    }
 
    public synchronized static void saveAsXlsb(File sourceFile, File targetFile) {
        ComThread.InitSTA();
        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        Dispatch workbooks = null;
        Dispatch workbook = null;
 
        try {
            if (targetFile.exists() && targetFile.canRead()) {
                targetFile.delete();
            }
 
            // 不显示Excel
            xl.setProperty("Visible", new Variant(false));
            // 不提示警告等信息
            xl.setProperty("DisplayAlerts", new Variant(false));
 
            workbooks = xl.getProperty("Workbooks").toDispatch();
            //workbook = Dispatch.call(workbooks, "Open", sourceFile.getAbsolutePath()).toDispatch();
            //0--正常打开; 1--修复工作簿; 2--修复数据 (不能用于打开07)
            workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, 
                    new Object[] { sourceFile.getAbsolutePath(), 
                        Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull,
                        Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull,
                        Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, 1
            }, new int[1]).toDispatch();
            
            // 另保存为Excel, 50-xlsb
            Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { targetFile.getAbsolutePath(), new Variant(50) }, new int[1]);
 
        }
        catch (Exception e) {
            logger.debug(e);
            throw new RuntimeException(e);
        }
        finally {
            try {
                Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
            }
            catch (Exception ex) {
            }
 
            try {
                xl.invoke("Quit", new Variant[] {});
            }
            catch (Exception ex) {
            }
 
            ComThread.Release();
        }
    }
    
    public static void main(String[] args) {
        File sourceFile = new File("D:\\test\\prod.xlsx");
        File targetFile = new File("D:\\test\\test2.xlsx");
        
        if (!sourceFile.exists()) {
            System.out.println("file not exist:" + sourceFile);
            return;
        }
        
        if (targetFile.exists() && targetFile.canRead()) {
            targetFile.delete();
        }
        
        ComThread.InitSTA();
        try {
            ActiveXComponent xl = new ActiveXComponent("Excel.Application");
            try {
                Object xlo = xl.getObject();
                Dispatch workbooks = null; Dispatch workbook = null;
                
                try {
                    // 不显示Excel
                    System.out.println("version=" + xl.getProperty("Version"));  
                    System.out.println("version=" + Dispatch.get((Dispatch) xlo, "Version"));                
                    xl.setProperty("Visible", new Variant(true));
                    // 不提示警告等信息
                    xl.setProperty("DisplayAlerts", new Variant(false));
 
                    workbooks = xl.getProperty("Workbooks").toDispatch();
                    workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,  
                               new Object[] { sourceFile.getAbsolutePath(),  
                             Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull,
                             Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull,
                             Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, Variant.VariantNull, 1
                    }, new int[1]).toDispatch();
//                    workbook = Dispatch.call(workbooks, "Open", sourceFile.getAbsolutePath()).toDispatch();
                    // 另保存为Excel, 18-2003, 51-2007
                    Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { targetFile.getAbsolutePath(), new Variant(51) }, new int[1]);
                    System.err.println("ok!");
                }
                finally {
                    try {
                        Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
                    }
                    catch (Exception ex) {
                    }                    
                }
            }
            finally {
                try {
                    xl.invoke("Quit", new Variant[] {});
                }
                catch (Exception ex) {
                }
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            ComThread.Release();
        }
        System.err.println("done!");
    }
}