hutool导出Excel设置行高、列宽、合并列处理

还有一种

LinkedHashMap<String, Long> numMap = new LinkedHashMap<>();
numMap = list.stream().collect(Collectors.groupingBy(xxx::getxxx, LinkedHashMap::new, Collectors.counting()));
numMap = list.stream().collect(Collectors.groupingBy(item->item.getxxx(), LinkedHashMap::new, Collectors.counting()));

 

 

设置可以根据实际情况设定,本次仅供参考:

复制
public static void exportUserMealExcel(HttpServletResponse response, List<?> list, BigDecimal bigDecimal, BigDecimal bankCost){
try {
ExcelWriter writer = ExcelUtil.getWriter();
writer.getSheet().setColumnWidth(3, 10000);
writer.getSheet().setColumnWidth(4, 8000);
writer.getSheet().setColumnWidth(5, 10000);
writer.getSheet().setColumnWidth(6, 4000);
writer.getSheet().setColumnWidth(7, 8000);
writer.getSheet().setColumnWidth(8, 10000);
writer.getSheet().setColumnWidth(9, 10000);

writer.setHeaderAlias(ExcelHeaderUtils.IndexOperaLogInfoInfoHeard());
writer.setOnlyAlias(true);
writer.autoSizeColumnAll();
writer.write(list, true);
writer.setRowHeight(0, 50);


writer.setRowHeight(list.size() + 1, 50);
//写完数据最后一行进行合并结算
writer.merge(list.size() + 1, list.size() + 1, 0, 4, "扣费总计:" + CommonService.formatCost(bigDecimal), true);
writer.merge(list.size() + 1, list.size() + 1, 5, 8, "支付扣费总计: " + CommonService.formatCost(bankCost), true);
writer.setRowHeight(list.size() + 1, 50);
String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmss") + ".xls";
//设置content—type
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
//Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// 为了前端能监听到文件下载成功
Cookie cookie = new Cookie("fileDownload", "true");
cookie.setPath("/");
response.addCookie(cookie);
//-----------------------------
ServletOutputStream outputStream = response.getOutputStream();
//将Writer刷新到OutPut
writer.flush(outputStream, true);
outputStream.close();
writer.close();
}catch (IOException e){
logger.error("导出扫码用餐数据异常" + e.getMessage());
}
}
-----------------------------------
©著作权归作者所有:来自51CTO博客作者wx59a3ee9481c03的原创作品,请联系作者获取转载授权,否则将追究法律责任
hutool导出Excel设置行高、列宽、合并列处理
https://blog.51cto.com/u_13253065/7272739

posted @ 2024-01-12 08:40  全琪俊  阅读(927)  评论(0编辑  收藏  举报