Lorsque vous travaillez sur des projets système, vous devez souvent effectuer des fonctions d'exportation, qu'il s'agisse d'exporter des fichiers CVS Excel ou d'exportation. Ma démo ci-dessous est implémentée dans le cadre de SpringMVC.
1. Dans JS, il vous suffit d'utiliser le mode Get pour demander l'exportation:
$ ('# word-export-btn'). parent (). sur ('cliquez', fonction () {var promotionword = json.stringify ($ ('# mainform'). serializeObject ()); location.href = "$ {ctx} / promotionword / export? Promotionword =" + promotionword;});2. Ce que vous devez faire dans le contrôleur est de sortir le fichier au format de flux de données:
@RequestMapping ("/ Export") public void Export (HTTPSession Session, String Promotionword, httpServLetRequest Request, httpServletResponse réponse) lance ioException {user sessionUser = (user) session.getAttribute ("user"); JSONObject JSONOBJ = JSONObject.ParseObject (Promotionword); Hssfworkbook wb = promotionwordService.export (sessionUser.getId (), jsonObj); Response.SetContentType ("Application / Vnd.ms-Excel"); Calendar cal = calendar.getInstance (); SimpledateFormat sdf = new SimpledateFormat ("yyyy-mm-dd"); String filename = "word-" + sdf.format (cal.gettime ()) + ".xls"; Response.SetHeader ("Content-Disposition", "Pixe; FileName =" + FileName); OutputStream ouputStream = réponse.getOutputStream (); WB.Write (ouputStream); ouputStream.flush (); ouputStream.close (); }3. Dans le service, les données doivent être écrites dans le fichier de format:
Export HSSFWorkBook public (String UserId, JSONObject JSONOBJ) {hssfworkBook wb = new hssfworkbook (); Hssfsheet sheet = wb.createSheet ("word"); Hssfrow row = sheet.createrow (0); HSSFCELLSTYLE STYLE = WB.CreateCellStyle (); style.setAlignment (hssfCellStyle.align_center); List <promotionword> pwordlist; map <string, object> map = new hashmap <> (); map.put ("userId", userid); map.put ("checkExistrule", jsonobj.getString ("checkexistrule")); map.put ("status", jsonobj.getString ("status"); map.put ("qsstar", ", jsonobj.getString ("qsstar")); map.put ("impressionCount", jsonobj.getString ("impressionnount")); map.put ("selectGroupId", jsonobj.getString ("selectGroupId")); map.put ("ischeck", jsonoBj.getString ("isCheck")); JSONOBJ.GetString ("Word")); Long Impression = JSONOBJ.GetLong ("ImpressionCount"); Long Click = JSONOBJ.GetLong ("ClickCount"); if (Impression! = Null) {Promotionword Word = Promotionword (); Word.SetCreatedBy (Userid); Word.SetpressionCount7 (Impression); P Wordlist = getTwentyPecentlists (word); if (pwordlist! = null && pwordlist.size ()> 0) {map.put ("impressionntcount", pwordlist.get (pwordlist.size () - 1) .getImpressionCount ());} else {map.put ("ImpressionCount", 1);}} else if (clique Promotionword (); word.setCreatedBy (userId); word.setClickCount7 (cliquez); pwordList = getTwentyPecentLists (word); if (pwordlist! = Null && pwordlist.size ()> 0) {map.put ("clickCount", pwordlist.get (pwordlist.size () - 1) .getClickCount ());} else {map.put ("ClickCount", 1);}} list <promotionword> list = Commondao.QueryList (ProMotion_Word_Dao + ".QueryExportDatabyusers", Map); String [] Excelheader = {"Keyword", ",", ". "Évaluation promotionnelle", "achat chaud", "exposition", "clics-through", "temps de clics", "temps de promotion", "dépenser", "clics moyens", "produits assortis", "classement estimé", "statut"}; for (int i = 0; i <excelHeader.length; i ++) {hssfcell cell = row.createCell (i); Cell.SetCellValue (ExcelHeader [i]); Cell.SetCellStyle (style); if (i == 0) {sheet.setColumnWidth (0, 30 * 256); } else {sheet.setColumnWidth (i, 10 * 256); }} if (list! = null && list.size ()> 0) pour (int i = 0; i <list.size (); i ++) {row = sheet.createrow (i + 1); Promotionword word = list.get (i); row.CreateCell (0) .SetCellValue (word.getword ()); row.CreateCell (1) .SetCellValue (word.getprice () + ""); row.CreateCell (2) .SetCellValue (word.getSearchCount ()); row.CreateCell (3) .SetCellValue (word.getqsstar ()); row.CreateCell (4) .SetCellValue (word.getBcuyCount ()); row.CreateCell (5) .SetCellValue (word.getImpressionCount7 ()); row.CreateCell (6) .SetCellValue (word.getClickCount7 ()); row.CreateCell (6) .SetCellValue (word.getClickCount7 ()); if (word.getClickCount7 () == 0l) {Row.CreateCell (7) .SetCellValue ("0,00%"); } else {decimalformat df = new decimalformat ("0,00%"); Row.CreateCell (7) .SetCellValue (df.format ((double.valueof (word.getClickCount7 ())) / double.valueof (word.getImpressionCount7 ())))); } row.CreateCell (8) .SetCellValue (word.getOnlinetime7 ()); row.CreateCell (9) .SetCellValue (word.getCost7 () + ""); row.CreateCell (10) .SetCellValue (word.getAvgCost7 () + ""); row.CreateCell (11) .SetCellValue (word.getMatchCount ()); String rank = ""; if (word.getMatchCount ()! = null && word.getMatchCount ()! = 0) {if (word.getPropostrank () == null || word.getProposctrank () == 0l) {rank = "autre position"; } else {rank = "" th "+ word.getprospectrank () +" bit ";}} else {rank =" --- ";} row.createcell (12) .setCellValue (Rank); row.createcell (13) .setcellValue (word.getStatus () == 1?" Pause ":" start ");} wb;}De cette façon, vous pouvez cliquer pour exporter directement et ce sera efficace.
Ce qui précède est l'intégralité du contenu de la méthode simple d'exportation des fichiers Excel dans Javaweb qui vous a été apporté. J'espère que tout le monde soutiendra Wulin.com plus ~