Contoh -contoh dalam artikel ini berbagi dengan Anda kode spesifik untuk mengekspor secara dinamis Excel ke zip unduhan untuk java untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Paket pack.java.io.demo; import java.io.bufferedoutputStream; impor java.io.file; impor java.io.fileinputStream; impor java.io.fileOutputStream; impor java.io.ioexception; impor java.text.simpledateFate; 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; impor jxl.write.label; impor jxl.write.writableCellformat; import jxl.write.writablefont; impor JXL.Write.writablesheet; Import; writEffbook; Impor JXL.Write.Write.writeExcepion; writException; writException; Impor Jxl.write.write.writeException; /** * Instance file terkompresi zip * Tambahkan oleh zhou haitao * @author administrator * */kelas publik zipdemo {/** * @param args * @throws ioException * @throws writeException * @throws rowsexedException */public static void Main (string [] {String path = "c:/document/excel"; // Buat folder; createFile (Path); // Buat file Excel; createExcelfile (Path); // Buat file .zip; Craetezippath (Path); // hapus semua file di direktori; File file = file baru (path); // hapus file; deleteexcelpath (file); // buat ulang file; file.mkdirs (); } /*** Buat folder; * @param path * @return */ public static String createFile (string path) {file file = file baru (path); // Tentukan apakah ada file; if (! File.exists ()) {// Buat file; boolean bol = file.mkdirs (); if (bol) {system.out.println (path+"path berhasil dibuat!"); } else {System.out.println (Path+"Path Creation Gagal!"); }} else {System.out.println (path+"file sudah ada!"); } return path; } /*** Buat file Excel di direktori yang ditentukan; * @param path * @throws ioException * @throws writeException * @throws RowsexededException */public static void createExcelfile (string path) melempar ioException, rowsexededException, writeException {for (int i = 0; i <3; i ++) {for (int i = 0; i <3; i ++) {for (int i = 0; i <3; i ++) {for (int i = 0; i <3; i+++) {for (int i = 0; Workbook workbook = workbook.createworkBook (file baru (path+"/"+new SimpleDeFormat ("yyyymmddhhmmsss"). Format (tanggal baru ())+"_"+(i+1)+". Xls")); // Buat file lembar pertama; WritableSheet sheet = workbook.createSheet ("Ekspor file excel", 0); // atur lebar default; sheet.getSettings (). setDefaultColumnWidth (30); // atur font; WritableFont font1 = writablefont baru (writablefont.arial, 14, writablefont.bold, false, underlinestyle.no_underline, colour.red); WritableCellFormat cellFormat1 = new WritableCellFormat (font1); // atur warna latar belakang; cellFormat1.setBackground (colour.blue_grey); // atur perbatasan; cellFormat1.setBorder (border.all, borderlinestyle.dash_dot); // atur bungkus garis otomatis; cellFormat1.setWrap (true); // Atur penyelarasan pusat teks; CellFormat1.setAlignment (Alignment.Centre); // atur pemusatan vertikal; cellFormat1.setVerticalAnlignment (vertikalalignment.centre); // Buat label sel label1 = label baru (0, 0, "Sel pertama di baris pertama (uji apakah akan secara otomatis membungkusnya!), CellFormat1); label label2 = label baru (1, 0," sel kedua di baris pertama ", label label 3, label baru (2, 0," sel ketiga di baris pertama ", sel fourFormat1); label baru) (2, 2," sel ketiga di baris pertama ", sel CellFormat1); label The Firp Label (2, 2," sel pertama "sel pertama", CellFormat1); label 3, 0, "CellFormat1); Label Label (2, 2," Sel ketiga di baris pertama ", sel CellFormat1); label The Cell); CellFormat1); Writablefont (Writable.arial, 14, writable.no_bold, false, underlinestyle.no_underline, colour.blue2); Borderlinestyle.Thin); 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(); } } / ** * menghasilkan file .zip; * @param path * @throws IoException * / public static void craetezippath (string path) melempar ioException {zipoutputStream zipoutputStream = null; ZipOutputStream = ZipoutputStream baru (baru BufferOutputStream (FileOnputStream (File))); String fileName = excelfile.getName (); zipoutputStream.write (buf, 0, len); Path */ Public boolean deleteexcelpath (file file) {string [] file = null if (file! = null) {file = file.list (); System.out.println (Hapus Sukses! ");Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.