国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java動態導出excel壓縮成zip下載的方法

2024-07-14 08:41:35
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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(); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁明县| 米脂县| 师宗县| 泽州县| 宁安市| 嫩江县| 布拖县| 汉寿县| 宁蒗| 左贡县| 上饶市| 康乐县| 嘉善县| 乌鲁木齐县| 宜州市| 咸阳市| 壤塘县| 墨竹工卡县| 化德县| 玉田县| 汉沽区| 娱乐| 汶川县| 唐河县| 衡水市| 柘城县| 荔波县| 耿马| 多伦县| 太康县| 天镇县| 蛟河市| 鱼台县| 聂拉木县| 栾城县| 凤翔县| 资阳市| 大理市| 金门县| 新巴尔虎右旗| 台东县|