数据导出⽣成word附件使⽤POI的XWPFTemplate对象⽐较常⽤的实现Java导⼊、导出Excel的技术有两种Jakarta POI和Java Excel。
Jakarta POI 是⼀套⽤于访问微软格式⽂档的Java API。Jakarta POI有很多组件组成,其中有⽤于
操作Excel格式⽂件的HSSF和
⽤于操作Word的HWPF;
excel锁定
⼀、前端使⽤get请求和post请求都可以
get请求:
地震自救小常识window.location.href= appPath+"/export/handoverForm?ticketId="+lf.ticketId +
"&complainId="+row.id+"&formCode="+plainDetail;
隐藏form表单改写成post请求,form表单get和post都可以:
<form id="exportForm" action="/rvice/xfComplain/exportCaLedger" method="post">
<input type="hidden" name="dataRange" id="dataRange"/>
褪色的回忆<input type="hidden" name="processStatus" id="processStatus"/>
<input type="hidden" name="cantonCode" id="cantonCode"/>散粉的作用
<input type="hidden" name="startDate" id="startDate"/>
<input type="hidden" name="endDate" id="endDate"/>
<input type="hidden" name="multiFildMatch" id="multiFildMatch"/>
</form>
$("#dataRange").val(lf.table.pageData.dataRange);
$("#processStatus").val(lf.table.pageData.processStatus);
$("#startDate").val(lf.table.pageData.startDate);
$("#endDate").val(lf.dDate);
$("#multiFildMatch").val(lf.table.pageData.multiFildMatch);
$('#exportForm').submit();
⼆、java代码
@ResponBody
@RequestMapping("/exportWord")
public void exportHandoverForm(HttpServletRequest request,
HttpServletRespon respon, ExportParam exportParam) {
String projectPath = getProjectPath(request);
String templateFile = getTemplateFile(exportParam, projectPath, request);
Map<String, Object> data = new HashMap<>();
File file = null;
InputStream in = null;
ServletOutputStream out = null;
try {
// 数据源
HandoverModel handoverModel = ComplainId());
河南牛肉汤// 反射机制,获取所有属性对象,在拿到属性值,设置数据
for (Field field : DeclaredFields()) {
// 暴⼒反射,不是public修饰的属性也要获取
field.tAccessible(true);
data.Name(), (handoverModel));
}
// 数据源组成map键值对形式
data.put("reportorList", ReportorList().getDatas());
String fileName = CaCode();
String exportFilePath = projectPath + ExportConstant.DEFAULT_EXPORT_PATH;
logger.info("----------templateFile:" + templateFile);
logger.info("-----------projectPath:" + projectPath);
// Configure对象是处理表格数据,可以⾃适应⽣成对应⾏数数据
Configure config = wBuilder().customPolicy("reportorList", new FileTablePolicy()).build();
// XWPFTemplate对象是处理word⽂档对象,根据map数据源的键值对渲染⽂档数据
怎样在网上购物XWPFTemplate template = pile(templateFile, config).render(data);
// ⽂件⽣成到本地,先⽣成好⽂档,再读取到内存中响应给前台
String downloadFileName = fileName + ".docx";
File outPutFile = new File(exportFilePath);
if (!ists()) {
outPutFile.mkdirs();
}
FileOutputStream outFile = new FileOutputStream(exportFilePath + downloadFileName);
template.write(outFile);
outFile.flush();
outFile.clo();
template.clo();
/
/ 通过⽂件流读取到⽂件,再将⽂件通过respon的输出流,返回给页⾯下载
file = new File(projectPath + ExportConstant.DEFAULT_EXPORT_PATH + downloadFileName); in = new FileInputStream(file);
respon.tCharacterEncoding("utf-8");
respon.tContentType("application/msword");
respon.tHeader("Content-Disposition", "attachment;filename="
.concat(String.de(downloadFileName, "UTF-8"))));
out = OutputStream();
byte[] buffer = new byte[512];
int bytesToRead = -1;
// ⽤响应对象respon中的输出流读取⽣成好的⽂件
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (Exception e) {
<("导出word出错", e);
} finally {如也是什么意思
if (in != null) try {
in.clo();
红船读后感
if (out != null) out.clo();
if (file != null) file.delete(); // 删除临时⽂件
} catch (IOException e) {
<("删除删除临时⽂件出错", e);
}
}
}
使⽤XWPFTemplate引擎,插件化Configure插⼊表格;
三、不需要插⼊表格:
XWPFTemplate template = pile(templateFile).render(data);