1:工具类
1.1:依赖
<!-- hutool依赖 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
<!-- poi依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
1.2:工具类
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ZipUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
/**
* 把传入的工作簿文件转成输入流
*
* @param workbooks 工作簿文件
* @return 文件流list
*/
public static List<ByteArrayOutputStream> workBook2Stream(List<HSSFWorkbook> workbooks) throws IOException {
List<ByteArrayOutputStream> list = new LinkedList<ByteArrayOutputStream>();
for (HSSFWorkbook obj : workbooks) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
obj.write(os);
list.add(os);
obj.close();
}
return list;
}
/**
* 压缩文件
*
* @param fileNames 被压缩的文件名称
* @return File 生成的压缩包文件
* @throws IOException
*/
public static File zipFile(String[] fileNames, List<HSSFWorkbook> workbooks) throws Exception {
// 写入输入流
List<ByteArrayOutputStream> workList = workBook2Stream(workbooks);
InputStream[] is = new InputStream[workList.size()];
int step = 0;
for (ByteArrayOutputStream bs : workList) {
byte[] content = bs.toByteArray();
ByteArrayInputStream bt = new ByteArrayInputStream(content);
is[step] = bt;
step++;
}
// 获取项目路径
String rootPath = "临时文件存放的路径。比如/opt/file";
// 创建临时文件tempFile
File zipFile = new File(rootPath + "/tempFile.zip");
if (!zipFile.exists()) {
zipFile.createNewFile();
}
zipFile = ZipUtil.zip(zipFile, fileNames, is);
// 关闭流
for (ByteArrayOutputStream steam : workList) {
IoUtil.close(steam);
}
for (InputStream steam : is) {
IoUtil.close(steam);
}
return zipFile;
}
2:例子
@GetMapping("/getZip")
public Response getZip(HttpServletResponse response) throws Exception {
//开始处理第一个Excel
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("sheet1");
List<HSSFWorkbook> list = new LinkedList<>();
String[] myArray = new String[2];//有几个Excel写几,我的例子是2个,所以写了2
HSSFRow nrow;
HSSFCell cell;
int q = 0;
List<String> sysUsers = new LinkedList<>();//这块是你查询出来的数据。正常来说是个List<实体类>
for (String sysUser : sysUsers) {
nrow = sheet.createRow(q);//创建第几行,第一次进来就是创建第一行
cell = nrow.createCell(0);//Excel第一个格
cell.setCellValue("第一个格子的值");
cell = nrow.createCell(1);//Excel第二个格
cell.setCellValue("第二个格子的值");
q++;
}
list.add(wb);
myArray[0] = "第一个Excel.xls";
//开始处理第二个Excel
q=0;
wb = new HSSFWorkbook();
sheet = wb.createSheet("sheet1");
List<String> sysRoles = new LinkedList<>();//这块是你查询出来的数据。正常来说是个List<实体类>
for (String sysRole : sysRoles) {
nrow = sheet.createRow(q);//创建第几行,第一次进来就是创建第一行
cell = nrow.createCell(0);//Excel第一个格
cell.setCellValue("第一个格子的值");
cell = nrow.createCell(1);//Excel第二个格
cell.setCellValue("第二个格子的值");
q++;
}
list.add(wb);
myArray[1] = "第二个Excel.xls";
File zipFile = zipFile(myArray, list);
LocalDateTime now = LocalDateTime.now();
String s = now + "";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/x-execl");
response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".zip");
// 读取文件
InputStream in = new FileInputStream(zipFile);
ServletOutputStream outputStream = response.getOutputStream();
// 写文件
int b;
while ((b = in.read()) != -1) {
outputStream.write(b);
}
in.close();
outputStream.close();
//删除临时文件
zipFile.delete();
return new Response(ResponseCode.OK);
}