시스템 프로젝트 작업을 수행 할 때는 Excel을 내보내거나 CVS 파일을 내보내 든 내보내기 기능을 수행해야합니다. 아래의 데모는 SpringMVC의 프레임 워크에서 구현됩니다.
1. JS에서는 GET 모드 만 사용하여 내보내기를 요청하면됩니다.
$ ( '##word-export-btn'). parent (). on ( 'click', function () {var promotionword = json.stringify ($ ( '#mainform'). serializeobject ()); location.href = "$ {ctx}/promotionword/export? promotionword ="+promotionword;});2. 컨트롤러에서해야 할 일은 파일을 데이터 스트림 형식으로 출력하는 것입니다.
@requestmapping ( "/Export") 공개 void 내보내기 (httpsession session, string promotionword, httpservletrequest request, httpservletresponse 응답)가 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"); 문자열 filename = "word-" + sdf.format (cal.gettime ()) + ".xls"; response.setHeader ( "내용화", "첨부 파일; filename =" + filename); outputStream ouputStream = response.getOutputStream (); wb.write (oputstream); OUPUTSTREAM.FLUSH (); OUPUTSTREAM.CLOSE (); }3. 서비스에서 데이터는 형식 파일로 작성해야합니다.
public hssfworkbook 내보내기 (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 <MACTOORDRIST; MAP <String, Object> Map = new Hashmap <> (); map.put ( "userId", userId); map.put ( "checkexistrule", jsonobj.getString ( "checkexistrule")); map.put ( "status", jsonobj.string ( "status"); map.put ( "qs star") jsonobj.getString ( "qsstar")); map.put ( "inffectionCount", jsonobj.getString ( "inffessionCount")); map.put ( "selectGroupId", jsonobj.getString ( "selectGroupId")); map.put ( "ischeck", jsonobj.getString ( "ischeck")); jsonobj.getString ( "Word")); long inffression = jsonobj.getLong ( "inffectionCount"); long click = jsonobj.getLong ( "clickCount"); if (inffection! = null) {empressionword = new PromotionWord (); Word.setCreatedBy (userId); pwore); getTwentyPerCentLists (Word); if (pwordList! = null && pwordList.size ()> 0) {map.put ( "inffectionCount", pwordList.get (pwordList.size () -1) .getImpressionCount ();} else {map.put ( "inffectionCount", 1); 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 <PromotionWord> list = commondao.queryList (promation_word_word_dao + ".QueryExportDatabyUser" 뜨거운 ","프로모션 등급 ","구매 ","노출 ","클릭-스루 ","클릭-스루 시간 ","홍보 시간 ","지출 ","평균 클릭 ","일치하는 제품 ","추정 순위 ","상태 "}; for (int i = 0; i <excelheader.length; i ++) {hssfcell cell = row.createcell (i); cell.setCellValue (ExcelHeader [i]); Cell.setCellStyle (스타일); if (i == 0) {sheet.setColumnWidth (0, 30*256); } else {sheet.setColumnWidth (i, 10*256); }} if (list! = null && list.size ()> 0) for (int i = 0; i <list.size (); i ++) {row = sheet.createrow (i+1); 프로모션 워드 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 = 새로운 십진형 ( "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 ()); 문자열 순위 = ""; if (word.getMatchCount ()! = null && word.getMatchCount ()! = 0) {if (word.getPropcectrank () == null || word.getPropcectrank () == 0l) {rank = "기타 위치"; } else {else {rank = "th"+word.getProctrank ()+"bit";}} else {rank = "---";} row.createCell (12) .setCellValue (rank); row.createCell (13) .setCellValue (word.get.getStatus () ==?이런 식으로 클릭하여 직접 내보내기를 클릭하면 효과적입니다.
위는 Javaweb에서 Excel 파일을 내보내는 간단한 방법의 전체 내용입니다. 모두가 wulin.com을 더 지원하기를 바랍니다