java:读取并修改word中的内容并保存。
来源:互联网 发布:dota妹子知乎 编辑:程序博客网 时间:2024/05/17 06:16
1.读取word内容
2.修改要修改的内容
3.保存word
public Map readwriteWord(Map<String, String> map,String gcid,String deptid) {//查找word模板 BindContractTemplate bindContractTemplate = new BindContractTemplate(); bindContractTemplate.setGCId(gcid); bindContractTemplate.setDeptId(deptid); bindContractTemplate = bindContractTemplateService.get(bindContractTemplate);//吧服务器的模板下载到本机中 String photoUrl = bindContractTemplate.getContractTemplateUrl(); String fileNames = photoUrl.substring(photoUrl.lastIndexOf("/")); String filePath = "/var/xxx/webserver/xxx/contract/"; boolean flag = saveUrlAs(photoUrl, filePath +fileNames); // 读取word模板 FileInputStream in = null; try { in = new FileInputStream(new File(filePath + fileNames)); //in = new FileInputStream(new File("F:\\1495206705005.doc")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } HWPFDocument hdt = null; try { hdt = new HWPFDocument(in); } catch (IOException e1) { e1.printStackTrace(); } Fields fields = hdt.getFields(); Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN) .iterator(); while (it.hasNext()) { System.out.println(it.next().getType()); } // 读取word文本内容 Range range = hdt.getRange(); // 替换文本内容 for (Map.Entry<String, String> entry : map.entrySet()) { if (entry.getValue() == null) { entry.setValue(" "); } range.replaceText("${" + entry.getKey() + "}", entry.getValue()); } ByteArrayOutputStream ostream = new ByteArrayOutputStream(); String fileName = "" + System.currentTimeMillis(); fileName += ".doc"; FileOutputStream out = null; String path = "http://test.fq.pms.efanghang.com/"+fileName; String pathout = "/var/fangqian/webserver/qypms_boot_jar/contract/" + fileName; //String path = "f:\\"+fileName; try { out = new FileOutputStream("/var/fangqian/webserver/qypms_boot_jar/contract/" + fileName, true); //out = new FileOutputStream(path, true); } catch (FileNotFoundException e) { e.printStackTrace(); } try { hdt.write(ostream); } catch (IOException e) { e.printStackTrace(); } // 输出字节流 try { out.write(ostream.toByteArray()); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { ostream.close(); } catch (IOException e) { e.printStackTrace(); } Map mapout=new HashMap(); mapout.put("path",path); mapout.put("pathout",pathout); System.out.println("修改word完啦"); return mapout; }
阅读全文
0 0
- java:读取并修改word中的内容并保存。
- java读取网页内容,并保存
- matlab读取txt文本内容修改并保存到txt
- java 读取并保存excel中的图片
- java 读取并保存excel中的图片
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- jcaob中读取word中的内容,并显示出来
- C#读取word文档内容并显示
- php读取word\pdf等文档的内容,并将其保存到网页中
- Java读取word中的内容
- XWPFDocument读取word并修改发送
- winform修改配置文件并保存,读取!
- 如何修改并读取StatusBar的内容
- 读取并修改元素的内容
- poi读取模板并修改模板内容
- java POI 修改 读取 word 内容
- 读取YU12文件中的Y分量内容,并一桢一桢的保存下来
- 项目实战:自定义View实现右上角更多,让页面更简洁
- HTML中引入CSS的方法
- 遗传算法(python版)
- vim中多标签和多窗口的使用
- Jackson的使用
- java:读取并修改word中的内容并保存。
- Android百分比布局(percent-support-lib)
- 安卓自定义刻度盘控件
- java获取获得Timestamp类型的当前系统时间。
- ASP.NET集成支付宝支付功能---即时支付
- 论文翻译:ViBe+算法(ViBe算法的改进版本)
- Hadooop运行WordCount(Hello world)程序
- HDU 1043 Eight A*算法+康托展开
- Mac pip 使用国内源 安装 scrapy