-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordString.java
More file actions
146 lines (128 loc) · 3.28 KB
/
WordString.java
File metadata and controls
146 lines (128 loc) · 3.28 KB
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
package utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* read wps/word2010+
*
* @author lah01
*
*/
public class WordString {
public static void main(String[] args) {
String str = extractDoc("d:/qwewqeqwe.docx",
"d:/java项目/hibernate项目/dcms/WebContent/file/1.txt");
System.out.println(str);
}
/**
* 把word文档转换为TXT格式的文档
*
* @param inputFile
* word文件的绝对路径
* @param outputFile
* 转化为TXT文件后的绝对路径
* @return 转化之后的TXT文件的字符串
*/
public static String extractDoc(String inputFile, String outputFile) {
String content = "";
ComThread.InitSTA();
// 打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
Dispatch doc2 = null;
try {
// 设置word不可见
app.setProperty("Visible", new Variant(false));
Dispatch doc1 = app.getProperty("Documents").toDispatch();// 所有文档窗口
doc2 = Dispatch.invoke(
doc1,
"Open",
Dispatch.Method,
new Object[] { inputFile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
// 作为TXT格式,保存到了临时文件
Dispatch.invoke(doc2, "SaveAs", Dispatch.Method, new Object[] {
outputFile, new Variant(7) }, new int[1]);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭Word
Variant f = new Variant(false);
Dispatch.call(doc2, "Close", f);// doc2关闭
app.invoke("Quit", new Variant[] {});// 退出word
ComThread.Release();
content = getContent(outputFile);
// WordString.delete(outputFile);
WordString.forceDelete(outputFile);
}
return content;
}
/**
* 删除文件
*
* @param file
* 需要删除的文件的路径
*/
// public static void delete(String file) {
// File afile = new File(file);
// // boolean bool = false;
// while(afile.exists())
// {
// afile.delete();
// // int i = 1 ;
// }
// }
/**
* 强力删除文件
*
* @param file
* 需要删除的文件的路径
*/
public static boolean forceDelete(String file) {
File f = new File(file);
boolean result = false;
int tryCount = 0;
while (!result && tryCount++ < 10) {
System.gc(); // System.gc()直接调用了系统内存回收
result = f.delete();
}
return result;
}
/**
* 取得TXT文件的内容
*
* @param file
* TXT文件的路径
* @return TXT文件中的字符串
*/
public static String getContent(String file) {
StringBuffer sb = new StringBuffer();
String temp = "";
String sb1 = "";
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(
file), "gbk");
BufferedReader bis = new BufferedReader(isr);
int i=0;
while ((temp = bis.readLine()) != null) {
if(i!=0)
sb.append("'");
sb.append(temp);
sb.append("'+");
sb.append("'<br>'\r\n");
sb.append("+");
i++;
}
sb.append("'");
sb1 = sb.toString().replace("//\\s+/"," ");
System.out.println(sb1);
} catch (Exception e) {
e.printStackTrace();
}
return sb1;
}
}