Java POI Wordドキュメントを書く
15072 ワード
Apache POIを使用してwordドキュメントを書く例:
1 package apache.poi;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import org.apache.poi.hwpf.HWPFDocument;
14 import org.apache.poi.hwpf.usermodel.Range;
15 import org.apache.poi.poifs.filesystem.DirectoryEntry;
16 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
17
18
19 public class ExportDocTest {
20
21 public static void main(String[] args) {
22 String destFile="D:\\11.doc";
23 //##################### Word #################################################
24 StringBuffer fileCon=new StringBuffer();
25 fileCon.append(" 317258963215223
" +
26 "2011 09 2013 07 3
" +
27 "
" +
28 "2013000001 2013 07 08");
29 fileCon.append("
\r
");
30
31 new ExportDocTest().exportDoc(destFile, fileCon.toString());
32
33 //################## Word Word ###################################################
34 Map<String, String> map=new HashMap<String, String>();
35
36 map.put("name", "Zues");
37 map.put("sex", " ");
38 map.put("idCard", "200010");
39 map.put("year1", "2000");
40 map.put("month1", "07");
41 map.put("year2", "2008");
42 map.put("month2", "07");
43 map.put("gap", "2");
44 map.put("zhuanye", " ");
45 map.put("type", " ");
46 map.put("bianhao", "2011020301");
47 map.put("nowy", "2011");
48 map.put("nowm", "01");
49 map.put("nowd", "20220301");
50 // biyezheng_moban.doc ,
51 HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map);
52 ByteArrayOutputStream ostream = new ByteArrayOutputStream();
53 try {
54 document.write(ostream);
55 // word
56 OutputStream outs=new FileOutputStream(destFile);
57 outs.write(ostream.toByteArray());
58 outs.close();
59 } catch (IOException e) {
60 e.printStackTrace();
61 }
62
63 }
64
65
66 /**
67 *
68 * @param destFile
69 * @param fileCon
70 */
71 public void exportDoc(String destFile,String fileCon){
72 try {
73 //doc content
74 ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes());
75 POIFSFileSystem fs = new POIFSFileSystem();
76 DirectoryEntry directory = fs.getRoot();
77 directory.createDocument("WordDocument", bais);
78 FileOutputStream ostream = new FileOutputStream(destFile);
79 fs.writeFilesystem(ostream);
80 bais.close();
81 ostream.close();
82
83 } catch (IOException e) {
84 e.printStackTrace();
85 }
86 }
87
88
89 /**
90 * word
91 * @param srcPath
92 * @param map
93 * @return
94 */
95 public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {
96 try {
97 // word
98 FileInputStream fis = new FileInputStream(new File(srcPath));
99 HWPFDocument doc = new HWPFDocument(fis);
100 // word
101 Range bodyRange = doc.getRange();
102 //
103 for (Map.Entry<String, String> entry : map.entrySet()) {
104 bodyRange.replaceText("${" + entry.getKey() + "}", entry
105 .getValue());
106 }
107 return doc;
108 } catch (Exception e) {
109 e.printStackTrace();
110 return null;
111 }
112 }
113
114 }
例で使用した添付ファイル(ダウンロードをクリック)