栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java简单操作word实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java简单操作word实例

本文为大家分享了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(" 张大炮      男317258963215223n" +
 "2011   09    2013   07    3n" +
 "  二炮研究成人n" +
 "2013000001 2013   07   08");
    fileCon.append("nrnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
    
    new ExportDocTest().exportDoc(destFile, fileCon.toString());
    
    //##################根据Word模板导出单个Word文档###################################################
    Map map=new HashMap();
    
    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();
    }
    
  }
  
  
  
  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();
    }
  }
  
  
  
  public HWPFdocument replaceDoc(String srcPath, Map map) {
    try {
      // 读取word模板
      FileInputStream fis = new FileInputStream(new File(srcPath));
      HWPFdocument doc = new HWPFdocument(fis);
      // 读取word文本内容
      Range bodyRange = doc.getRange();
      // 替换文本内容
      for (Map.Entry entry : map.entrySet()) {
 bodyRange.replaceText("${" + entry.getKey() + "}", entry
     .getValue());
      }
      return doc;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/150462.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号