|
@@ -2,6 +2,11 @@ package com.fdkankan.fusion.controller;
|
|
|
|
|
|
|
|
|
import com.deepoove.poi.XWPFTemplate;
|
|
|
+import com.deepoove.poi.data.RowRenderData;
|
|
|
+import com.deepoove.poi.data.Rows;
|
|
|
+import com.deepoove.poi.data.TableRenderData;
|
|
|
+import com.deepoove.poi.data.Tables;
|
|
|
+import com.deepoove.poi.util.PoitlIOUtils;
|
|
|
import com.fdkankan.fusion.common.ResultCode;
|
|
|
import com.fdkankan.fusion.common.ResultData;
|
|
|
import com.fdkankan.fusion.entity.CaseInquest;
|
|
@@ -12,10 +17,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
@@ -58,13 +60,34 @@ public class CaseInquestController {
|
|
|
if(caseInquest == null){
|
|
|
throw new BusinessException(ResultCode.INQUEST_ERROR2);
|
|
|
}
|
|
|
- OutputStream os = res.getOutputStream();
|
|
|
|
|
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/inquest-template.docx");
|
|
|
// 渲染模板
|
|
|
+
|
|
|
+ RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
|
|
|
+ .bgColor("4472C4").center().create();
|
|
|
+ RowRenderData row1 = Rows.create("李四", "博士");
|
|
|
+ TableRenderData tableRenderData = Tables.create(row0, row1);
|
|
|
+ caseInquest.setTableRenderData(tableRenderData);
|
|
|
XWPFTemplate template = XWPFTemplate.compile(inputStream).render(caseInquest);
|
|
|
|
|
|
- template.writeAndClose(os);
|
|
|
+ // 设置响应头,指定文件类型和内容长度
|
|
|
+ res.setContentType("application/octet-stream");
|
|
|
+ res.setHeader("Content-Disposition", "attachment; filename=output.docx");
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 返回网络流
|
|
|
+ OutputStream out = res.getOutputStream();
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(out);
|
|
|
+ template.write(bos);
|
|
|
+ bos.flush();
|
|
|
+ out.flush();
|
|
|
+ // 关闭流
|
|
|
+ PoitlIOUtils.closeQuietlyMulti(template, bos, out);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|