java动态导出excel压缩成zip下载的⽅法本⽂实例为⼤家分享了java动态导出excel压缩成zip下载的具体代码,供⼤家参考,具体内容如下
package pack.java.io.demo;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* zip压缩⽂件实例
* add by 周海涛
* @author Administrator
*
*/
public class ZipDemo {
/**
* @param args
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
mkdirs方法public static void main(String[] args) throws RowsExceededException, WriteException, IOException {
String path = "C:/document/excel";
//创建⽂件夹;
createFile(path);
//创建Excel⽂件;
createExcelFile(path);
/
/⽣成.zip⽂件;
craeteZipPath(path);
//删除⽬录下所有的⽂件;
File file = new File(path);
//删除⽂件;
deleteExcelPath(file);
//重新创建⽂件;
file.mkdirs();
}
/**
* 创建⽂件夹;
* @param path
* @return
*/
public static String createFile(String path){
File file = new File(path);
//判断⽂件是否存在;
if(!ists()){
//创建⽂件;
boolean bol = file.mkdirs();
if(bol){
System.out.println(path+" 路径创建成功!");
}else{
System.out.println(path+" 路径创建失败!");
}
}else{
System.out.println(path+" ⽂件已经存在!");
}
return path;
}
/**
* 在指定⽬录下创建Excel⽂件;
* @param path
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
public static void createExcelFile(String path) throws IOException, RowsExceededException, WriteException{
for(int i =0;i<3;i++){
//创建Excel;
WritableWorkbook workbook = ateWorkbook(new File(path+"/" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date() )+"_"+(i+1)+".xls")); //创建第⼀个sheet⽂件;
WritableSheet sheet = ateSheet("导出Excel⽂件", 0);
/
/设置默认宽度;
//设置字体;
WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
//设置背景颜⾊;
cellFormat1.setBackground(Colour.BLUE_GREY);
//设置边框;
cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
//设置⾃动换⾏;
cellFormat1.setWrap(true);
//设置⽂字居中对齐⽅式;
cellFormat1.setAlignment(Alignment.CENTRE);
//设置垂直居中;
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
//创建单元格
Label label1 = new Label(0, 0, "第⼀⾏第⼀个单元格(测试是否⾃动换⾏!)",cellFormat1);
Label label2 = new Label(1, 0, "第⼀⾏第⼆个单元格",cellFormat1);
Label label3 = new Label(2, 0, "第⼀⾏第三个单元格",cellFormat1);
Label label4 = new Label(3, 0, "第⼀⾏第四个单元格",cellFormat1);
//添加到⾏中;
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
//给第⼆⾏设置背景、字体颜⾊、对齐⽅式等等;
WritableFont font2 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setBackground(Colour.PINK);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat2.setWrap(true);
//创建单元格;
Label label11= new Label(0, 1, "第⼆⾏第⼀个单元格(测试是否⾃动换⾏!)",cellFormat2);
Label label22 = new Label(1, 1, "第⼆⾏第⼆个单元格",cellFormat2);
Label label33 = new Label(2, 1, "第⼆⾏第三个单元格",cellFormat2);
Label label44 = new Label(3, 1, "第⼆⾏第四个单元格",cellFormat2);
sheet.addCell(label11);
sheet.addCell(label22);
sheet.addCell(label33);
sheet.addCell(label44);
//写⼊Excel表格中;
workbook.write();
//关闭流;
workbook.close();
}
}
/**
* ⽣成.zip⽂件;
* @param path
* @throws IOException
*/
public static void craeteZipPath(String path) throws IOException{
ZipOutputStream zipOutputStream = null;
File file = new File(path+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".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 = Name();
fileInputStream = new FileInputStream(excelFile);
//放⼊压缩zip包中;
zipOutputStream.putNextEntry(new ZipEntry(path + "/"+fileName));
/
/读取⽂件;
while((ad(buf)) >0){
zipOutputStream.write(buf, 0, len);
}
//关闭;
zipOutputStream.closeEntry();
if(fileInputStream != null){
fileInputStream.close();
}
}
}
if(zipOutputStream !=null){
zipOutputStream.close();
}
}
/**
* 删除⽬录下所有的⽂件;
* @param path
*/
public static boolean deleteExcelPath(File file){
String[] files = null;
if(file != null){
files = file.list();
}
if(file.isDirectory()){
for(int i =0;i<files.length;i++){
boolean bol = deleteExcelPath(new File(file,files[i]));
if(bol){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}
return file.delete();
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论