打开APP
userphoto
未登录

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

开通VIP
java word操作

1.java复制文件

savepath:D:\\

Files.copy(Paths.get(savePath+"mould\\mould.doc"), new FileOutputStream(savePath+"dayplan\\1111.doc"));

2.调用本地office打开word

Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler "  + "d:\\dayplan\\1111.doc");

3.替换word数据

详见:java修改Word文档内容

pom:

 <!--poi支持-->
        <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi</artifactId>
         <version>3.10-FINAL</version>
        </dependency>
        <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi-ooxml</artifactId>
         <version>3.10-FINAL</version>
        </dependency>

 

Map map=new HashMap();
            map.put("name","admin");//${name}
            map.put("age","24");//${age}
            map.put("sex","男");//${sex}
            CreatWordByModel("f:/test.doc",map,"f:/test2.doc");

 

public static void CreatWordByModel(String  tmpFile, Map<String, String> contentMap, String exportFile) throws Exception{

        InputStream in = null;
        in = new FileInputStream(new File(tmpFile));

        HWPFDocument document = null;
        document = new HWPFDocument(in);
        // 读取文本内容
        Range bodyRange = document.getRange();
        System.out.println(bodyRange.toString());
        System.out.println(bodyRange.text());
        // 替换内容
        for (Map.Entry<String, String> entry : contentMap.entrySet()) {
            bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
        }

        //导出到文件
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            document.write((OutputStream)byteArrayOutputStream);
            OutputStream outputStream = new FileOutputStream(exportFile);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java解析json生成excel
java使用poi读取doc和docx文件
使用POI操作Excel和Word
Java Batik操作SVG,实现svg读取,生成,动态操作
Java生产验证码各种工具类
Java爬虫入门
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服