java簡單操作word執行個體_java

來源:互聯網
上載者:User

本文為大家分享了java簡單操作word例子,供大家參考,具體內容如下

package apache.poi;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.usermodel.Range;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class ExportDocTest {    public static void main(String[] args) {    String destFile="D:\\11.doc";    //#####################根據自訂內容匯出Word文檔#################################################    StringBuffer fileCon=new StringBuffer();    fileCon.append("        張大炮      男       317258963215223\n" +        "2011   09    2013   07    3\n" +        "  二炮研究       成人\n" +        "2013000001               2013   07   08");    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");        new ExportDocTest().exportDoc(destFile, fileCon.toString());        //##################根據Word模板匯出單個Word文檔###################################################    Map<String, String> map=new HashMap<String, String>();        map.put("name", "Zues");    map.put("sex", "男");    map.put("idCard", "200010");    map.put("year1", "2000");    map.put("month1", "07");    map.put("year2", "2008");    map.put("month2", "07");    map.put("gap", "2");    map.put("zhuanye", "電腦科學與技術");    map.put("type", "研究生");    map.put("bianhao", "2011020301");    map.put("nowy", "2011");    map.put("nowm", "01");    map.put("nowd", "20220301");    //注意biyezheng_moban.doc文檔位置,此例中為應用根目錄    HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map);    ByteArrayOutputStream ostream = new ByteArrayOutputStream();    try {      document.write(ostream);      //輸出word檔案      OutputStream outs=new FileOutputStream(destFile);      outs.write(ostream.toByteArray());      outs.close();    } catch (IOException e) {      e.printStackTrace();    }      }      /**   *    * @param destFile   * @param fileCon   */  public void exportDoc(String destFile,String fileCon){    try {      //doc content      ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes());      POIFSFileSystem fs = new POIFSFileSystem();      DirectoryEntry directory = fs.getRoot();       directory.createDocument("WordDocument", bais);      FileOutputStream ostream = new FileOutputStream(destFile);      fs.writeFilesystem(ostream);      bais.close();      ostream.close();          } catch (IOException e) {      e.printStackTrace();    }  }      /**   * 讀取word模板並替換變數   * @param srcPath   * @param map   * @return   */  public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {    try {      // 讀取word模板      FileInputStream fis = new FileInputStream(new File(srcPath));      HWPFDocument doc = new HWPFDocument(fis);      // 讀取word常值內容      Range bodyRange = doc.getRange();      // 替換常值內容      for (Map.Entry<String, String> entry : map.entrySet()) {        bodyRange.replaceText("${" + entry.getKey() + "}", entry            .getValue());      }      return doc;    } catch (Exception e) {      e.printStackTrace();      return null;    }  }}

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.