Java POI Word 写文档

原文地址为: Java POI Word 写文档

一个使用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\n" +
 26                 "2011     09        2013     07       3\n" +
 27                 "    二炮研究              成人\n" +
 28                 "2013000001                             2013     07     08");
 29         fileCon.append("\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
 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 }

例子中用到的附件(点击下载)


转载请注明本文地址: Java POI Word 写文档

猜你喜欢

转载自blog.csdn.net/zhengxiuchen86/article/details/81742377