打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java:读取并修改word中的内容并保存。

1.读取word内容

2.修改要修改的内容

3.保存word

  1. public Map readwriteWord(Map<String, String> map,String gcid,String deptid) {  
  2.     //查找word模板  
  3.         BindContractTemplate bindContractTemplate = new BindContractTemplate();  
  4.         bindContractTemplate.setGCId(gcid);  
  5.         bindContractTemplate.setDeptId(deptid);  
  6.         bindContractTemplate = bindContractTemplateService.get(bindContractTemplate);  
  7.     //吧服务器的模板下载到本机中  
  8.         String photoUrl = bindContractTemplate.getContractTemplateUrl();  
  9.         String fileNames = photoUrl.substring(photoUrl.lastIndexOf("/"));  
  10.         String filePath = "/var/xxx/webserver/xxx/contract/";  
  11.         boolean flag = saveUrlAs(photoUrl, filePath +fileNames);  
  12.         // 读取word模板  
  13.         FileInputStream in = null;  
  14.         try {  
  15.             in = new FileInputStream(new File(filePath + fileNames));  
  16.             //in = new FileInputStream(new File("F:\\1495206705005.doc"));  
  17.         } catch (FileNotFoundException e1) {  
  18.             e1.printStackTrace();  
  19.         }  
  20.         HWPFDocument hdt = null;  
  21.         try {  
  22.             hdt = new HWPFDocument(in);  
  23.         } catch (IOException e1) {  
  24.             e1.printStackTrace();  
  25.         }  
  26.         Fields fields = hdt.getFields();  
  27.         Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN)  
  28.                 .iterator();  
  29.         while (it.hasNext()) {  
  30.             System.out.println(it.next().getType());  
  31.         }  
  32.         // 读取word文本内容  
  33.         Range range = hdt.getRange();  
  34.         // 替换文本内容  
  35.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  36.             if (entry.getValue() == null) {  
  37.                 entry.setValue("  ");  
  38.             }  
  39.             range.replaceText("${" + entry.getKey() + "}", entry.getValue());  
  40. //例如:range.replaceText("${辰辰}", entry.getValue());就是用map的value值代替word中的${辰辰} ,${} 作为标识符,可以改成自己需要的字符,  
  41. //用于辨别word中需要修改的内容的位置  
  42.         }  
  43.         ByteArrayOutputStream ostream = new ByteArrayOutputStream();  
  44.         String fileName = "" + System.currentTimeMillis();  
  45.         fileName += ".doc";  
  46.         FileOutputStream out = null;  
  47.         String path = "http://test.fq.pms.efanghang.com/"+fileName;  
  48.         String pathout = "/var/fangqian/webserver/qypms_boot_jar/contract/" + fileName;  
  49.         //String path = "f:\\"+fileName;  
  50.         try {  
  51.             out = new FileOutputStream("/var/fangqian/webserver/qypms_boot_jar/contract/" + fileName, true);  
  52.             e.printStackTrace();  
  53.         }  
  54.         try {  
  55.             hdt.write(ostream);  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.         // 输出字节流  
  60.         try {  
  61.             out.write(ostream.toByteArray());  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.         try {  
  66.             out.close();  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         try {  
  71.             ostream.close();  
  72.         } catch (IOException e) {  
  73.             e.printStackTrace();  
  74.         }  
  75.         Map mapout=new HashMap();  
  76.         mapout.put("path",path);  
  77.         mapout.put("pathout",pathout);  
  78.         System.out.println("修改word完啦");  
  79.         return mapout;  
  80.     }  




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java 操作word 使用POI3.7
Java读取FTP上的txt文件
java读写文件
java 追加写文件
静态HTML生成模式代码
FTP上传工具类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服