Jacob操作office文档WordPPTExcel.docx
- 文档编号:23164584
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:18
- 大小:20.48KB
Jacob操作office文档WordPPTExcel.docx
《Jacob操作office文档WordPPTExcel.docx》由会员分享,可在线阅读,更多相关《Jacob操作office文档WordPPTExcel.docx(18页珍藏版)》请在冰豆网上搜索。
Jacob操作office文档WordPPTExcel
Jacob操作office文档(Word,PPT,Excel)
jacob操作word
1.public boolean doc2pdf(String srcFilePath, String pdfFilePath) {
2. ActiveXComponent app = null;
3. Dispatch doc = null;
4. try {
5. ComThread.InitSTA();
6. app = new ActiveXComponent("Word.Application");
7. app.setProperty("Visible", false);
8. Dispatch docs = app.getProperty("Documents").toDispatch();
9. doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
10. new Object[] { srcFilePath,
11. new Variant(false),
12. new Variant(true),//是否只读
13. new Variant(false),
14. new Variant("pwd") },
15. new int[1]).toDispatch();
16.// Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
17. Dispatch.put(doc, "RemovePersonalInformation", false);
18. Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, wdFormatPDF); // word保存为pdf格式宏,值为17
19.
20. return true; // set flag true;
21. } catch (ComFailException e) {
22. return false;
23. } catch (Exception e) {
24. return false;
25. } finally {
26. if (doc !
= null) {
27. Dispatch.call(doc, "Close", false);
28. }
29. if (app !
= null) {
30. app.invoke("Quit", 0);
31. }
32. ComThread.Release();
33. }
34. }
Jacob操作ppt
1.public boolean ppt2pdf(String srcFilePath, String pdfFilePath) {
2. ActiveXComponent app = null;
3. Dispatch ppt = null;
4. try {
5. ComThread.InitSTA();
6. app = new ActiveXComponent("PowerPoint.Application");
7. Dispatch ppts = app.getProperty("Presentations").toDispatch();
8.
9. // 因POWER.EXE的发布规则为同步,所以设置为同步发布
10. ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly
11. true,// Untitled指定文件是否有标题
12. false// WithWindow指定文件是否可见
13. ).toDispatch();
14.
15. Dispatch.call(ppt, "SaveAs", pdfFilePath, ppSaveAsPDF); //ppSaveAsPDF为特定值32
16.
17. return true; // set flag true;
18. } catch (ComFailException e) {
19. return false;
20. } catch (Exception e) {
21. return false;
22. } finally {
23. if (ppt !
= null) {
24. Dispatch.call(ppt, "Close");
25. }
26. if (app !
= null) {
27. app.invoke("Quit");
28. }
29. ComThread.Release();
30. }
31. }
Jacob操作Excel
1.package com;
2.import java.io.ObjectInputStream.GetField;
3.import java.util.ArrayList;
4.import java.util.Date;
5.import java.util.List;
6.
7.import com.jacob.activeX.ActiveXComponent;
8.import .ComThread;
9.import .Dispatch;
10.import .Variant;
11.
12.
13.public class ready {
14. private static ActiveXComponent xl = null; //Excel对象(防止打开多个)
15. private static Dispatch workbooks = null; //工作簿对象
16. private Dispatch workbook = null; //具体工作簿
17. private Dispatch sheets = null;// 获得sheets集合对象
18. private Dispatch currentSheet = null;// 当前sheet
19. /**
20. * 打开excel文件
21. * @param filepath 文件路径名称
22. * @param visible 是否显示打开
23. * @param readonly 是否只读方式打开
24. */
25. private void OpenExcel(String filepath, boolean visible) {
26. try {
27. initComponents(); //清空原始变量
28. ComThread.InitSTA();
29. if(xl==null)
30. xl = new ActiveXComponent("Excel.Application"); //Excel对象
31. xl.setProperty("Visible", new Variant(visible));//设置是否显示打开excel
32. if(workbooks==null)
33. workbooks = xl.getProperty("Workbooks").toDispatch(); //打开具体工作簿
34. workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
35. new Object[] { srcFilePath,
36. new Variant(false), // 是否以只读方式打开
37. new Variant(true),
38. "1",
39. "pwd" }, //输入密码"pwd",若有密码则进行匹配,无则直接打开
40. new int[1]).toDispatch();
41. } catch (Exception e) {
42. e.printStackTrace();
43. releaseSource();
44. }
45. }
46. /**
47. * 工作簿另存为
48. * @param filePath 另存为的路径
49. * 例如 SaveAs="D:
TEST/c.xlsx"
50. */
51. private void SaveAs(String filePath){
52. Dispatch.call(workbook, "SaveAs",filePath);
53. }
54. /**
55. * 关闭excel文档
56. * @param f 含义不明 (关闭是否保存?
默认false)
57. */
58. private void CloseExcel(boolean f) {
59. try {
60. Dispatch.call(workbook, "Save");
61. Dispatch.call(workbook, "Close", new Variant(f));
62. } catch (Exception e) {
63. e.printStackTrace();
64. } finally {
65. releaseSource();
66. }
67. }
68. /*
69. * 初始化
70. * */
71. private void initComponents(){
72. workbook = null;
73. currentSheet = null;
74. sheets = null;
75. }
76. /**
77. * 释放资源
78. */
79. private static void releaseSource(){
80. if(xl!
=null){
81. xl.invoke("Quit", new Variant[] {});
82. xl = null;
83. }
84. workbooks = null;
85. ComThread.Release();
86. System.gc();
87. }
88. /**
89. * 得到当前sheet
90. * @return
91. */
92. private Dispatch getCurrentSheet() {
93. currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
94. return currentSheet;
95. }
96. /**
97. * 修改当前工作表的名字
98. * @param newName
99. */
100. private void modifyCurrentSheetName(String newName) {
101. Dispatch.put(getCurrentSheet(), "name", newName);
102. }
103.
104. /**
105. * 得到当前工作表的名字
106. * @return
107. */
108. private String getCurrentSheetName(Dispatch sheets) {
109. return Dispatch.get(sheets, "name").toString();
110. }
111. /**
112. * 通过工作表名字得到工作表
113. * @param name sheetName
114. * @return
115. */
116. private Dispatch getSheetByName(String name) {
117. return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{name}, new int[1]).toDispatch();
118. }
119. /**
120. * 得到sheets的集合对象
121. * @return
122. */
123. private Dispatch getSheets() {
124. if(sheets==null)
125. sheets = Dispatch.get(workbook, "sheets").toDispatch();
126. return sheets;
127. }
128. /**
129. * 通过工作表索引得到工作表(第一个工作簿index为1)
130. * @param index
131. * @return sheet对象
132. */
133. private Dispatch getSheetByIndex(Integer index) {
134. return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{index}, new int[1]).toDispatch();
135. }
136.
137. /**
138. * 得到sheet的总数
139. * @return
140. */
141. private int getSheetCount() {
142. int count = Dispatch.get(getSheets(), "count").toInt();
143. return count;
144. }
145. /**
146. * 给所有的sheet添加背景
147. * @param filepath 图片路径
148. */
149. public void setBlackGroudPrituce(String filepath)
150. {
151. int num=this.getSheetCount();
152. for (int i = 1; i <= num; i++) {
153. Dispatch sheets=this.getSheetByIndex(i);
154. Dispatch.call(sheets,"SetBackgroundPicture",filepath);
155. }
156. }
157. /**
158. * 添加新的工作表(sheet),并且隐藏(添加后为默认为当前激活的工作表)
159. */
160. public void addSheet(String name) {
161.// for (int i = 1; i <= this.getSheetCount(); i++) {
162.// Dispatch sheets=this.getSheetByIndex(i);
163.// if(name.equals(this.getCurrentSheetName(sheets)))
164.// {
165.// return false;
166.// }
167.// }
168. currentSheet=Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add").toDispatch();
169. // Dispatch.put(currentSheet,"Name",name);
170. Dispatch.put(currentSheet, "Visible", new Boolean(false));
171. System.out.println("插入信息为:
"+name);
172. }
173. /**
174. * 得到工作薄的名字
175. * @return
176. */
177. private String getWorkbookName() {
178. if(workbook==null)
179. return null;
180. return Dispatch.get(workbook, "name").toString();
181. }
182. /**
183. * 获取所有表名
184. */
185. public List findSheetName()
186. {
187. int num=this.getSheetCount();
188. List list=new ArrayList();
189. for (int i = 1; i <= num; i++) {
190.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Jacob 操作 office 文档 WordPPTExcel