java动态导出excel压缩成zip下载的方法
本文实例为大家分享了java动态导出excel压缩成zip下载的具体代码,供大家参考,具体内容如下
packagepack.java.io.demo;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
importjxl.Workbook;
importjxl.format.Alignment;
importjxl.format.Border;
importjxl.format.BorderLineStyle;
importjxl.format.Colour;
importjxl.format.UnderlineStyle;
importjxl.format.VerticalAlignment;
importjxl.write.Label;
importjxl.write.WritableCellFormat;
importjxl.write.WritableFont;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importjxl.write.WriteException;
importjxl.write.biff.RowsExceededException;
/**
*zip压缩文件实例
*addby周海涛
*@authorAdministrator
*
*/
publicclassZipDemo{
/**
*@paramargs
*@throwsIOException
*@throwsWriteException
*@throwsRowsExceededException
*/
publicstaticvoidmain(String[]args)throwsRowsExceededException,WriteException,IOException{
Stringpath="C:/document/excel";
//创建文件夹;
createFile(path);
//创建Excel文件;
createExcelFile(path);
//生成.zip文件;
craeteZipPath(path);
//删除目录下所有的文件;
Filefile=newFile(path);
//删除文件;
deleteExcelPath(file);
//重新创建文件;
file.mkdirs();
}
/**
*创建文件夹;
*@parampath
*@return
*/
publicstaticStringcreateFile(Stringpath){
Filefile=newFile(path);
//判断文件是否存在;
if(!file.exists()){
//创建文件;
booleanbol=file.mkdirs();
if(bol){
System.out.println(path+"路径创建成功!");
}else{
System.out.println(path+"路径创建失败!");
}
}else{
System.out.println(path+"文件已经存在!");
}
returnpath;
}
/**
*在指定目录下创建Excel文件;
*@parampath
*@throwsIOException
*@throwsWriteException
*@throwsRowsExceededException
*/
publicstaticvoidcreateExcelFile(Stringpath)throwsIOException,RowsExceededException,WriteException{
for(inti=0;i<3;i++){
//创建Excel;
WritableWorkbookworkbook=Workbook.createWorkbook(newFile(path+"/"+newSimpleDateFormat("yyyyMMddHHmmsss").format(newDate())+"_"+(i+1)+".xls"));
//创建第一个sheet文件;
WritableSheetsheet=workbook.createSheet("导出Excel文件",0);
//设置默认宽度;
sheet.getSettings().setDefaultColumnWidth(30);
//设置字体;
WritableFontfont1=newWritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormatcellFormat1=newWritableCellFormat(font1);
//设置背景颜色;
cellFormat1.setBackground(Colour.BLUE_GREY);
//设置边框;
cellFormat1.setBorder(Border.ALL,BorderLineStyle.DASH_DOT);
//设置自动换行;
cellFormat1.setWrap(true);
//设置文字居中对齐方式;
cellFormat1.setAlignment(Alignment.CENTRE);
//设置垂直居中;
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
//创建单元格
Labellabel1=newLabel(0,0,"第一行第一个单元格(测试是否自动换行!)",cellFormat1);
Labellabel2=newLabel(1,0,"第一行第二个单元格",cellFormat1);
Labellabel3=newLabel(2,0,"第一行第三个单元格",cellFormat1);
Labellabel4=newLabel(3,0,"第一行第四个单元格",cellFormat1);
//添加到行中;
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
//给第二行设置背景、字体颜色、对齐方式等等;
WritableFontfont2=newWritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);
WritableCellFormatcellFormat2=newWritableCellFormat(font2);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setBackground(Colour.PINK);
cellFormat2.setBorder(Border.ALL,BorderLineStyle.THIN);
cellFormat2.setWrap(true);
//创建单元格;
Labellabel11=newLabel(0,1,"第二行第一个单元格(测试是否自动换行!)",cellFormat2);
Labellabel22=newLabel(1,1,"第二行第二个单元格",cellFormat2);
Labellabel33=newLabel(2,1,"第二行第三个单元格",cellFormat2);
Labellabel44=newLabel(3,1,"第二行第四个单元格",cellFormat2);
sheet.addCell(label11);
sheet.addCell(label22);
sheet.addCell(label33);
sheet.addCell(label44);
//写入Excel表格中;
workbook.write();
//关闭流;
workbook.close();
}
}
/**
*生成.zip文件;
*@parampath
*@throwsIOException
*/
publicstaticvoidcraeteZipPath(Stringpath)throwsIOException{
ZipOutputStreamzipOutputStream=null;
Filefile=newFile(path+newSimpleDateFormat("yyyyMMddHHmmss").format(newDate())+".zip");
zipOutputStream=newZipOutputStream(newBufferedOutputStream(newFileOutputStream(file)));
File[]files=newFile(path).listFiles();
FileInputStreamfileInputStream=null;
byte[]buf=newbyte[1024];
intlen=0;
if(files!=null&&files.length>0){
for(FileexcelFile:files){
StringfileName=excelFile.getName();
fileInputStream=newFileInputStream(excelFile);
//放入压缩zip包中;
zipOutputStream.putNextEntry(newZipEntry(path+"/"+fileName));
//读取文件;
while((len=fileInputStream.read(buf))>0){
zipOutputStream.write(buf,0,len);
}
//关闭;
zipOutputStream.closeEntry();
if(fileInputStream!=null){
fileInputStream.close();
}
}
}
if(zipOutputStream!=null){
zipOutputStream.close();
}
}
/**
*删除目录下所有的文件;
*@parampath
*/
publicstaticbooleandeleteExcelPath(Filefile){
String[]files=null;
if(file!=null){
files=file.list();
}
if(file.isDirectory()){
for(inti=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。