当前位置: 移动技术网 > IT编程>开发语言>Java > java动态导出excel压缩成zip下载的方法

java动态导出excel压缩成zip下载的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

中美航线运力增加,傅铁山,秦宜智家庭背景

本文实例为大家分享了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;
import java.text.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 
 */
 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(!file.exists()){
  //创建文件;
  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 = workbook.createworkbook(new file(path+"/" + new simpledateformat("yyyymmddhhmmsss").format(new date() )+"_"+(i+1)+".xls"));
  //创建第一个sheet文件;
  writablesheet sheet = workbook.createsheet("导出excel文件", 0);
  //设置默认宽度;
  sheet.getsettings().setdefaultcolumnwidth(30);
  
  //设置字体;
  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 = excelfile.getname();
  fileinputstream = new fileinputstream(excelfile);
  //放入压缩zip包中;
  zipoutputstream.putnextentry(new zipentry(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();
 }
 }
 
 /**
 * 删除目录下所有的文件;
 * @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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网