Java使用word模板生成多个word文件,并导出一个zip压缩包
程序员文章站
2022-07-12 12:31:35
...
生成word方法可以参考
链接: https://blog.csdn.net/qq_40977118/article/details/106918521.
这里需要修改controller,将循环生成的word下载到指定文件夹下,然后将完全限定名放入list中,最后将list中所有文件打成zip包下载
package com.example.demo.controller;
import ch.qos.logback.core.util.FileUtil;
import com.example.demo.util.CustomXWPFDocument;
import com.example.demo.util.WordUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@RestController
public class ExportWordController {
@RequestMapping(value = "/export")
public void export(HttpServletRequest request,HttpServletResponse response) throws Exception{
WordUtil xwpfTUtil = null;
OutputStream os = null;
InputStream is = null;
List<String> files = new ArrayList();
for (int i = 0; i < 2; i++) {
xwpfTUtil = new WordUtil();
Map<String, Object> params = new HashMap<>();
params.put("${Name}", "Fisher3652");
params.put("${Sex}", "男");
params.put("${Desc}", "18岁\tJAVA开发\r熟悉JVM基本原理");
params.put("${@Pic}", "C:\\Users\\111\\Pictures\\123.jpg");
CustomXWPFDocument doc;
is = FileUtil.class.getClassLoader().getResourceAsStream("static/Demo.docx");
doc = new CustomXWPFDocument(is);
xwpfTUtil.replaceInPara(doc, params);
xwpfTUtil.replaceInTable(doc, params);
String path = request.getSession().getServletContext().getContextPath() + "/word";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String fileName = request.getSession().getServletContext().getContextPath() + "/word/" + i + ".docx";
os = new FileOutputStream(new File(fileName));
files.add(fileName);
doc.write(os);
System.out.println("word成功生成");
}
xwpfTUtil.close(os);
xwpfTUtil.close(is);
os.flush();
os.close();
writeZip(files, "文件汇总");
}
private void writeZip(List<String> files, String zipname) throws IOException {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
String fileName = zipname + ".zip";
OutputStream os = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
ZipOutputStream zos = new ZipOutputStream(os);
byte[] buf = new byte[8192];
int len;
for (int i = 0; i < files.size(); i++) {
File file = new File(files.get(i));
if (!file.isFile()) continue;
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
while ((len = bis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
}
zos.closeEntry();
zos.close();
}
}
导出结果