1. Ajouter des informations sur le journal d'exportation au projet
2. Importez le fichier JAR qui exploite des fichiers Excel tels que POI - *. Pot dans le projet
Excel Export est pour transmettre des paramètres au contrôleur en fonction des conditions de premier plan, interroger la base de données en fonction des paramètres, interroger la collection de liste et générer des données ExcLE pour télécharger la collection de liste.
Extrait de code:
Contorller.java
/ ** * Informations d'exportation * @param modèle * / @RequestMapping ("exportCustomer.do") @SystemControllerLog (Description = "Database Form ExportExcle") public void exportCustomer (ModelMap Model) {// todia Si vous avez besoin d'ajouter une condition //Model.Addattribute("Username ", Namestr); // Obtenez la liste des données de la liste des données <CMCustomer> CUSList = CustomerService.ExportCustomer (modèle); // Utilisez la méthode pour générer un modèle de modèle Excle HSSFWorkBook Workbook = CustomerService.CreateExcel (CUSList, demande); SimpledateFormat format = new SimpledateFormat ("yyyymmddhhmmss"); // Définir le format de nom de fichier try {// définir le nom exclé ISO-8859-1 pour empêcher la chaîne brouillé msg = new String (("Customer Information_" + format.format (new Date ()) + ".xls"). GetBytes (), "iso-8859-1"); // Utilisez le temps d'exportation comme nom de fichier Response.SetContentType ("Application / vnd.ms-excel"); Response.AddHeader ("Content-Disposition", "Pixe; FileName =" + MSG); workbook.write (réponse.getOutputStream ()); } catch (ioException e) {logger.Error (e); }} 2. Méthode CreateExcel en service
public hssfworkbook CreateExcel (list <ccustomer> cUSList, httpservletRequest request) {// créer un webbook, correspondant à un fichier excel hssfworkbook workbook = new hssfworkbook (); // Ajouter une feuille dans le livre Web, correspondant à la feuille dans le fichier Excel HSSFSheet Sheet = Workbook.CreateTheet ("Table d'information client"); // Définir la largeur de la colonne Sheet.SetColumnWidth (0, 25 * 100); sheet.setColumnWidth (1, 35 * 100); sheet.setColumnWidth (2, 35 * 100); sheet.setColumnWidth (3, 40 * 100); sheet.setColumnWidth (4, 45 * 100); sheet.setColumnWidth (5, 45 * 100); sheet.setColumnWidth (6, 50 * 100); sheet.setColumnWidth (7, 80 * 100); sheet.setColumnWidth (8, 35 * 100); sheet.setColumnWidth (9, 40 * 100); // Ajouter la ligne 0 de l'en-tête dans la feuille hssfrow row = sheet.createrow (0); // Créez une cellule, définissez l'en-tête de table, définissez l'en-tête de la table sur HSSFCellStyle Style = Workbook.CreateCellStyle (); // Créer un style de format centré.SetAlignment (hssfCellStyle.align_center); // avec border style.setborderbottom (hssfcellStyle.border_thin); // générer une police hssffont font = workbook.CreateFont (); // Font épaississant Font.SetBoldweight (hssffont.boldweight_bold); // FONT TAILLE FONT.SetFontheightInpoints ((court) 12); // applique la police au style actuel. // Définissez la colonne entière sur le centre ou la gauche séparément HSSFCellStyle Style1 = Workbook.CreateCellStyle (); style1.setAlignment (hssfCellStyle.align_center); HSSFCELLSTYLE Style2 = Workbook.CreateCellStyle (); style2.SetAlignment (hssfCellStyle.align_left); HSSFCELLSTYLE Style3 = Workbook.CreateCellStyle (); style3.SetAlignment (hssfCellStyle.align_left); Hssffont hssffont = workbook.CreateFont (); hssffont.setColor (hssffont.color_red); hssffont.setboldweight (hssffont.boldweight_bold); style3.setfont (hssffont); HSSFCELLSTYLE Style4 = Workbook.CreateCellStyle (); style4.SetAlignment (hssfCellStyle.align_left); Hssffont hssffont1 = workbook.CreateFont (); hssffont1.setColor (hssffont.color_normal); hssffont1.setboldweight (hssffont.boldweight_bold); style4.setfont (hssffont1); HSSFCELL CELLE = ROW.CREATECELL (0); Cell.SetCellValue ("numéro de série"); Cell.SetCellStyle (style); Cell = row.CreateCell (1); Cell.SetCellValue ("Nom du client"); Cell.SetCellStyle (style); Cell = row.CreateCell (2); Cell.SetCellValue ("Gender"); Cell.SetCellStyle (style); Cell = row.CreateCell (3); Cell.SetCellValue ("statut"); Cell.SetCellStyle (style); Cell = row.CreateCell (4); Cell.SetCellValue ("téléphone"); Cell.SetCellStyle (style); Cell = Row.CreateCell (5); Cell.SetCellValue ("Mailbox"); Cell.SetCellStyle (style); Cell = row.CreateCell (6); Cell.SetCellValue ("adresse"); Cell.SetCellStyle (style); pour (int i = 0; i <cusList.size (); i ++) {String logTypedis = ""; row = sheet.createrow (i + 1); CmCustomer cmCustomer = cUSList.get (i); // Créez une cellule et définissez la valeur // La colonne numérotée est sur le HSSFCELL gauche C1 = Row.CreateCell (0); c1.setCellStyle (style2); c1.setCellValue (i); HSSFCELL C2 = ROW.CREATECELL (1); C2.SetCellStyle (style1); c2.SetCellValue (cmCustomer.getCustomName ()); // Nom du client String Sexstr = cmCustomer.getSEx (); // Gender 0: Femme, 1: Male String Sex = ""; if ("1" .equals (sextr)) {sexe = "mâle"; } if ("0" .equals (sextr)) {sexe = "femelle"; } Hssfcell c3 = row.createCell (2); // Gender C3.SetCellStyle (style1); C3.SetCellValue (sexe); String stattusstr = cmCustomer.getStatus (); // Statut du client 1. On-the-the-job, 2. LETT String status = ""; if ("1" .equals (stattusstr)) {status = "On-the-the-job"; } if ("2" .equals (stattusstr)) {status = "démission"; } Hssfcell c4 = row.createCell (3); // Status C4.SetCellStyle (style1); C4.SetCellValue (statut); String CustomerId = cmCustomer.getCustomerId (); // Customer ID List <CMPHone> Phonelist = cmphonEmapper.SelectByCustomerId (CustomerId); String phone = ""; if (phonélist! = null && phonelist.size ()> 0) {for (int j = 0; j <phonelist.size (); j ++) {phone = phonelist.get (j) .getphone (); }} Hssfcell c5 = row.createCell (4); // Telect C5.SetCellStyle (style1); C5.SetCellValue (téléphone); List <cMemail> emailList = cMemailMapper.Selectall (CustomerId); Chaîne e-mail = ""; if (emaillist! = null && emaillist.size ()> 0) {for (int j = 0; j <emaillist.size (); j ++) {email = emaillist.get (j) .getEmail (); }} Hssfcell c6 = row.createCell (5); // emailbox c6.setCellStyle (style1); c6.setCellValue (e-mail); Cmaddress cmaddress = new Cmaddress (); cmaddress.setCustomerId (CustomerId); List <cmaddress> adderssList = cmaddressmapper.selectall (cmaddress); String AdderSSS = ""; if (adderssList! = null && addersslist.size ()> 0) {for (int j = 0; j <addersslist.size (); j ++) {adderss = addersslist.get (j) .getAddress (); }} Hssfcell c7 = row.createCell (6); // adresse C7.SetCellStyle (style1); C7.SetCellValue (Adderss); // Utilisez le format par défaut Row.CreateCell (1) .SetCellValue (cmcustomer.getCustomEname ()); Row.CreateCell (2) .SetCellValue (sexe); row.CreateCell (3) .SetCellValue (statut); Row.CreateCell (4) .SetCellValue (téléphone); Row.CreateCell (5) .SetCellValue (e-mail); Row.CreateCell (6) .SetCellValue (Adderss); } return workbook; } 3. Page Call JSP
// Fonction d'informations d'exportation exporbtn () {$ .ajax ({type: "post", URL: "<% = path%> / client / exportCustomer.do", succès: fonction (data) {window.open ('<% = path%> / client / exportCustomer.do');}}); }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.