SpringBoot+Hutool Excel数据库导出
0:SpringBoot Hutool导出 数据导出 hutool excel导出 原样式
1:前期数据准备(List<Bean> list);
2:导出代码(基于Ruoyi-vue SpringBoot后端版本);
@GetMapping("/priceweek/importTemplate")
@Log(title = "导出周平均价格", businessType = BusinessType.EXPORT)
public AjaxResult importTemplate(DrWeekPrice drWeekPrice) throws IOException {
SysDept sysDept=sysDeptService.selectDeptById(Long.parseLong(drWeekPrice.getMarketId()));
List<DrWeekPrice> list = drWeekPriceService.selectDrWeekAvgPrice(drWeekPrice);
//定义hutool格式
ExcelWriter writer = ExcelUtil.getWriter();
writer.autoSizeColumnAll(); //自动列宽 感觉不太好用
writer.setColumnWidth(-1,20);
writer.setRowHeight(-1,50);
writer.setOnlyAlias(true); //将导出的excel表中的ID去掉。
//冻结前两行
writer.setFreezePane(2);
OutputStream out =null;
writer.addHeaderAlias("shopNumber","商户编号");
writer.addHeaderAlias("shopName","商户名称");
writer.addHeaderAlias("shopPeople","商户负责人");
writer.addHeaderAlias("className","类别名称");
writer.addHeaderAlias("product","规格型号");
writer.addHeaderAlias("unit","单位");
writer.addHeaderAlias("thisWeekPrice","本周总销售额");
writer.addHeaderAlias("thisDayTrading","本周总销量");
writer.addHeaderAlias("thisWeekAvgPrice","本周均价");
writer.addHeaderAlias("lastWeekAvgPrice","上周均价");
writer.addHeaderAlias("ringvalue","价格浮动");
String fileweek= DateUtil.format(drWeekPrice.getSurveyDateWeek(), "yyyy-MM-dd");
writer.merge(11, fileweek+"-"+sysDept.getDeptName()+"-"+"代表品周价格信息");
writer.write(list,true);
writer.renameSheet("all");
String filename="代表品周价格.xls";
out= new FileOutputStream(getAbsoluteFile(filename));
writer.flush(out);
return AjaxResult.success(filename);
}
3
Hutool的Http文件下载方法
因为Hutool-http机制问题,请求页面返回结果是一次性解析为byte[]的,如果请求URL返回结果太大(比如文件下载),那内存会爆掉,因此针对文件下载HttpUtil单独做了封装。文件下载在面对大文件时采用流的方式读写,内存中只是保留一定量的缓存,然后分块写入硬盘,因此大文件情况下不会对内存有压力。
文件下载 since 5.7.12
String fileUrl = "http://mirrors.sohu.com/centos/8.4.2105/isos/x86_64/CentOS-8.4.2105-x86_64-dvd1.iso";
//将文件下载后保存在E盘,返回结果为下载文件大小
long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
System.out.println("Download size: " + size);
下载流文件 since 5.7.12
HttpUtil.download()