1. เพิ่มข้อมูลบันทึกการส่งออกไปยังโครงการ
2. นำเข้าไฟล์ JAR ที่ใช้งานไฟล์ excel เช่น poi-*. jar ในโครงการ
Excel Export คือการส่งผ่านพารามิเตอร์ไปยังคอนโทรลเลอร์ตามเงื่อนไขเบื้องหน้าตรวจสอบฐานข้อมูลตามพารามิเตอร์สอบถามการรวบรวมรายการและสร้างข้อมูล Excle เพื่อดาวน์โหลดรายการรวบรวมรายการ
รหัสตัวอย่าง:
Contorller.java
/*** ข้อมูลการส่งออก* @param model*/@requestmapping ("ExportCustomer.do") @SystemControllerLog (คำอธิบาย = "รูปแบบฐานข้อมูล exportExcle") โมฆะสาธารณะ exportCustomer (รุ่น modelmap) {// toDo ถ้าคุณต้องการเพิ่มเงื่อนไข //model // รับข้อมูลที่จะส่งออกรายการ <CmCustomer> CUSLIST = customerservice.exportCustomer (รุ่น); // ใช้วิธีการสร้างสมุดงาน hssfworkbook สไตล์ excle เทมเพลต = customerservice.createExcel (cuslist, คำขอ); SimpledateFormat format = new SimpledateFormat ("yyyymmddhhmmss"); // กำหนดรูปแบบชื่อไฟล์ลอง {// กำหนดชื่อ excle iso-8859-1 เพื่อป้องกันสตริงที่อ่านไม่ออก = สตริงใหม่ (("ข้อมูลลูกค้า _" + format.format (วันที่ใหม่ ()) + ".xls"). getBytes (), "ISO-8859-1"); // ใช้เวลาส่งออกเป็นชื่อไฟล์ตอบสนอง SetContentType ("Application/vnd.ms-excel"); Response.addheader ("เนื้อหา-การจัดสรร", "สิ่งที่แนบมา; ชื่อไฟล์ =" + ผงชูรส); workbook.write (response.getOutputStream ()); } catch (ioexception e) {logger.error (e); - 2. วิธีการ CreateEexcel ในการบริการ
public hssfworkbook createEexcel (รายการ <Cmcustomer> cuslist, httpservletrequest Request) {// สร้าง webbook ซึ่งสอดคล้องกับสมุดงาน hssfworkbook ของไฟล์ excel = hssfworkbook ใหม่ (); // เพิ่มแผ่นในเว็บบุ๊คที่สอดคล้องกับแผ่นงานในไฟล์ hssfsheet excel file = workbook.createsheet ("ตารางข้อมูลลูกค้า"); // ตั้งค่าแผ่นความกว้างคอลัมน์ 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.createrow (0); // สร้างเซลล์ตั้งค่าส่วนหัวของตารางตั้งส่วนหัวตารางเป็นศูนย์ HSSFCELLSTYLE = Workbook.CreateCellStyle (); // สร้างรูปแบบ style.setalignment (hssfcellstyle.align_center); // ด้วยสไตล์ชายแดน SetBorderBottom (hssfcellstyle.border_thin); // สร้างตัวอักษร HSSFFONT FONT = Workbook.CreateFont (); // ตัวอักษรหนาฟอนต์ SetBoldweight (hssffont.boldweight_bold); // ตัวอักษรขนาด Font.SetFontheightInpoints ((สั้น) 12); // ใช้แบบอักษรกับสไตล์สไตล์ปัจจุบัน SetFont (FONT); // ตั้งค่าคอลัมน์ทั้งหมดเป็นกึ่งกลางหรือซ้ายแยก 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 (สไตล์); เซลล์ = row.createCell (1); cell.setCellValue ("ชื่อลูกค้า"); cell.setcellstyle (สไตล์); เซลล์ = row.createCell (2); cell.setCellValue ("เพศ"); cell.setcellstyle (สไตล์); เซลล์ = row.createCell (3); cell.setCellValue ("สถานะ"); cell.setcellstyle (สไตล์); เซลล์ = row.createCell (4); cell.setCellValue ("โทรศัพท์"); cell.setcellstyle (สไตล์); เซลล์ = row.createCell (5); cell.setCellValue ("กล่องจดหมาย"); cell.setcellstyle (สไตล์); เซลล์ = row.createCell (6); cell.setCellValue ("ที่อยู่"); cell.setcellstyle (สไตล์); สำหรับ (int i = 0; i <cuslist.size (); i ++) {สตริง logTypedis = ""; row = sheet.createrow (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: เพศชายเพศ = ""; 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. on-the-Job, 2. ปล่อยสถานะสตริง = ""; if ("1" .Equals (statusstr)) {status = "on-the-Job"; } if ("2" .Equals (statusstr)) {status = "ลาออก"; } hssfcell c4 = row.createCell (3); // สถานะ c4.setcellstyle (style1); C4.SetCellValue (สถานะ); String customerId = cmcustomer.getCustomerId (); // รายการรหัสลูกค้า <cmphone> phonelist = cmphonemapper.selectbyCustomerId (customerId); สตริงโทรศัพท์ = ""; if (phonelist! = null && phonelist.size ()> 0) {สำหรับ (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); string email = ""; if (emaillist! = null && emaillist.size ()> 0) {สำหรับ (int j = 0; j <emaillist.size (); j ++) {email = emaillist.get (j) .getEmail (); }} hssfcell c6 = row.createCell (5); // emailbox c6.setcellstyle (style1); C6.SetCellValue (อีเมล); cmaddress cmaddress = ใหม่ cmaddress (); cmaddress.setCustomerID (CustomerID); รายการ <Cmaddress> addersSlist = cmaddressMapper.selectall (cmaddress); สตริง adderss = ""; if (addersslist! = null && addersslist.size ()> 0) {สำหรับ (int j = 0; j <addersslist.size (); j ++) {adderss = addersslist.get (j) .getAddress (); }} hssfcell c7 = row.createCell (6); // ที่อยู่ c7.setcellstyle (style1); C7.SetCellValue (Adderss); // ใช้แถวรูปแบบเริ่มต้น CreateCell (1) .SetCellValue (cmcustomer.getCustomerName ()); row.createCell (2) .setCellValue (เพศ); row.createCell (3) .setCellValue (สถานะ); row.createCell (4) .setCellValue (โทรศัพท์); row.createCell (5) .setCellValue (อีเมล); row.createCell (6) .setCellValue (adderss); } ส่งคืนเวิร์กบุ๊ก; - 3. การโทรหน้า JSP
// ฟังก์ชั่นข้อมูลการส่งออก exporbtn () {$ .ajax ({ประเภท: "โพสต์", url: "<%= path%>/ลูกค้า/exportCustomer.do", ความสำเร็จ: ฟังก์ชั่น (ข้อมูล) {window.Open ('<%= path%>/ลูกค้า -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น