国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java簡(jiǎn)單操作word實(shí)例

2019-11-26 14:31:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文為大家分享了java簡(jiǎn)單操作word例子,供大家參考,具體內(nèi)容如下

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";    //#####################根據(jù)自定義內(nèi)容導(dǎo)出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());        //##################根據(jù)Word模板導(dǎo)出單個(gè)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", "計(jì)算機(jī)科學(xué)與技術(shù)");    map.put("type", "研究生");    map.put("bianhao", "2011020301");    map.put("nowy", "2011");    map.put("nowm", "01");    map.put("nowd", "20220301");    //注意biyezheng_moban.doc文檔位置,此例中為應(yīng)用根目錄    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文本內(nèi)容      Range bodyRange = doc.getRange();      // 替換文本內(nèi)容      for (Map.Entry<String, String> entry : map.entrySet()) {        bodyRange.replaceText("${" + entry.getKey() + "}", entry            .getValue());      }      return doc;    } catch (Exception e) {      e.printStackTrace();      return null;    }  }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临潭县| 沈阳市| 鲁山县| 化隆| 霍城县| 安龙县| 米易县| 石首市| 开封市| 菏泽市| 石林| 合江县| 石泉县| 贵港市| 原阳县| 佛坪县| 白玉县| 南木林县| 定结县| 清苑县| 儋州市| 鸡东县| 铜川市| 遂平县| 遂宁市| 宝应县| 绥棱县| 东阳市| 大渡口区| 柳州市| 嘉黎县| 长子县| 临汾市| 罗城| 资中县| 浑源县| 漳浦县| 凤山县| 凤山县| 文昌市| 繁昌县|