使用poi-tl根据富文本编辑器代码生成word下载功能

直接上干货

 

1、引入插件

       
            com.deepoove
            poi-tl
            1.6.0
         

2、后端代码处理

String content = (String) info.get("plancontent");
		//构造完整的html页面,方便组件进行处理
		content = ""
				+ content + "";
		InputStream is = new ByteArrayInputStream(content.getBytes("utf-8"));
		response.setHeader("Content-Type", "application/msword;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=test.docx");
		ServletOutputStream out = response.getOutputStream();
		POIFSFileSystem fs = new POIFSFileSystem();
		fs.createDocument(is, "WordDocument");
		fs.writeFilesystem(out);
		out.close();
		is.close();

3、前端vue请求方法处理

// 下载
function downloadInfo(data) {
  return request({
    url: '/netFightmap/downloadInfo',
    method: 'post',
    responseType: 'blob',
    data: data
  })
}
    download() {
      this.loading = true;
      downloadInfo({
        id: this.fightmapid,
        userid: this.$store.state.user.userId
      }).then((res) => {
        this.loading = false;
        const content = res
        const blob = new Blob([content])
        const fileName = this.industryMapDetail.customername + '.docx'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
        }) .catch((error) => {
          this.loading = false
          console.log(error)
        })
    },

查看效果,

使用poi-tl根据富文本编辑器代码生成word下载功能_第1张图片

 使用poi-tl根据富文本编辑器代码生成word下载功能_第2张图片

 综上,此插件用于在线生成word文档。

这几天需要说要把附件也给下载下来然后压缩一起下载,下面实现的方法如下

Map info = netFightmapService.getInfoAndUpdateNum(id, userId);
		String content = (String) info.get("plancontent");
		String customername = (String) info.get("customername");
		// 构造完整的html页面,方便组件进行处理
		content = ""
				+ content + "";
		InputStream is = new ByteArrayInputStream(content.getBytes("utf-8"));
		// 设置响应请求头
		response.setContentType("application/zip");
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("附件.zip", "UTF-8"));
		ServletOutputStream out = response.getOutputStream();
		// 生成在线文档,页面转换成word文档
		ByteArrayOutputStream out1 = new ByteArrayOutputStream();
		POIFSFileSystem fs = new POIFSFileSystem();
		fs.createDocument(is, "WordDocument");
		fs.writeFilesystem(out1);
		// 查询所有的附件
		NetAttachmentEntity netAttachmentEntity = new NetAttachmentEntity();
		netAttachmentEntity.setOtherid(id);
		List netAttachmentList = netAttachmentService.getListByObject(netAttachmentEntity);
		ZipOutputStream zipOutputStream = new ZipOutputStream(out);
		// 根据附件id下载文件流
		for (int i = 0; i < netAttachmentList.size(); i++) {
			NetAttachmentEntity attachmentEntity = netAttachmentList.get(i);
			// 文件id
			String fileid = attachmentEntity.getAttachmentfileid();
			// 根据文件id查询文件对应的文件流
			MyFile f = ComFileManager.FileDowload(fileid, "net");
			// 把文件流写入压缩文件流里头然后输出
			ZipEntry zipEntry = new ZipEntry(f.getFileName());
			zipOutputStream.putNextEntry(zipEntry);
			InputStream in = f.getInStream();
			int temp = 0;
			while ((temp = in.read()) != -1) { // 读取内容
				zipOutputStream.write(temp); // 压缩输出
			}
		}
		ZipEntry zipEntry = new ZipEntry(customername + ".docx");
		zipOutputStream.putNextEntry(zipEntry);
		zipOutputStream.write((byte[]) out1.toByteArray());
		if (out1 != null) {
			try {
				out1.close();
			} catch (Exception e2) {
				bizlog.info("下载页面文件--生成在线文件关闭失败", e2);
			}
		}
		if (zipOutputStream != null) {
			try {
				zipOutputStream.close();
			} catch (Exception e2) {
				bizlog.info("下载所有附件--zip实体关闭失败", e2);
			}
		}
		if (out != null) {
			try {
				out.close();
			} catch (Exception e) {
				bizlog.info("下载所有附件--输入缓冲流关闭失败", e);
			}
		}
		if (is != null) {
			try {
				is.close();
			} catch (Exception e) {
				bizlog.info("下载所有附件--输入缓冲流关闭失败", e);
			}
		}

你可能感兴趣的:(word,在线生成文件,下载,POI-tl)