Les exemples de cet article partagent avec vous le code spécifique pour l'exportation dynamiquement Excel vers le téléchargement zip pour Java pour votre référence. Le contenu spécifique est le suivant
package pack.java.io.demo; Importer java.io.bufferedoutputStream; import java.io.file; import java.io.fileInputStream; importer java.io.fileoutputStream; import java.io.ititil.date; java.util.zip.zipentry; import java.util.zip.zipoutputStream; import jxl.workbook; import jxl.format.alignment; import jxl.format.borter; import jxl.format.borDedernestyle; jxl.write.label; importer jxl.write.writablecellformat; import jxl.write.witablefont; import jxl.write.writableheet; import jxl.write.witableworkwork; import jxl.write.writeException; import jxl.write.biff.rowsexededException; / ** * instance de fichier compressé zip * Ajouter par zhou haitao * @author administrateur * * / public class zipdemo {/ ** * @param args * @throws ioException * @throws writeException * @throws rowsexededexception * / public static void main "C: / Document / Excel"; // Créer un dossier; CreateFile (chemin); // Créer un fichier Excel; CreateExCelfile (chemin); // Créer un fichier .zip; Craetezippath (chemin); // Supprimer tous les fichiers du répertoire; Fichier fichier = nouveau fichier (chemin); // supprimer le fichier; DeleteExcelPath (fichier); // recréer le fichier; file.mkDirs (); } / ** * Créer un dossier; * @param path * @return * / public static String createFile (String path) {file file = new File (path); // déterminer si le fichier existe; if (! file.exists ()) {// créer un fichier; booléen bol = file.mkDirs (); if (bol) {System.out.println (path + "Path a été créé avec succès!"); } else {System.out.println (path + "La création de chemin a échoué!"); }} else {System.out.println (path + "Le fichier existe déjà!"); } chemin de retour; } / ** * Créer un fichier Excel dans le répertoire spécifié; * @param path * @throws ioException * @throws writeException * @throws rowSExededException * / public static void createExCelfile (String Path) lance ioException, rowSExechedException, writeException {for (int i = 0; i <3; i ++) {// create excel; Witchableworkbook workbook = workbook.createworkbook (nouveau fichier (path + "/" + new SimpledateFormat ("yyyymmddhhmmss"). Format (new Date ()) + "_" + (i + 1) + ". Xls")); // Créer le premier fichier de feuille; Writshesheet sheet = workbook.createSheet ("Fichier Excel Excel", 0); // définir la largeur par défaut; sheet.getSettings (). setDefaultColumnWidth (30); // définir la police; WITCABLEFONT FONT1 = NOUVEAU ÉCRITABLEFONT (WITCILABLEFONT.ARIAL, 14, WITCABLEFONT.BOLD, FAUX, Underlinestyle.no_underline, colore.red); WriteableCellFormat CellFormat1 = new WritableCellFormat (FONT1); // définit la couleur d'arrière-plan; CellFormat1.setbackground (colour.blue_grey); // Définit la bordure; CellFormat1.SetBorder (border.all, borderlinestyle.dash_dot); // Définir une enveloppe automatique de ligne; CellFormat1.setwrap (true); // définir l'alignement du centrage de texte; CellFormat1.Setalignment (Alignment.Cente); // Définir le centrage vertical; CellFormat1.SetVerticalAlignment (VerticalAlignment.Cente); // Créer un label de cellule Label1 = new Label (0, 0, "La première cellule de la première ligne (tester s'il faut l'envelopper automatiquement!), CellFormat1); Label Label2 = new Label (1, 0," La deuxième cellule de la première ligne ", CellFormat1); Label Label3 = New Label (2, 0," The Third Cell in the First Row ", First Lormat1); Label Label4 = New Label (3, 0" CellFormat1); // ajoute à la ligne; WritemableFont (WriteableFont.arial, 14, WriteableFont.No_Bold, False, Undernestyle.No_Underline, Colour.Blue2); BorderlineSyle.thin); Row ", CellFormat2); label Label44 = new Label (3, 1," La quatrième cellule de la deuxième ligne ", CellFormat2); sheet.addcell (label11); sheet.addcell (label22); // workbook.closel (Label33); / ** * Générer le fichier .zip; * @param chemin * @throws ioException * / public static void craetezippath (String Path) lance ioException {ZipOutputStream zipOutputStream = null; ZipOutputStream = new ZipOutputStream (new BufferedoutputStream (new FileOutputStream (fichier)); FileName = Excelfile.getName (); 0, Len);} // Close; DeleteExcelPath (fichier file) {string [] files = null; réussi! ");} else {System.out.println (" Delete a échoué! ");}}} return file.delete ();}}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.