The examples in this article share with you the specific code for dynamically exporting Excel to zip download for Java for your reference. The specific content is as follows
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 compressed file instance* add by Zhou Haitao* @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"; //Create a folder; createFile(path); //Create Excel file; createExcelFile(path); //Create .zip file; craeteZipPath(path); //Delete all files in the directory; File file = new File(path); //Delete file; deleteExcelPath(file); //Recreate the file; file.mkdirs(); } /** * Create a folder; * @param path * @return */ public static String createFile(String path){ File file = new File(path); //Determine whether the file exists; if(!file.exists()){ //Create a file; boolean bol = file.mkdirs(); if(bol){ System.out.println(path+" path was created successfully!"); }else{ System.out.println(path+" path creation failed!"); } }else{ System.out.println(path+" file already exists!"); } return path; } /** * Create Excel file in the specified directory; * @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++){ //Create Excel; WritableWorkbook workbook = Workbook.createWorkbook(new File(path+"/" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date() )+"_"+(i+1)+".xls")); //Create the first sheet file; WritableSheet sheet = workbook.createSheet("Export Excel file", 0); //Set the default width; sheet.getSettings().setDefaultColumnWidth(30); //Set the font; WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED); WritableCellFormat cellFormat1 = new WritableCellFormat(font1); //Set the background color; cellFormat1.setBackground(Colour.BLUE_GREY); //Set the border; cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT); //Set automatic line wrap; cellFormat1.setWrap(true); //Set text centering alignment; cellFormat1.setAlignment(Alignment.CENTRE); //Set vertical centering; cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE); //Create cell Label label1 = new Label(0, 0, "The first cell in the first row (test whether to automatically wrap it!), cellFormat1); Label label2 = new Label(1, 0, "The second cell in the first row", cellFormat1); Label label3 = new Label(2, 0, "The third cell in the first row", cellFormat1); Label label4 = new Label(3, 0, "The fourth cell in the first row", cellFormat1); //Add to the row; sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); //Set the background, font color, alignment, etc. for the second row; 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); //Create cell; Label label11= new Label(0, 1, "The first cell in the second row (test whether to wrap the line automatically!)",cellFormat2); Label label22 = new Label(1, 1, "The second cell in the second row",cellFormat2); Label label33 = new Label(2, 1, "The third cell in the second row",cellFormat2); Label label44 = new Label(3, 1, "The fourth cell in the second row",cellFormat2); sheet.addCell(label11); sheet.addCell(label22); sheet.addCell(label33); sheet.addCell(label44); //Write to Excel table; workbook.write(); //Close stream; workbook.close(); } } /** * Generate .zip file; * @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); //Put in a compressed zip package; zipOutputStream.putNextEntry(new ZipEntry(path + "/"+fileName)); //Read the file; while((len=fileInputStream.read(buf)) >0){ zipOutputStream.write(buf, 0, len); } //Close; zipOutputStream.closeEntry(); if(fileInputStream != null){ fileInputStream.close(); } } } if(zipOutputStream !=null){ zipOutputStream.close(); } } /** * Delete all files in the directory; * @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("Delete successful!"); }else{ System.out.println("Delete failed!"); } } } return file.delete(); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.