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 }

例子中用到的附件(點擊下載)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM