poi 导出 多个Excel 文件压缩
创建多个文件
public File export(){
File file=null;
Map<String, File> files = new HashMap<String, File>();
String outPutFilePath=repositoryUtils.getTempPath();
fileName = "xxx"+".xls";
file = excelUtils.outputExcelDataAtTempFile(hSSFWorkbook,outPutFilePath+fileName);
files.put(fileName, file);
if(files.size()<=1){
return file;
}else{
fileName = ZipUtils.compressFiles(outPutFilePath,"EmployeeAlAndCl.zip",files);
ZipUtils.removeTempFiles(files);
if (files != null) {
files.clear();
}
return new File(outPutFilePath+fileName);
}
}
//用输出流写出一个Excel 文件方法
public File outputExcelDataAtTempFile(HSSFWorkbook wb ,String path) throws IOException {
FileOutputStream fos = null;
try {
File file = new File(path);
fos = new FileOutputStream(file);
wb.write(fos);
return file;
} finally {
if(fos != null) {
fos.flush();
fos.close();
}
}
}
public class ZipUtils {
static final int BUFFER = 1024 * 1024 * 10;
public static String compressFiles (String outPutFilePath, String output, Map<String, File> files) throws IOException {
BufferedInputStream origin = null;
FileOutputStream dest = null;
ZipOutputStream out = null;
FileInputStream fi = null;
try {
dest = new
FileOutputStream(outPutFilePath+output);
out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
for (Iterator<String> it = files.keySet().iterator();it.hasNext();){
String filename = (String)it.next();
System.out.println("Adding: "+filename);
File f = (File)files.get(filename);
fi = new
FileInputStream(outPutFilePath+filename);
data = new byte[(int)f.length()];
data = getBytesFromFile(f);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(filename);
out.putNextEntry(entry);
int count = 0;
while((count = origin.read(data, 0,
data.length)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
} finally {
if (dest != null) {
try {
dest.close();
} catch (Exception e) {
dest = null;
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
out = null;
}
}
if (fi != null) {
try {
fi.close();
} catch (Exception e) {
fi = null;
}
}
if (origin != null) {
try {
origin.close();
} catch (Exception e) {
origin = null;
}
}
}
return output;
}
}
//handler 里
public String export() {
try {
File file=employeeAuditTrailReportService.export(employeeAuditTrailReportPageBean.getDepartmentCode(),
employeeAuditTrailReportPageBean.getEmployeeId(), caller.getLoginId());
if(file!=null){
return downloadUtils.sendBySerlvet(file, file.getName().endsWith(".xls")?excelName:zipName);
}else{
return Constants.EMPTY_STRING;
}
} catch (Exception e) {
return errorHandling(e);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述