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数据
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(); } }