java的poi的excel批量导入zip下载 代码

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

try {
File zipFile = new File(request.getSession().getServletContext ().getRealPath("") + File.separator + "demo.zip");
ZipOutputStream zipOut = null; // 声明压缩流对象
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.setComment(""); // 设置注释
//zip压缩打包
List<List<Map<String, Object>>> lists = ListSplitUtils.splitList(list, 10000);
for (int i = 0; i < lists.size(); i++) {
List<Map<String, Object>> twoList = new ArrayList<Map<String, Object>>();
twoList.addAll(lists.get(i));
Workbook wb = ExcelExportUtil.exportExcel(params, entityList, twoList);
zipOut.putNextEntry(new ZipEntry("demo" + i + ".xlsx"));
wb.write(zipOut);
zipOut.flush();
}
zipOut.close();
// 以流的形式下载文件。
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFile.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream (response.getOutputStream());
response.setContentType("application/octet-stream");
if (zipFile.getName() == null || zipFile.getName() == "") {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFile.getName().getBytes("UTF-8"), "ISO-8859-1"));
} else {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFile.getName().getBytes("UTF-8"), "ISO-8859-1"));
}
toClient.write(buffer);
toClient.flush();
toClient.close();
zipFile.delete(); //是否将生成的服务器端文件删除
} catch (IOException ex) {
ex.printStackTrace();
}

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 集合工具类
*/
public class {
//分片
public static <T> List<List<T>> splitList(List<T> list, int blockSize) {
List<List<T>> lists = new ArrayList<List<T>>();
if (blockSize == 1) {
lists.add(list);
return lists;
}
if (list != null && blockSize > 0) {
int listSize = list.size();
if (listSize <= blockSize) {
lists.add(list);
return lists;
}
int batchSize = listSize / blockSize;
int remain = listSize % blockSize;
for (int i = 0; i < batchSize; i++) {
int fromIndex = i * blockSize;
int toIndex = fromIndex + blockSize;
System.out.println("fromIndex=" + fromIndex + ", toIndex=" + toIndex);
lists.add(list.subList(fromIndex, toIndex));
}
if (remain > 0) {
System.out.println("fromIndex=" + (listSize - remain) + ", toIndex=" + (listSize));
lists.add(list.subList(listSize - remain, listSize));
}
}
return lists;
}
}