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

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());
//例如:range.replaceText("${辰辰}", entry.getValue());就是用map的value值代替word中的${辰辰} ,${} 作为标识符,可以改成自己需要的字符,
//用于辨别word中需要修改的内容的位置
        }
        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);
            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;
    }




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值