使用EasyPOI怎么实现动态生成列数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联-专业网站定制、快速模板网站建设、高性价比乾安网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式乾安网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖乾安地区。费用合理售后完善,10年实体公司更值得信赖。
cn.afterturn easypoi-base 3.2.0 org.slf4j slf4j-api org.apache.commons commons-lang3 cn.afterturn easypoi-annotation 3.2.0
定义表格样式
/** * 定义表格样式 * * @param start 查询起始日期 * @param end 查询结束日期 * @return java.util.List* @author huan * @date 2019/6/21 * @since 2.8.2 */ private List setExportExcelStyle(DateTime start, DateTime end) { //定义表格列名,该集合存放的就是表格的列明,每个对象就是表格中的一列 List modelList = new ArrayList (); //该对象就是定义列属性的对象 ExcelExportEntity excelentity = null; //定义第一个列 excelentity = new ExcelExportEntity("企业全称", "companyName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第二个列 excelentity = new ExcelExportEntity("企业简称", "companyShortName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第三个列,这里指定了日期显示格式 excelentity = new ExcelExportEntity("认证日期", "openDate"); excelentity.setWidth(20); excelentity.setHeight(10); excelentity.setFormat("yyyy-MM-dd"); modelList.add(excelentity); //定义第四个列,这边就是动态生成的,跟用用户选择的日期范围,动态生成列的数量 excelentity = new ExcelExportEntity(null, "recordDate"); //设置一个集合,存放动态生成的列 List modelListChild = new ArrayList (); start = DateUtils.getDateZeroTime(start); while (start.isBefore(end)) { String date = start.toString("yyyy-MM-dd"); modelListChild.add(new ExcelExportEntity(date, date, 15)); start = start.plusDays(1); } //日期按从小到大顺序排序,这里用了最简单的冒泡排序 for (int i = 0; i < modelListChild.size(); i++) { for (int j = 0; j < modelListChild.size(); j++) { String e1 = modelListChild.get(i).getKey().toString(); String e2 = modelListChild.get(j).getKey().toString(); if (e1.compareTo(e2) < 0) { ExcelExportEntity x1 = modelListChild.get(i); ExcelExportEntity x2 = modelListChild.get(j); modelListChild.set(j, x1); modelListChild.set(i, x2); } } } //将定义好的字列放到父列中 excelentity.setList(modelListChild); modelList.add(excelentity); //定义第五个列 excelentity = new ExcelExportEntity("应当使用天数", "shouldUseDay"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第六个列 excelentity = new ExcelExportEntity("实际使用天数", "actualUseDay"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第七个列 excelentity = new ExcelExportEntity("使用率", "rate"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第八个列 excelentity = new ExcelExportEntity("推荐人", "commandMan"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第九个列 excelentity = new ExcelExportEntity("拓客", "tk"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第十个列 excelentity = new ExcelExportEntity("对接人", "connector"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); return modelList; }
定义表格数据
private List
主体方法
/**
* 报表导出
*
* @param analyseStockQuery analyseStockQuery
* @param response response
* @return javax.servlet.http.HttpServletResponse
* @author huan
* @date 2019/6/21
* @since 2.8.2
*/
public HttpServletResponse exportStock(AnalyseStockQuery analyseStockQuery, HttpServletResponse response) {
try {
//设置默认查询日期
analyseStockQuery = setDefaultQueryDate(analyseStockQuery);
//参数校验
checkListDetailDataParam(analyseStockQuery);
//日期格式化
DateTime start = new DateTime().withDate(new LocalDate(analyseStockQuery.getQueryStartDate()));
DateTime end = new DateTime().withDate(new LocalDate(analyseStockQuery.getQueryLastDate()));
//定义表格样式
List modelList = setExportExcelStyle(start, end);
//定义表格名称
String fileName = URLEncoder.encode("客户库存使用统计表-" + start.toString("yyyy年MM月dd日") + "~" + end.toString("yyyy年MM月dd日"), "utf-8");
// Sheet1样式
ExportParams sheet1ExportParams = new ExportParams();
// 设置sheet得名称
sheet1ExportParams.setSheetName("入库统计");
// 创建sheet1使用得map
Map sheet1ExportMap = new HashMap<>();
// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
sheet1ExportMap.put("title", sheet1ExportParams);
//sheet1样式
sheet1ExportMap.put("entityList", modelList);
//sheet1中要填充得数据,true表示查询入库数据,false表示查询易签待入库数据
sheet1ExportMap.put("data", getData(analyseStockQuery, true));
//Sheet2设置
ExportParams sheet2ExportParams = new ExportParams();
sheet2ExportParams.setSheetName("易签待入库统计");
Map sheet2ExportMap = new HashMap<>();
sheet2ExportMap.put("title", sheet2ExportParams);
sheet2ExportMap.put("entityList", modelList);
sheet2ExportMap.put("data", getData(analyseStockQuery, false));
// 将sheet1、sheet2使用得map进行包装
List> sheetsList = new ArrayList<>();
sheetsList.add(sheet1ExportMap);
sheetsList.add(sheet2ExportMap);
// 执行方法
Workbook workBook = exportExcel(sheetsList, ExcelType.HSSF);
//设置response
response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
//设置编码格式
response.setCharacterEncoding("GBK");
//将表格内容写到输出流中并刷新缓存
@Cleanup ServletOutputStream out = response.getOutputStream();
workBook.write(out);
out.flush();
workBook.close();
} catch (FileNotFoundException e) {
log.debug("FileNotFoundException:{}", e.getMessage());
} catch (UnsupportedEncodingException e) {
log.debug("UnsupportedEncodingException:{}", e.getMessage());
} catch (IOException e) {
log.debug("IOException:{}", e.getMessage());
}
return response;
} 看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。