导出多个excel的zip压缩包工具类

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
public static void zipFile(HttpServletResponse response, List<XSSFWorkbook> workbookList, String fileName, List<String> file) throws IOException {
ServletOutputStream outputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
try {
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename= " + URLEncoder.encode(fileName, "UTF-8") + ".zip");
for (int i = 0; i < workbookList.size(); i++) {
XSSFWorkbook workbook = workbookList.get(i);
String str = file.get(i);
ZipEntry zipEntry = new ZipEntry(str);
zipOutputStream.putNextEntry(zipEntry);
workbook.write(zipOutputStream);
}
zipOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipOutputStream != null) {
zipOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
posted @ 2023-07-12 08:46  懂得归零  阅读(53)  评论(0编辑  收藏  举报