• 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();
            }
        }
  • 相关阅读:
    linux信号
    APM浅析
    Jackson高并发情况下,产生阻塞
    [转]slf4j 与log4j 日志管理
    2015-09-27 git学习
    MySQL学习笔记-锁相关话题
    MySQL学习笔记-MySQL数据库优化实践[转]
    MySQL学习笔记-事务相关话题
    几个关于网站架构和性能的问题(我在知乎上的问答)
    MySQL学习笔记-数据库文件
  • 原文地址:https://www.cnblogs.com/wl1202/p/13389987.html
Copyright © 2020-2023  润新知