700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java使用word模板生成多个word文件 并导出一个zip压缩包

Java使用word模板生成多个word文件 并导出一个zip压缩包

时间:2022-05-02 07:06:00

相关推荐

Java使用word模板生成多个word文件 并导出一个zip压缩包

生成word方法可以参考

链接: /qq_40977118/article/details/106918521.

这里需要修改controller,将循环生成的word下载到指定文件夹下,然后将完全限定名放入list中,最后将list中所有文件打成zip包下载

package com.example.demo.controller;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .URLEncoder;import java.nio.charset.StandardCharsets;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;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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 com.example.demo.util.CustomXWPFDocument;import com.example.demo.util.WordUtil;import ch.qos.logback.core.util.FileUtil;/*** word导出*/@RestControllerpublic class ExportWordController {@RequestMapping(value = "/export")public void export(HttpServletRequest request) throws Exception{WordUtil xwpfTUtil = null;FileOutputStream 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 realPath = request.getSession().getServletContext().getRealPath("/");String parentPath = new File(realPath).getParent() + "/table" ;File dir = new File(parentPath);if (!dir.exists()) {dir.mkdirs();}String fileName = parentPath + "/" + i + ".docx";System.out.println(fileName);os = new FileOutputStream(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 {String fileName = zipname + ".zip";ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletResponse response = servletRequestAttributes.getResponse();OutputStream os = response.getOutputStream();response.setCharacterEncoding(StandardCharsets.UTF_8.name());response.setContentType("application/x-zip-compressed");response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));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();}}

导出结果

代码下载地址/fisher3652/exportDemo.git

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。