当前位置: 首页 > ds >正文

浏览器下载poi-tl生成的word文件

1、生成单个word,直接写入response到浏览器下载

   //浏览器单个下载File sourceFile = new File(templatePath + "/" + fileName + ".docx");try {//创建行循环策略LoopRowTableRenderPolicy rowTableRenderPolicy = new LoopRowTableRenderPolicy();//告诉模板引擎,绑定行循环策略Configure configure = Configure.builder().bind("imgList", rowTableRenderPolicy) //附表1.build();XWPFTemplate template = XWPFTemplate.compile(sourceFile,configure).render(exportMap);ServletOutputStream outputStream = response.getOutputStream();response.setCharacterEncoding("UTF-8");// 对文件名进行编码处理中文问题exportFileName = new String(exportFileName.getBytes(), StandardCharsets.UTF_8);response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(exportFileName, "UTF-8"));template.write(outputStream);outputStream.close();template.close();} catch (Exception e) {e.printStackTrace();}

2、生成多个word文件保存到本地,生成压缩包,再写入response到浏览器下载zip文件

(1)生成word文件到本地,并返回保存文件的目录

    private String saveReportExportWord(String templatePath, String fileName, Map<String, Object> exportParams,HttpServletRequest request, HttpServletResponse response, String uuid) {//创建行循环策略LoopRowTableRenderPolicy rowTableRenderPolicy = new LoopRowTableRenderPolicy();//告诉模板引擎,绑定行循环策略Configure configure = Configure.builder().bind("imgList", rowTableRenderPolicy) //附表1.build();String saveFilePath = "";// 校验Assert.notNull(templatePath, "模板路径不能为空");Assert.notNull(fileName, "导出文件名不能为空");Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");//模板文件File sourceFile = new File(templatePath);// 生成文档保存目录String savePath = uploadedReportPath;String subPathPrefix = uuid + "/";savePath += "/" + subPathPrefix;// 判断目录是否存在,不存在创建File dir = new File(savePath);if (!dir.exists()) {dir.mkdirs();}// 将文档保存到指定目录OutputStream fos = null;try {saveFilePath = savePath + fileName;fos = new FileOutputStream(saveFilePath);XWPFTemplate.compile(sourceFile, configure).render(exportParams).writeAndClose(fos);} catch (Exception e) {e.printStackTrace();} finally {// 关闭输出流try {fos.close();} catch (IOException e) {e.printStackTrace();}}return savePath;}

2.将word文件目录打包返回到response到浏览器下载

    //returnSavePath 是存放生成的word的文件夹if (StringUtils.isNotEmpty(returnSavePath)) {File zip = ZipUtil.zip(returnSavePath);try {ServletOutputStream outputStream = response.getOutputStream();FileInputStream ins = new FileInputStream(zip);response.setCharacterEncoding("UTF-8");// 对文件名进行编码处理中文问题fileName = new String(fileName.getBytes(), StandardCharsets.UTF_8);response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".zip", "UTF-8"));byte[] b = new byte[1024];int length;while ((length = ins.read(b)) > 0) {outputStream.write(b, 0, length);}outputStream.close();ins.close();} catch (Exception e) {e.printStackTrace();}//删除临时文件zip.delete();//压缩包File childFile = new File(returnSavePath); //生成的文件try {deleteDirectory(childFile);} catch (IOException e) {e.printStackTrace();}}

http://www.xdnf.cn/news/11554.html

相关文章:

  • 【Java】纯小白的三种工厂模式基础知识学习笔记
  • 标题:**炫酷桌面美学:xscreensaver-aerial——Linux用户的苹果电视高清屏保**
  • 当python爬虫遇到10060错误
  • CC服务器
  • 【讲解清晰生动,深入浅出,通俗易懂】什么是测地线(geodesic)?
  • Java入门级基础教学(史上最详细的整合)
  • 【linux】ubuntu安装系统软件路径为阿里源(不同版本源)
  • 【接口汇总】常用免费的API
  • Win11系统在administered账户下无法登录微软账户显示:哎呀,出错了。解决方法。
  • Android多线程之HandlerThread详解
  • Unity中Avatar换装实现
  • 【初探Java之路 一 】什么是Java以及安装其环境
  • InstantClient+PLSQL安装配置教程
  • 【毕业设计源码】PHP网上商城管理系统
  • 计算机的发展史与计算机硬件组成
  • 单片机---HLK-W801开发环境搭建
  • JavaScript保姆级教程 ——— 重难点详细解析(万字长文,建议收藏)
  • 栏栅解密
  • linux ubuntu 24.04安装优麒麟ukylin版搜狗输入法
  • PostMessage和SendMessage的区别
  • 2023 Android面试官 常问的问题以及答案(附最新的Android面试大厂必考174题 )
  • ZYNQ FPGA实验——ADDA测试
  • 电脑出现错误代码:-118是什么情况,解决错误代码:-118的方法
  • 横盘是什么意思?
  • 嘉靖王朝最大的一出闹剧和惨剧——大礼仪之争
  • 使用C#和.NET Core的ETL作业
  • 【Qt系列教程】一、认识Qt、安装Qt、运行Hello Qt
  • Linux系统命令大全(超级详细版)
  • TestLink的使用实例
  • Ubuntu、kubuntu、xUbuntu、lubuntu等版本区别及界面样式