zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    CF1394A Boboniu Chats with Du 题解
    P3377 【模板】左偏树(可并堆)题解
    P2152 [SDOI2009]SuperGCD 题解
    在其他模块中调用代码
    教程:创建Go模块
    Go入门
    反悔贪心
    codeforces 1569 E. Playoff Restoration (meet-in-the-middle)
    codeforces 1036 F. Relatively Prime Powers (容斥+精度处理+大数边界处理)
    icpc沈阳2020 H. The Boomsday Project (dp+二分)
  • 原文地址:https://www.cnblogs.com/wl1202/p/13389987.html
Copyright © 2011-2022 走看看