一 maven引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
二 代码
String inputUrl = "src/main/resources/dataTempl.docx";
String outputUrl = "C:\\Users\\"+userid+"\\Desktop\\数据-"+customerOrg.toString()+"-"+incidentNumber.toString()+".docx";
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("name", customerOrg.toString());
testMap.put("number", incidentNumber.toString());
testMap.put("event", problemSummary.toString());
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
testMap.put("time", df.format(new Date()).toString());
List<String[]> testList = new ArrayList<String[]>();
boolean changeResult = wordUtils.changWord(inputUrl, outputUrl, testMap, testList);
if(changeResult){
new Alert(Alert.AlertType.INFORMATION,"生产成功,请在桌面查看").showAndWait();
try {
String[] cmd = new String[5];
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = outputUrl;
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}else{
new Alert(Alert.AlertType.INFORMATION,"出现异常,请关闭word文件后重试").show();
}