最近转换的需求比较多,最近做了一个Word转Html的
这个要导一个包和配置一个文件
1.jacob.jar
2.与jacob.jar相对应的jacob.dll(放在windows/sys32下或者放在jre下面)
代码
1 package test;
2
3 import java.io.File;
4
5 import com.jacob.activeX.ActiveXComponent;
6 import com.jacob.com.Dispatch;
7 import com.jacob.com.Variant;
8
9 public class WordToHtml {
10
11 int WORD_HTML = 8;
12 int WORD_TXT = 7;
13 int EXCEL_HTML = 44;
14
15 /**
16 * WORD转HTML
17 *
18 * @param docfile
19 * WORD文件全路径
20 * @param htmlfile
21 * 转换后HTML存放路径
22 */
23 public void wordToHtml(String docfile, String htmlfile) {
24 ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
25 try {
26 app.setProperty("Visible", new Variant(false));
27 Dispatch docs = app.getProperty("Documents").toDispatch();
28 Dispatch doc = Dispatch.invoke(
29 docs,
30 "Open",
31 Dispatch.Method,
32 new Object[] { docfile, new Variant(false),
33 new Variant(true) }, new int[1]).toDispatch();
34 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
35 htmlfile, new Variant(WORD_HTML) }, new int[1]);
36 Variant f = new Variant(false);
37 Dispatch.call(doc, "Close", f);
38 } catch (Exception e) {
39 e.printStackTrace();
40 } finally {
41 app.invoke("Quit", new Variant[] {});
42 }
43 }
44
45 /**
46 * EXCEL转HTML
47 *
48 * @param xlsfile
49 * EXCEL文件全路径
50 * @param htmlfile
51 * 转换后HTML存放路径
52 */
53 public void excelToHtml(String xlsfile, String htmlfile) {
54 ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
55 try {
56 app.setProperty("Visible", new Variant(false));
57 Dispatch excels = app.getProperty("Workbooks").toDispatch();
58 Dispatch excel = Dispatch.invoke(
59 excels,
60 "Open",
61 Dispatch.Method,
62 new Object[] { xlsfile, new Variant(false),
63 new Variant(true) }, new int[1]).toDispatch();
64 Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
65 htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
66 Variant f = new Variant(false);
67 Dispatch.call(excel, "Close", f);
68 System.out.println("wordtohtml转换成功");
69 } catch (Exception e) {
70 e.printStackTrace();
71 } finally {
72 app.invoke("Quit", new Variant[] {});
73 }
74 }
75
76 /**
77 * /删除指定文件夹
78 *
79 * @param folderPath
80 * 文件夹全路径
81 * @param htmlfile
82 * 转换后HTML存放路径
83 */
84 public void delFolder(String folderPath) {
85 try {
86 delAllFile(folderPath); // 删除完里面所有内容
87 String filePath = folderPath;
88 filePath = filePath.toString();
89 java.io.File myFilePath = new java.io.File(filePath);
90 myFilePath.delete(); // 删除空文件夹
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94 }
95
96 /**
97 * /删除指定文件夹下所有文件
98 *
99 * @param path
100 * 文件全路径
101 */
102 public boolean delAllFile(String path) {
103 boolean flag = false;
104 File file = new File(path);
105 if (!file.exists()) {
106 return flag;
107 }
108 if (!file.isDirectory()) {
109 return flag;
110 }
111 String[] tempList = file.list();
112 File temp = null;
113 for (int i = 0; i < tempList.length; i++) {
114 if (path.endsWith(File.separator)) {
115 temp = new File(path + tempList[i]);
116 } else {
117 temp = new File(path + File.separator + tempList[i]);
118 }
119 if (temp.isFile()) {
120 temp.delete();
121 }
122 if (temp.isDirectory()) {
123 delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
124 delFolder(path + "/" + tempList[i]);// 再删除空文件夹
125 flag = true;
126 }
127 }
128 return flag;
129 }
130
131 public static void main(String[] args) {
132 // TODO Auto-generated method stub
133 WordToHtml wordtohtml = new WordToHtml();
134 wordtohtml.wordToHtml("D:\\test.doc", "D:\\test.html");
135 System.out.println("word转html成功");
136 }
137 }
138
2
3 import java.io.File;
4
5 import com.jacob.activeX.ActiveXComponent;
6 import com.jacob.com.Dispatch;
7 import com.jacob.com.Variant;
8
9 public class WordToHtml {
10
11 int WORD_HTML = 8;
12 int WORD_TXT = 7;
13 int EXCEL_HTML = 44;
14
15 /**
16 * WORD转HTML
17 *
18 * @param docfile
19 * WORD文件全路径
20 * @param htmlfile
21 * 转换后HTML存放路径
22 */
23 public void wordToHtml(String docfile, String htmlfile) {
24 ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
25 try {
26 app.setProperty("Visible", new Variant(false));
27 Dispatch docs = app.getProperty("Documents").toDispatch();
28 Dispatch doc = Dispatch.invoke(
29 docs,
30 "Open",
31 Dispatch.Method,
32 new Object[] { docfile, new Variant(false),
33 new Variant(true) }, new int[1]).toDispatch();
34 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
35 htmlfile, new Variant(WORD_HTML) }, new int[1]);
36 Variant f = new Variant(false);
37 Dispatch.call(doc, "Close", f);
38 } catch (Exception e) {
39 e.printStackTrace();
40 } finally {
41 app.invoke("Quit", new Variant[] {});
42 }
43 }
44
45 /**
46 * EXCEL转HTML
47 *
48 * @param xlsfile
49 * EXCEL文件全路径
50 * @param htmlfile
51 * 转换后HTML存放路径
52 */
53 public void excelToHtml(String xlsfile, String htmlfile) {
54 ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
55 try {
56 app.setProperty("Visible", new Variant(false));
57 Dispatch excels = app.getProperty("Workbooks").toDispatch();
58 Dispatch excel = Dispatch.invoke(
59 excels,
60 "Open",
61 Dispatch.Method,
62 new Object[] { xlsfile, new Variant(false),
63 new Variant(true) }, new int[1]).toDispatch();
64 Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
65 htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
66 Variant f = new Variant(false);
67 Dispatch.call(excel, "Close", f);
68 System.out.println("wordtohtml转换成功");
69 } catch (Exception e) {
70 e.printStackTrace();
71 } finally {
72 app.invoke("Quit", new Variant[] {});
73 }
74 }
75
76 /**
77 * /删除指定文件夹
78 *
79 * @param folderPath
80 * 文件夹全路径
81 * @param htmlfile
82 * 转换后HTML存放路径
83 */
84 public void delFolder(String folderPath) {
85 try {
86 delAllFile(folderPath); // 删除完里面所有内容
87 String filePath = folderPath;
88 filePath = filePath.toString();
89 java.io.File myFilePath = new java.io.File(filePath);
90 myFilePath.delete(); // 删除空文件夹
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94 }
95
96 /**
97 * /删除指定文件夹下所有文件
98 *
99 * @param path
100 * 文件全路径
101 */
102 public boolean delAllFile(String path) {
103 boolean flag = false;
104 File file = new File(path);
105 if (!file.exists()) {
106 return flag;
107 }
108 if (!file.isDirectory()) {
109 return flag;
110 }
111 String[] tempList = file.list();
112 File temp = null;
113 for (int i = 0; i < tempList.length; i++) {
114 if (path.endsWith(File.separator)) {
115 temp = new File(path + tempList[i]);
116 } else {
117 temp = new File(path + File.separator + tempList[i]);
118 }
119 if (temp.isFile()) {
120 temp.delete();
121 }
122 if (temp.isDirectory()) {
123 delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
124 delFolder(path + "/" + tempList[i]);// 再删除空文件夹
125 flag = true;
126 }
127 }
128 return flag;
129 }
130
131 public static void main(String[] args) {
132 // TODO Auto-generated method stub
133 WordToHtml wordtohtml = new WordToHtml();
134 wordtohtml.wordToHtml("D:\\test.doc", "D:\\test.html");
135 System.out.println("word转html成功");
136 }
137 }
138
每种功能的实现方法有很多,希望各位可以交流不同的思想和方法。可以加QQ412546724。呵呵