1. إضافة معلومات سجل التصدير إلى المشروع
2. استيراد ملف JAR الذي يدير ملفات Excel مثل POI-*. جرة في المشروع
تتمثل Excel Export في تمرير المعلمات إلى وحدة التحكم وفقًا لشروط المقدمة ، والاستعلام عن قاعدة البيانات وفقًا للمعلمات ، والاستعلام عن مجموعة القائمة ، وإنشاء بيانات Excle لتنزيل مجموعة القائمة.
قصاصة الكود:
contorller.java
/*** معلومات التصدير* param model*/ @requestmapping ("orportCustomer.do") SystemControllerLog (description = "تصدير نموذج قاعدة البيانات") public void orportcustomer (modelMap model) {// todo if ezed stize a condition //model.addattribute privation "، namest) ؛ // احصل على قائمة قائمة البيانات المراد تصديرها <CMCUSTOMER> CUSLIST = customerervice.exportCustomer (Model) ؛ // استخدم الطريقة لإنشاء نمط قالب excle HSSFWorkbook Workbook = customerervice.createExcel (Cuslist ، request) ؛ تنسيق SimplEdateFormat = جديد SimplEdateFormat ("Yyyymmddhhmmss") ؛ // تحديد تنسيق اسم الملف جرب {// تحديد اسم Excle ISO-8859-1 لمنع سلسلة MSG = سلسلة جديدة (("معلومات العميل" // استخدم وقت التصدير باعتباره اسم الملف Response.setContentType ("application/vnd.ms-excel") ؛ Response.addheader ("Disposition" ، "المرفق ؛ filename =" + msg) ؛ Workbook.write (response.getOutputStream ()) ؛ } catch (ioException e) {logger.error (e) ؛ }} 2. طريقة CreateExcel في الخدمة
Public HSSFWorkbook CreateExcel (قائمة <CMCUSTOMER> CUSLIST ، HTTPSERVELTREQUEST طلب) {// إنشاء كتاب ويب ، يتوافق مع ملف excel HSSFWORD Workbook = جديد HSSFWORKBOOK () ؛ // أضف ورقة في كتاب الويب ، المقابلة للصفائح الموجودة في ملف HSSFSheet File excel = Workbook.CreateSheet ("جدول معلومات العميل") ؛ // SET COLUME WIDTH SHOET.SetColumnWidth (0 ، 25 * 100) ؛ sheet.setColumnWidth (1 ، 35 * 100) ؛ sheet.setColumnWidth (2 ، 35 * 100) ؛ sheet.setColumnWidth (3 ، 40 * 100) ؛ sheet.setColumnWidth (4 ، 45 * 100) ؛ sheet.setColumnWidth (5 ، 45 * 100) ؛ sheet.setColumnWidth (6 ، 50 * 100) ؛ sheet.setColumnWidth (7 ، 80 * 100) ؛ sheet.setColumnWidth (8 ، 35 * 100) ؛ sheet.setColumnWidth (9 ، 40 * 100) ؛ // أضف صف 0 من الرأس في الصفوف HSSFrow Row = sheet.createrw (0) ؛ // إنشاء خلية ، قم بتعيين رأس الجدول ، اضبط رأس الجدول على مركز HSSFCellStyle Style = Workbook.CreateCellStyle () ؛ // إنشاء نمط تنسيق محور. // with border style.setBorderBottom (hssfcellstyle.border_thin) ؛ // إنشاء خط خط HSSFFONT = workbook.createfont () ؛ // font font // font size font.setfontheightinpoints (((قصيرة) 12) ؛ // قم بتطبيق الخط على النمط الحالي. // قم بتعيين العمود بأكمله على الوسط أو اليسار بشكل منفصل HSSFCellStyle Style1 = Workbook.CreateCellStyle () ؛ style1.setAlignment (HSSfCellStyle.align_center) ؛ HSSFCellStyle Style2 = Workbook.CreateCellStyle () ؛ style2.setalignment (HSSfCellStyle.Align_left) ؛ HSSFCellStyle Style3 = Workbook.CreateCellStyle () ؛ style3.setalignment (HSSfCellStyle.Align_left) ؛ HSSFFONT HSSFFONT = Workbook.createfont () ؛ hssffont.setColor (hssffont.color_red) ؛ hssffont.setboldweight (hssffont.boldweight_bold) ؛ style3.setFont (Hssffont) ؛ HSSFCellStyle Style4 = Workbook.CreateCellStyle () ؛ style4.setalignment (hssfcellstyle.align_left) ؛ HSSFFONT HSSFFONT1 = Workbook.createfont () ؛ hssffont1.setColor (hssffont.color_normal) ؛ hssffont1.setBoldweight (hssffont.boldweight_bold) ؛ style4.setFont (Hssffont1) ؛ HSSFCell Cell = row.createCell (0) ؛ cell.setCellValue ("الرقم التسلسلي") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (1) ؛ cell.setCellValue ("اسم العميل") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (2) ؛ cell.setCellValue ("الجنس") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (3) ؛ cell.setCellValue ("الحالة") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (4) ؛ cell.setCellValue ("الهاتف") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (5) ؛ cell.setCellValue ("صندوق البريد") ؛ cell.setCellStyle (style) ؛ الخلية = row.createcell (6) ؛ cell.setCellValue ("العنوان") ؛ cell.setCellStyle (style) ؛ لـ (int i = 0 ؛ i <cuslist.size () ؛ i ++) {string logtypedis = "" ؛ الصف = sheet.createrw (i + 1) ؛ cmcustomer cmcustomer = cuslist.get (i) ؛ // قم بإنشاء خلية وضبط القيمة // العمود المرقم على HSSFCELL C1 = ROW.CREATECELL (0) ؛ c1.setCellStyle (style2) ؛ c1.setCellValue (i) ؛ HSSFCell C2 = ROW.CREATECELL (1) ؛ C2.SetCellStyle (style1) ؛ c2.setCellValue (cmcustomer.getCustomerName ()) ؛ // اسم العميل سلسلة sexstr = cmcustomer.getsex () ؛ // الجنس 0: أنثى ، 1: سلسلة sex = "" ؛ if ("1" .equals (sexstr)) {sex = "male" ؛ } if ("0" .equals (sexstr)) {sex = "female" ؛ } hssfcell c3 = row.createcell (2) ؛ // الجنس c3.setCellStyle (style1) ؛ C3.SetCellValue (الجنس) ؛ String statusstr = cmcustomer.getStatus () ؛ // حالة العميل 1. على العمل ، 2. ترك string status = "" ؛ if ("1" .equals (StatusStr)) {status = "on-the-job" ؛ } if ("2" .equals (statusstr)) {status = "resign" ؛ } hssfcell c4 = row.createcell (3) ؛ // status c4.setCellStyle (style1) ؛ C4.SetCellValue (الحالة) ؛ string customerid = cmcustomer.getCustomerId () ؛ // قائمة معرف العميل <CMphone> phonelist = cmphonemapper.selectbyCustomerId (customerId) ؛ سلسلة الهاتف = "" ؛ if (phonelist! = null && phonelist.size ()> 0) {for (int j = 0 ؛ j <phonelist.size () ؛ j ++) {phone = phonelist.get (j) .getPhone () ؛ }} hssfcell c5 = row.createcell (4) ؛ // telect c5.setCellStyle (style1) ؛ C5.SetCellValue (الهاتف) ؛ قائمة <Cmemail> emaillist = cmemailmapper.selectall (customerId) ؛ سلسلة البريد الإلكتروني = "" ؛ if (emaillist! = null && emaillist.size ()> 0) {for (int j = 0 ؛ j <emaillist.size () ؛ j ++) {emaiList.get (j) .getemail () ؛ }} hssfcell c6 = row.createCell (5) ؛ // eilmbox c6.setCellStyle (style1) ؛ c6.setCellValue (البريد الإلكتروني) ؛ cmaddress cmaddress = cmaddress جديد () ؛ cmaddress.setCustomerId (customerId) ؛ قائمة <Cmaddress> addersslist = cmaddressMapper.Selectall (cmaddress) ؛ سلسلة adderss = "" ؛ if (addersSlist! = null && addersslist.size ()> 0) {for (int j = 0 ؛ j <addersslist.size () ؛ j ++) {adderss = addersslist.get (j) .getaddress () ؛ }} hssfcell c7 = row.createcell (6) ؛ // address c7.setCellStyle (style1) ؛ c7.setCellValue (adderss) ؛ // استخدم التنسيق الافتراضي row.createcell (1) .setCellValue (cmcustomer.getCustomerName ()) ؛ row.createcell (2) .SetCellValue (sex) ؛ row.createcell (3) .SetCellValue (الحالة) ؛ row.createcell (4) .SetCellValue (Phone) ؛ row.createcell (5) .SetCellValue (البريد الإلكتروني) ؛ row.createcell (6) .SetCellValue (adderss) ؛ } مهام الإرجاع ؛ } 3. صفحة JSP Call
// وظيفة معلومات التصدير exporbtn () {$ .ajax ({type: "post" ، url: "<٪ = path ٪>/customer/esportcustomer.do" ، النجاح: الوظيفة (البيانات) {window.open ('<٪ = path ٪>/customer/esportcustomer.do') ؛}) ؛ }ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.