话不多说,先讲需求,再上代码。
需求1:数据库查询出数据 导出并封装成excel 再打包成zip 并下载下来
代码:1):数据查出来不说了。
2):封装成excel
3):将excel打包成zip
4):下载
5) :下载完成后删除文件夹以及文件 我把它放在了第四步里面
备注:需要导入的包没贴出来 具体需要哪些包写的时候有提示
public void exportExcel(){
public Map exportExcel(String startDate,String serialNo,String plugName,
HttpServletRequest request, HttpServletResponse response,String path){
/*********按条件查询数据**********/
List
/**
* 生成zip文件 zip文件名字是ExcelDataZip.zip
* @param path
* @throws IOException
*/
public String craeteZipPath(String path) throws IOException{
ZipOutputStream zipOutputStream = null;
File file = new File(path+"ExcelDataZip"+".zip");
zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
File[] files = new File(path).listFiles();
FileInputStream fileInputStream = null;
byte[] buf = new byte[1024];
int len = 0;
if(files!=null && files.length > 0){
for(File excelFile:files){
String fileName = excelFile.getName();
fileInputStream = new FileInputStream(excelFile);
//放入压缩zip包中;
zipOutputStream.putNextEntry(new ZipEntry(fileName));
while((len=fileInputStream.read(buf)) >0){
zipOutputStream.write(buf, 0, len);
}
zipOutputStream.closeEntry();
if(fileInputStream != null){
fileInputStream.close();
}
}
}
if(zipOutputStream !=null){
zipOutputStream.close();
}
return null;
}
/**
* 下载excelDataZip
* @param response
* @param request
* @return
*/
public String downLoadExcelZip(HttpServletResponse response,HttpServletRequest request,String path) {
String fileName = "excelDataZip.zip";
String realPath=path+"excelDataZip.zip";
try {
File file = new File(realPath);
File file2 = new File(path);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes("ISO8859-1"), "UTF-8"));
response.setContentLength((int) file.length());
response.setContentType("application/zip");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
OutputStream myout = response.getOutputStream();
// 开始循环下载
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
buff.close();
file.delete();
deleteFile(file2);//删除新建的文件夹
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
* 递归删除文件夹
* @param file
*/
public void deleteFile(File file) {
if(file.exists()){
if(file.isFile()){//判断是否是文件
file.delete();
}else if(file.isDirectory()){//否则如果它是一个目录
File[] files = file.listFiles();//声明目录下所有的文件 files[];
for(int i = 0;i < files.length;i ++){//遍历目录下所有的文件
this.deleteFile(files[i]);//把每个文件用这个方法进行迭代
}
file.delete();//删除文件夹
}
}else{
System.out.println("所删除的文件不存在");
}
}
//最后贴一个 解压zip的
/**
* 解压zip
* @param zipFile 解压的zip文件
* @param descDir 目标地址
*/
public String zipToExcel(File zipFile,String descDir)throws IOException{
String fileName="";
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
//解压zip文件中有中文目录或者中文文件
ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
fileName = zipEntryName;
InputStream in = zip.getInputStream(entry);
String outPath = descDir+"/"+zipEntryName;
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
System.out.println("******************解压完毕********************");
return fileName;
}