Ao trabalhar em projetos do sistema, geralmente é necessário fazer funções de exportação, seja para exportar o Excel ou Export CVS Arquivos. Minha demonstração abaixo é implementada no âmbito da SpringMVC.
1. Em JS, você só precisa usar o modo GET para solicitar a exportação:
$ ('#word-export-btn'). parent (). on ('clique', function () {var promoçãoword = json.stringify ($ ('#mainform'). SerializeObject ()); location.href = "$ {ctx}/promovionword/exportword?2. O que você precisa fazer no controlador é emitir o arquivo no formato do fluxo de dados:
@RequestMapping ("/Export") Exportar public void (sessão de HttpSession, String PromotionWord, solicitação httpServletRequest, resposta httpSertletResponse) lança ioexception {user SessionUser = (user) session.getAttribute ("usuário"); JsonObject jsonObj = jsonObject.ParseObject (PromotionWord); HSSFWorkbook WB = PromotionWordService.Export (sessionUser.getId (), JsonObj); Response.setContentType ("Application/vnd.ms-excel"); Calendário cal = calendar.getInstance (); SimpledateFormat sdf = new SimpleDateFormat ("AAAA-MM-DD"); String filename = "word-" + sdf.format (cal.gettime ()) + ".xls"; Response.setheader ("Content-Disposition", "Applement; FileName =" + FileName); OutputStream opUtStream = Response.getOutputStream (); wb.Write (OPUTSTREAM); OUPUTSTREAM.FLUSH (); OUPUTSTREAM.CLOSE (); }3. No serviço, os dados precisam ser gravados no arquivo de formato:
public HSSFWorkbook Export (String UserID, JsonObject jsonObj) {HSSFWorkbook WB = new HSSFWorkbook (); Folha de hssfheet = wb.createSheet ("word"); Hssfrow line = sheet.crerowroow (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")); jsonObj.getString ("QSSTAR")); map.put ("ImpressionCount", jsonObj.getString ("ImpressionCount")); map.put ("selectGroupId", jsonObj.getString ("SelectGroupid"); jsonObj.getString ("word"))); longa impressão = jsonObj.getlong ("ImpressionCount"); clique longo = jsonobj.getlong ("clickcount"); if (impressão! = null) {promoção word = new promoção (); gettwentyPercentLists (word); if (pwordList! = null && pwordList.size ()> 0) {map.put ("ImpressionCount", pwordList.get (pwordList.size ()-1) .getImpressionCount ();} mais {map.put.put.! PromotionWord (); Word.SetCreatedBy (UserID); Word.SetClickCount7 (Click); PwordList = GettwentyPercentLists (Word); if (pwordList! = Null && pwordList.size ()> 0) {map.put ("ClickCount", pwordList.get (pwordList.size ()-1) .getClickCount ());} else {map.put ("clickcount", 1);}} list <vroionword> list = commondao.QueryList (Promotion_word_dao + ".Queryerer hawern; Hot "," classificação promocional "," compra quente "," exposição "," clique "," tempo de clique "," tempo de promoção "," gasto "," cliques médios "," produtos correspondentes "," classificação estimada "," status "}; for (int i = 0; i <excelHeader.length; i ++) {hssfcell célula = 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.crerowerow (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%"); row.createCell (7) .setCellValue (df.format ((duplo.valueof (word.getClickCount7 ()))/duplo.valueof (word.getImpressionCount7 ())))); } Row.CreateCell (8) .SetCellValue (Word.TeTonLineTime7 ()); 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.getProspectancy ()+" bit ";}} else {rank =" --- ";} row.createCell (12) .setCellValue (rank); linha;Dessa forma, você pode clicar para exportar diretamente e ele será efetivo.
O exposto acima é o conteúdo inteiro do método simples de exportar arquivos do Excel em Javaweb trazido a você. Espero que todos apoiem mais wulin.com ~