เมื่อทำงานในโครงการระบบคุณมักจะต้องทำฟังก์ชั่นการส่งออกไม่ว่าจะเป็นการส่งออกไฟล์ Excel หรือส่งออกไฟล์ CVS การสาธิตด้านล่างของฉันถูกนำไปใช้ภายใต้กรอบของ SpringMVC
1. ใน JS คุณจะต้องใช้โหมด GET เพื่อขอการส่งออก:
$ ('#word-export-btn'). parent (). on ('คลิก', function () {var promotionword = json.stringify ($ ('#mainform'). serializeObject ()); location.href = "$ {ctx}2. สิ่งที่คุณต้องทำในคอนโทรลเลอร์คือการส่งออกไฟล์ในรูปแบบสตรีมข้อมูล:
@RequestMapping ("/export") โมฆะสาธารณะส่งออก (เซสชัน httpsession, สตริงโปรโมชั่นคำ, httpservletrequest คำขอ, httpservletResponse การตอบสนอง) โยน ioexception {user sessionuser = (ผู้ใช้) เซสชัน Session.getAttribute ("ผู้ใช้"); jsonObject jsonobj = jsonobject.parseObject (คำส่งเสริมการขาย); HSSFWORKBOOK WB = PROMOTIONWORDSERVICE.EXPORT (SessionUser.getId (), JSONOBJ); Response.SetContentType ("Application/vnd.ms-excel"); ปฏิทิน cal = calendar.getInstance (); SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD"); String filename = "word-" + sdf.format (cal.getTime ()) + ".xls"; Response.SetheAder ("เนื้อหา-การจัดสรร", "สิ่งที่แนบมา; ชื่อไฟล์ =" + ชื่อไฟล์); outputStream ouputstream = response.getOutputStream (); wb.write (ouputstream); ouputstream.flush (); ouputstream.close (); -3. ในบริการต้องเขียนข้อมูลไปยังไฟล์รูปแบบ:
การส่งออก hssfworkbook สาธารณะ (String userId, jsonobject jsonobj) {hssfworkbook wb = hssfworkbook ใหม่ (); แผ่น hssfsheet = wb.createsheet ("word"); hssfrow row = sheet.createrow (0); สไตล์ hssfcellstyle = wb.createCellstyle (); style.setAlignment (hssfcellstyle.align_center); รายการ <Somotionword> pWordList; แผนที่ <สตริง, วัตถุ> แผนที่ = ใหม่ hashmap <> (); map.put ("userId", userId); map.put ("checkexistrule", jsonobj.getString ("checkexistrule")); map.put ("สถานะ", jsonobj.getstring jsonobj.getString ("qsstar")); map.put ("impressionCount", jsonobj.getString ("impressionCount")); map.put ("selectgroupid", jsonobj.getString ("selectgroupid")); map.put ("ischeck", jsonobj. jsonobj.getString ("Word")); ความประทับใจยาว = jsonobj.getLong ("impressionCount"); long click = jsonobj.getLong ("clickCount"); ถ้า (impression! getTwentypercentLists (Word); if (pWordList! = null && pWordList.size ()> 0) {map.put ("impressionCount", pWordList.get (pWordList.Size ()-1) คำส่งเสริมการขายใหม่ (); word.setCreatedby (userId); word.setClickCount7 (คลิก); pWordList = getTwentypercentLists (Word); ถ้า (pWordList! = null && pWordList.size ()> 0) {map.put ("clickCount" PWORDLIST.GET (PWORDLIST.SIZE ()-1) .GetClickCount ());} else {map.put ("clickCount", 1);}} รายการ <SfotionWord> รายการ = commondao.QueryList ร้อน "," การจัดอันดับโปรโมชั่น "," ซื้อร้อน "," เปิดรับแสง "," คลิกผ่าน "," เวลาคลิกผ่าน "," เวลาส่งเสริมการขาย "," ใช้จ่าย "," คลิกเฉลี่ย "," ผลิตภัณฑ์ที่ตรงกัน "," การจัดอันดับโดยประมาณ "," สถานะ "}; สำหรับ (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) สำหรับ (int i = 0; i <list.size (); i ++) {row = sheet.createrow (i+1); คำโปรโมชั่นคำ = 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 = 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 road = ""; if (word.getMatchCount ()! = null && word.getMatchCount ()! = 0) {ถ้า (word.get.getProspectrank () == null || word.getProspectrank () == 0L) {road = "ตำแหน่งอื่น"; } else {rank = "" th "+word.getProspectrank ()+" บิต ";}} else {road =" --- ";} row.createCell (12) .setCellValue (อันดับ); row.createcell (13) .setCellValue (Word.getStatus ()ด้วยวิธีนี้คุณสามารถคลิกเพื่อส่งออกโดยตรงและจะมีประสิทธิภาพ
ข้างต้นเป็นเนื้อหาทั้งหมดของวิธีการง่าย ๆ ในการส่งออกไฟล์ Excel ใน Javaweb ที่มาถึงคุณ ฉันหวังว่าทุกคนจะสนับสนุน wulin.com เพิ่มเติม ~