عند العمل على مشاريع النظام ، غالبًا ما تحتاج إلى القيام بوظائف التصدير ، سواء كان ذلك هو تصدير ملفات Excel أو تصدير ملفات CVS. يتم تنفيذ العرض التوضيحي الخاص بي أدناه تحت إطار springMVC.
1. في JS ، تحتاج فقط إلى استخدام وضع Get لطلب التصدير:
$ ('#Word-Export-Btn'). Parent (). on ('click' ، function () {var propervionword = json.stringify ($ ('#mainform'). serializeObject ()) ؛ location.href = "$ {ctx}/proderiOnword/export؟2. ما عليك القيام به في وحدة التحكم هو إخراج الملف بتنسيق دفق البيانات:
@requestmapping ("/export") public void تصدير (جلسة httpsession ، ترويج السلسلة ، طلب httpservletrequest ، استجابة httpservletresponse) يلقي ioException {user sessionuser = (المستخدم) session.getattribute ("user") ؛ jsonobject jsonobj = jsonobject.parseObject (الترويج) ؛ HSSfworkbook WB = الترويج wordwordservice.export (SessionUser.getId () ، jsonobj) ؛ استجابة. Calendar Cal = Calendar.getInstance () ؛ SimplEdateFormat SDF = جديد spiStFormat ("Yyyy-MM-DD") ؛ اسم filename = "word-" + sdf.format (cal.getTime ()) + ".xls" ؛ استجابة. OutputStream ouputstream = response.getOutputStream () ؛ WB.Write (ouputstream) ؛ ouputstream.flush () ؛ ouputstream.close () ؛ }3. في الخدمة ، يجب كتابة البيانات إلى ملف التنسيق:
Public HSSFWorkbook Export (String userId ، JSonObject JSonobj) {HSSFWorkbook WB = New HSSFWorkBook () ؛ ورقة HSSFSHEET = WB.CREATESTEET ("كلمة") ؛ hssfrow row = sheet.createrw (0) ؛ HSSFCellStyle Style = WB.CreateCellStyle () ؛ style.setAlignment (HSSfCellStyle.Align_Center) ؛ قائمة <Premplowword> pwordlist ؛ map <string ، object> map = new hashmap <> () ؛ map.put ("userId" ، userId) ؛ map.put ("checkexistrule" ، jsonobj.getString ("checkexistrule")) ؛ map.put ("الحالة" jsonobj.getString ("qsstar")) ؛ map.put ("الانطباع" ، jsonobj.getString ("lrearmcount")) ؛ map.put ("selectgroupid" ، jsonobj.getString ("selectgroupid")) ؛ map.put ( jsonobj.getString ("Word"))) ؛ الانطباع الطويل = jsonobj.getlong ("lexpressioncount") ؛ click long = jsonobj.getlong ("clickcount") ؛ if (الانطباع! = null) getTwentyperCentLists (word) ؛ if (pwordlist! = null && pwordlist.size ()> 0) {map.put ("represscount" ، pwordlist.get (pwordlist.size ()-1). عرض الترويج () ؛ word.setCreatedBy (userId) ؛ word.setClickCount7 (انقر) ؛ pwordlist = getTwentyperCentLists (word) ؛ if (pwordlist! = null && pwordlist.size ()> 0) {map.put ("clickcount" ، pwordlist.get (pwordlist.size ()-1) .getClickCount ()) ؛} آخر {map.put ("clickcount" ، 1) ؛}} قائمة <PromeInword> list = commondao.querylist (regirion_word_dao + ". "التقييم الترويجي" ، "شراء الساخنة" ، "التعرض" ، "النقر إلى الظهور" ، "الوقت إلى وقت النقر" ، "وقت الترويج" ، "الإنفاق" ، "متوسط النقرات" ، "المنتجات المتطابقة" ، "التصنيف المقدر" ، "الحالة"} ؛ لـ (int i = 0 ؛ i <excelheader.length ؛ i ++) {hssfcell cell = row.createcell (i) ؛ cell.setCellValue (excelheader [i]) ؛ cell.setCellStyle (style) ؛ if (i == 0) {sheet.setColumnWidth (0 ، 30*256) ؛ } آخر {sheet.setColumnWidth (i ، 10*256) ؛ }} if (list! = null && list.size ()> 0) لـ (int i = 0 ؛ i <list.size () ؛ i ++) {row = sheet.createrw (i+1) ؛ Wordword 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 ٪") ؛ } آخر {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 ()) ؛ رتبة سلسلة = "" ؛ if (word.getMatchCount ()! = null && word.getMatchCount ()! = 0) {if (word.getProspectRank () == null || word.getProspectRank () == 0l) {rank = "موضع آخر" ؛ } else {rank = "" th "+word.getProspectRank ()+" bit "؛}} else {rank =" --- "؛} row.createcell (12) .SetCellValue (rank) ؛ row.createCell (13) .setCellValue (word.getStatus () == 1؟وبهذه الطريقة ، يمكنك النقر للتصدير مباشرة وستكون فعالة.
ما سبق هو المحتوى الكامل للطريقة البسيطة لتصدير ملفات Excel في Javaweb التي جلبتها إليك. آمل أن يدعم الجميع wulin.com أكثر ~