-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHWPFTest.java
More file actions
80 lines (73 loc) · 3 KB
/
Copy pathHWPFTest.java
File metadata and controls
80 lines (73 loc) · 3 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class HWPFTest {
public static void doStuff(){
String filePath = "D:\\insiders_report4.doc";
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream(filePath));
HWPFDocument doc = new HWPFDocument(fs);
doc = replaceText(doc, "#FIO#", "Молдабеков Нурлан Амиржанович");
doc = replaceText(doc, "#BIN#", "900524300077");
doc = replaceText(doc, "#INCDAY#", "05");
doc = replaceText(doc, "#INCMONTH#", "марта");
doc = replaceText(doc, "#INCYEAR#", "2016");
doc = replaceText(doc, "#EXCDAY#", "05");
doc = replaceText(doc, "#EXCMONTH#", "марта");
doc = replaceText(doc, "#EXCYEAR#", "2016");
doc = replaceText(doc, "#MAINCHIEF#", "Эгглтон Майкл Джеймс");
saveWord("D:\\result.doc", doc);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
private static HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText){
Range r1 = doc.getRange();
for (int i = 0; i < r1.numSections(); ++i ) {
Section s = r1.getSection(i);
for (int x = 0; x < s.numParagraphs(); x++) {
Paragraph p = s.getParagraph(x);
/*String text = p.text();
if(text.contains(findText)) {
p.replaceText(replaceText, findText);
}*/
for (int z = 0; z < p.numCharacterRuns(); z++) {
CharacterRun run = p.getCharacterRun(z);
String text = run.text();
if(text.contains(findText)) {
run.replaceText(findText, replaceText);
}
}
}
}
return doc;
}
private static void saveWord(String filePath, HWPFDocument doc) throws FileNotFoundException, IOException{
FileOutputStream out = null;
try{
out = new FileOutputStream(filePath);
doc.write(out);
}
finally{
out.close();
}
}
}