这几天有个业务需要把项目里数据生成excel后打包zip下载,但之前的项目基本都是用Apache自带的poi去做,这个项目引入Hutool工具,所以就用了Hutool的功能去做了,跟之前相比感觉省事了一些。
导包的细节就不展示了,直接去Maven去找就行,直接代码吧。
1、实体
@Getter
@Setter
public class Student {
private String id;
private String name;
private Integer age;
private String sex;
}
2、映射
这里用的是tk.Mybatis
public interface StudentMappper extends Mapper<Student>{}
3、导出的实体工具类
这个类只有需要导出的excel的字段,id,创建时间之类的不用,方便后续管理excel的字段变更
public class StudentExport {
private String name;
private Integer age;
private String sex;
}
4、Service
@Service
public class StudentService {
@Autowired
private StudentMappper studentMappper;
public List<Student> getAll(){
return studentMappper.selectAll();
}
//创建单个excel的方法,多个excel的话,加几个这种方法就行
public List<Object> createExcel() {
List<Object> result=new ArrayList<Object>();
List<Student> sList=getAll();
List<StudentExport> exportList=new ArrayList<StudentExport>();
for (Student student : sList) {
StudentExport export=new StudentExport();
export.setAge(student.getAge());
export.setName(student.getName());
export.setSex(student.getSex());
exportList.add(export);
}
ExcelWriter writer=new ExcelUtil().getWriter();
//添加表头对应的数据列
writer.addHeaderAlias("name", "名字");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("sex", "性别");
writer.write(exportList,true);
//格式化文件名字模板
String fileName = String.format("%s-%s.xls", "student", DateUtil.format(new Date(), "yyyyMMdd"));
//写入流
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.flush(out);
//关闭
writer.close();
result.add(out);
result.add(fileName);
return result;
}
//循环导入excel的流,准备在Controller层生成zip包
public void writeZos(List<ByteArrayOutputStream> bosList, ZipOutputStream zos,List<String> excelName) throws IOException {
for (int i = 0; i < bosList.size(); i++) {
//将多个excel都转成字节流写入
zos.putNextEntry(new ZipEntry(excelName.get(i)));
byte[] excelStream=bosList.get(i).toByteArray();
zos.write(excelStream);
//记得关闭
zos.closeEntry();
}
}
}
5、Controller层
public class StudentApi {
@Autowired
private StudentService studentService;
public void createZip(HttpServletResponse response) throws IOException {
List<ByteArrayOutputStream> bosList=new ArrayList<ByteArrayOutputStream>();
List<String> excelName =new ArrayList<String>();
//这里循环取出service里填充的单个excel流和单个excelName,如果多个循环即可
bosList.add((ByteArrayOutputStream) studentService.createExcel().get(0));
excelName.add((String) studentService.createExcel().get(1));
//创建HttpServerResponse的输出流
OutputStream out=response.getOutputStream();
//创建写入流
BufferedInputStream bis;
//创建要写入的文件
File file=new File("student.zip");
//通过ZipOutputStream定义要写入的对象
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(file));
//调取service层写入循环excle的方法,将流输入
studentService.writeZos(bosList, zos, excelName);
zos.close();
//定义返回类型
response.setContentType("text/html; charset=UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode("student.zip", "UTF-8"))));
bis = new BufferedInputStream(new FileInputStream("student.zip"));
//定义byte,长度就是要转成zip文件的byte长度,避免浪费资源
byte[] buffer=new byte[bis.available()];
bis.read(buffer);
out.flush();
out.write(buffer);
}
}
Controller通过postman或者Swagger直接调取就能生成zip下载了
最后
以上就是大意烤鸡最近收集整理的关于使用Hutool生成多个excel文件合并成zip压缩包下载的全部内容,更多相关使用Hutool生成多个excel文件合并成zip压缩包下载内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复