Al trabajar en proyectos del sistema, a menudo debe realizar funciones de exportación, ya sea para exportar Excel o exportar archivos CVS. Mi demostración a continuación se implementa en el marco de SpringMVC.
1. En JS, solo necesita usar el modo Get para solicitar la exportación:
$ ('#Word-Export-BTN'). Parent (). on ('Click', function () {var promocionandoword = json.stringify ($ ('#mainform'). serializeObject ()); ubicación.href = "$ {ctx}/promotionword/export? Promotionword ="+promocionword;});2. Lo que debe hacer en el controlador es generar el archivo en formato de flujo de datos:
@RequestMapping ("/Export") public void export (sesion httpsession, string promotionword, httpservletRequest solicitud, httpServletResponse respuesta) lanza ioexception {user sessionUser = (user) session.getAttribute ("user"); JsonObject jsonobj = jsonObject.ParseObject (Promotionword); HSSFWorkBook wb = PromotionWordService.Export (SessionUser.getID (), JSONOBJ); Response.setContentType ("Aplicación/VND.MS-Excel"); Calendario cal = calendario.getInstance (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd"); Cadena filename = "word-" + sdf.format (cal.gettime ()) + ".xls"; respuesta.setheader ("Disposición de contenido", "Adjunto; FileName =" + FileName); OutputStream OUPUPTSTREAM = Response.getOutputStream (); wb.write (oUptStream); OUPUPTSTREAM.FLUSH (); oUptStream.close (); }3. En el servicio, los datos deben escribirse en el archivo de formato:
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.setalignment (hssfcellstyle.align_center); LIST <MOMCOTionWord> PWordList; MAP <String, Object> MAP = New HashMap <> (); map.put ("userId", userId); map.put ("checkexistrule", jsonobj.getString ("checkExistrule")); map.put ("estado", jsonObj.getstring ("estado")); map.put ("Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar", "Qsstar". jsonObj.getString ("Qsstar")); map.put ("impresionCount", jsonObj.getStRing ("impresionCount")); map.put ("selectGroupId", jsonObj.getString ("selectGroupId")); map.put ("isCheck", jsonObj.getSting ("ischeck"); map.put ("word (" word "," word "," word "," word "," word "," word "," word "," word "," word "," word "," word "," word "," word "," word "," word ". jsonObj.getString ("Word")); Long Impression = jsonObj.getLong ("ImpressionCount"); Long Click = jsonObj.getLong ("DickCount"); if (impresión! = NULL) {Promotionword Word = New PromotionWord (); Word.SetCreatedBy (userID); word.set.setcount7 (impresionante de impresión); pWordList = pWordList getTwentyPercentLists (word); if (pwordList! = null && pwordList.size ()> 0) {map.put ("impresionCount", pwordList.get (pwordList.size ()-1) .getImpressionCount ());} else {MAP.put ("ImpresionCountCounter new PromotionWord (); Word.SetCreatedBy (userId); word.setClickCount7 (clic); pwordList = getTwentyPercentLists (word); if (pwordList! = null && pwordList.size ()> 0) {map.put ("clickcount",,,, pwordList.get (pwordList.size ()-1) .getClickCount ());} else {map.put ("chowCount", 1);}} list <Comotionword> list = Commondao.QueryList (promoción_word_dao + ". PRECIO. PRECIO. Hot "," Calificación promocional "," Comprar caliente "," exposición "," clics "," tiempo de clic "," tiempo de promoción "," gasto "," clics promedio "," productos emparejados "," clasificación estimada "," estado "}; for (int i = 0; i <Excelheader.length; i ++) {hssfcell cell = row.createCell (i); Cell.SetCellValue (ExcelHeader [i]); Cell.SetCellStyle (estilo); if (i == 0) {sheet.setColumnWidth (0, 30*256); } else {sheet.setColumnWidth (i, 10*256); }} if (list! = null && list.size ()> 0) para (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.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%"); fila } row.createCell (8) .SetCellValue (word.getOnlinETime7 ()); Row.CreateCell (9) .SetCellValue (word.getCost7 ()+""); Row.CreateCell (10) .SetCellValue (word.getAvgCost7 ()+""); Row.CreateCell (11) .SetCellValue (word.getMatchCount ()); Cadena rank = ""; if (word.getMatchCount ()! = null && word.getMatchCount ()! = 0) {if (word.getProspectank () == null || word.getProSpectrank () == 0l) {rank = "otra posición"; } else {rank = "" th "+word.getprospectrank ()+" bit ";}} else {rank =" --- ";} row.createCell (12) .setCellValue (rank); filaDe esta manera, puede hacer clic para exportar directamente y será efectivo.
Lo anterior es todo el contenido del método simple de exportar archivos de Excel en Javaweb que se le trae. Espero que todos apoyen a Wulin.com más ~