jxls version:2.8.1
需求:
利用jxls导出数据到excel。表格模板有表格嵌套表格样式。
模板样式查找官方文档找到合并单元格命令:
官方文档合并命令文档地址:http://jxls.sourceforge.net/reference/merge_cells_command.html
具体实现:
excel标注:
注意:标注上的命令不可以添加回车换行。jx:each(items="orderProductList" var="orderProduct" lastCell="H8")
jx:mergeCells(lastCell="A8" rows="orderProduct.orderProductGoodsList.size()")
jx:each(items="orderProduct.orderProductGoodsList" var="orderProductGoods" lastCell="H8")
数据结构:
{
orderProductList: [{
id: 1,
boxId: "1",
orderProductGoodsList: [{
speficicationName: "name",
weight: 6.6,
takeTaskId: 1
}],
weight: 6.6
}]
}
java代码:
public class ExcelTemplateUtil {
/**
*
* @param templateFileName 模板文件名字
* @param downloadFileName 导出下载文件名字
* @param response 响应对象
* @param dataMap 数据map
*/
public static void exportExcel(String templateFileName, String downloadFileName, HttpServletResponse response, Map<String, Object> dataMap){
try {
if (StringUtil.isNullOrBlank(downloadFileName)) {
downloadFileName = templateFileName;
}
//设置jxls相关信息
Context context = new Context();
for (String key : dataMap.keySet()) {
context.putVar(key, dataMap.get(key));
}
//加载模板
InputStream exIs = templateFileName.getClass().getResourceAsStream("/xls/"+templateFileName+".xlsx");
//生成导出文件
ByteArrayOutputStream os = new ByteArrayOutputStream();
JxlsHelper.getInstance().processTemplate(exIs, os, context);
response.reset();
response.addHeader("Content-Disposition",
"attachment;filename=" + new String((downloadFileName+DateUtil.formatDate(new Date(), "yyyyMMddHHmmss")+".xlsx").getBytes("UTF-8"),
"ISO-8859-1"));
response.addHeader("Content-Length", "" + os.size());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(os.toByteArray());
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
image
网友评论