Wenn Sie an Systemprojekten arbeiten, müssen Sie häufig Exportfunktionen ausführen, unabhängig davon, ob es sich um Exportieren von Excel oder Exportdateien handelt. Meine folgende Demo ist im Rahmen des Framework von SpringMVC implementiert.
1. In JS müssen Sie nur GET -Modus verwenden, um den Export anzufordern:
$ ('#word-export-btn'). parent (). on ('click', function () {var Promotionword = json.Stringify ($ ('#MainForm'). SerializeObject ());2. Was Sie im Controller tun müssen, ist, die Datei im Datenstromformat auszugeben:
@RequestMapping ("/export") public void export (httpSession Session, String -PromotionWord, httpServletRequest -Anforderung, httpServletResponse -Antwort) löst IOException {User Sessionuser = (Benutzer-) Sitzung.getAttribute ("Benutzer") aus; JsonObject jsonObj = jsonObject.ParseObject (Promotionword); HSSFWORKBOOK WB = PromotionwordService.export (SessionUser.getID (), JSONOBJ); response.setContentType ("application/vnd.ms-excel"); Kalender cal = calendar.getInstance (); SimpledateFormat SDF = new SimpledateFormat ("yyyy-mm-dd"); String Dateiname = "Word-" + SDF.Format (cal.getTime ()) + ".xls"; response.setheader ("Inhaltsdisposition", "Anhang; Dateiname =" + Dateiname); OutputStream ouputStream = response.getOutputStream (); WB.Write (OuputStream); ouputStream.flush (); ouputStream.close (); }3. Im Dienst müssen Daten in die Formatdatei geschrieben werden:
public HSSFWorkbook Export (String userID, jsonObject jsonObj) {HSSFWORKBOOK WB = new HSSFWorkbook (); HSSFSheet Sheet = WB.CreateSheet ("Word"); HSSfrow Row = Sheet.CreateRow (0); HssfcellStyle style = wb.createcellstyle (); style.setAnignment (hssfcellStyle.Align_Center); LIST <PROMOTOTORAD> PWordList; MAP <String, Objekt> map = new HashMap <> (); map.put ("userId", userId); map.put ("schickexistrule", jsonobj.getString ("schickexistrule"); map.put ("Status", jsonobj.getstring ("Status"); Map.put ("Map.put (" Map.put ("qStar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "qssstar", "map.put"; jsonobj.getString ("qsstar")); map.put ("ImpressionCount", jsonobj.getString ("ImpressionCount"); map.put ("selectGroupid", JsonObj.getString ("SelectGroupid"); jsonobj.getString ("word")); long impression = jsonobj.getlong ("impressionCount"); langklick = jsonobj.getLong ("clickCount"); if (impression! getTwentyPercentlists(word);if(pWordList != null && pWordList.size() > 0){map.put("impressionCount", pWordList.get(pWordList.size()-1).getImpressionCount());}else{map.put("impressionCount", 1);}}else if(click != null){PromotionWord word = new PromotionWord (); Word.SetCreatedBy (userId); Word.SetClickCount7 (klick); pWordList.get(pWordList.size()-1).getClickCount());}else{map.put("clickCount", 1);}}List<PromotionWord> list = commonDao.queryList(PROMOTION_WORD_DAO + ".queryExportDataByUser", map);String[] excelHeader = {"Keyword", "Price", "Search Hot", "Werbemittel", "Kauf hot", "Belichtung", "Klick", "Klickzeit", "Werbezeit", "Ausgaben", "durchschnittliche Klicks", "Matched Products", "Geschätztes Ranking", "Status"}; für (int i = 0; i <excelheader.length; i ++) {hssfcell cell = row.createcell (i); cell.setcellValue (excelheader [i]); cell.setCellStyle (Stil); if (i == 0) {Sheet.SetColumnWidth (0, 30*256); } else {Sheet.SetColumnWidth (i, 10*256); }} if (list! 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.getQStar ()); Row.CreateCell (4) .SetCellValue (Word.getBuyCount ()); 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 ()! } else {rank = "" th "+word.getProspectrank ()+" bit ";}} else {rank =" --- ";} row.createcell (12) .setCellValue (Rank); Row.CreateCell (13) .setCellvalue (WordStatus () == 1?Auf diese Weise können Sie klicken, um direkt zu exportieren, und es ist effektiv.
Das obige ist der gesamte Inhalt der einfachen Methode zum Exportieren von Excel -Dateien in Javaweb. Ich hoffe, jeder wird Wulin.com mehr unterstützen ~