目录
一、先用word文档(.doc)制作xml模板
1.word文档如下:
2.在动态生成数据的表格中绑定工作域:
3.表格全部绑定成功后如下:
4.然后另存文件为xml文件:
5.将生成的xml模板放到项目resource下的template文件夹中
二、编写word工具类
代码如下:
public class DocUtils {
//参数:filePath 文件生成的地址;
// dataMap 渲染绑定域的数据;
// templatePath 模板地址;
public static void saveWord(String filePath, Map<String, Object> dataMap, String templatePath) throws IOException {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(DocUtils.class, "/");
Template template = configuration.getTemplate(templatePath);
InputStreamSource streamSource = createWord(template, dataMap);
InputStream inputStream = streamSource.getInputStream();
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] bytes = new byte[1024];
while ((inputStream.read(bytes)) != -1) {
outputStream.write(bytes);// 写入数据
}
inputStream.close();
outputStream.close();
}
public static InputStreamSource createWord(Template template, Map<String, Object> dataMap) {
StringWriter out = null;
Writer writer = null;
try {
out = new StringWriter();
writer = new BufferedWriter(out, 1024);
template.process(dataMap, writer);
return new ByteArrayResource(out.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void isExist(String path) {
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
}
}
调用saveWord方法传入对应参数即可生成对应的word文档
三、生成word文件方法以及保存到根目录方法
public void generateRessultFile(CompanyConfirmationRightsResult vo) {
try{
//1.通过传入的实体组装Map数据
Map dataMap = setResultMap(vo);
//2.生成到的根目录地址
ClassPathResource resource = new ClassPathResource("templates/mybatis/tradeResult/");
//3.文件地址
String filePath = resource.getPath() + DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSS") + "-" + "成交结果确认书.docx";
//4.文件名称
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
//5.文件地址是否存在,不存在新建目录
DocUtils.isExist(filePath);
//6.传入对应的文件地址、Map组装数据、xml模板地址
DocUtils.saveWord(filePath, dataMap,"templates/templateResult.xml");
}catch (IOException e){
e.printStackTrace();
}
}
//通过传入的实体组装Map数据
public Map setResultMap(CompanyConfirmationRightsResult vo){
Map<String, Object> map = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
map.put("projectNum",vo.getProjectNum());
map.put("transferName",vo.getTransferName());
map.put("transferCode",vo.getTransferCode());
map.put("assigneeName",vo.getAssigneeName());
map.put("assigneeCode",vo.getAssigneeCode());
map.put("transactionDate",simpleDateFormat.format(vo.getTransactionDate()));
map.put("changeType",vo.getChangeType());
map.put("n1",vo.getN1());
map.put("n2",vo.getN2());
map.put("n3",vo.getN3());
map.put("n4",vo.getN4());
map.put("n5",vo.getN5());
map.put("y1",vo.getY1());
map.put("y2",vo.getY2());
map.put("y3",vo.getY3());
map.put("y4",vo.getY4());
map.put("y5",vo.getY5());
map.put("c1",vo.getC1());
map.put("c2",vo.getC2());
map.put("c3",vo.getC3());
map.put("c4",vo.getC4());
map.put("c5",vo.getC5());
map.put("d1",vo.getD1());
map.put("d2",vo.getD2());
map.put("d3",vo.getD3());
map.put("d4",vo.getD4());
map.put("d5",vo.getD5());
map.put("year",vo.getYear());
map.put("month",vo.getMonth());
map.put("day",vo.getDay());
return map;
}
执行完之后就回看到项目多了一个文件夹 以及文件夹下面生成的word文档
四、生成之后使用完想要删除文件夹里的文件可以调用以下方法
//删除生成的文件
File files = new File(resource.getPath());
deleteFile(files);
public void deleteFile(File oldPath) {
if (oldPath.isDirectory()) {
System.out.println(oldPath + "是文件夹--");
File[] files = oldPath.listFiles();
for (File file : files) {
deleteFile(file);
}
} else {
oldPath.delete();
}
}