1. Tambahkan informasi log ekspor ke proyek
2. Impor file JAR yang mengoperasikan file Excel seperti POI-*. JAR dalam proyek
Ekspor Excel adalah untuk meneruskan parameter ke pengontrol sesuai dengan kondisi latar depan, meminta database sesuai dengan parameter, meminta koleksi daftar, dan menghasilkan data excle untuk mengunduh koleksi daftar.
Cuplikan kode:
Contorller.java
/*** Informasi Ekspor* @param Model*/@RequestMapping ("ExportCustomer.do") @systemControllerLog (description = "Database Form ExportExcle") public void ExportCustomer (model ModelMap) {// Todo jika Anda perlu menambahkan kondisi //model.addanributeributeributeributeributeributeributeributeributeributeributeributeributeributeributeributeributeributeributeributing // Dapatkan data yang akan diekspor daftar daftar <cmcustomer> cuslist = customerservice.exportCustomer (model); // Gunakan metode ini untuk menghasilkan Gaya Template Excle HSSFWorkbook Workbook = CustomerService.createExcel (cuslist, permintaan); Format SimpleDateFormat = new SimpleDateFormat ("yyyymmddhhmmss"); // Tentukan Format Nama File Coba {// Tentukan nama excle ISO-8859-1 untuk mencegah string kacau msg = string baru (("Informasi Pelanggan_" + Format.Format (Tanggal Baru ()) + ".xls"). GetBytes (), "ISO-8859-1"); // Gunakan waktu ekspor sebagai nama file response.setContentType ("Application/vnd.ms-excel"); response.addheader ("Disposisi konten", "lampiran; fileName =" + msg); workbook.write (response.getoutputStream ()); } catch (ioException e) {logger.error (e); }} 2. Metode CreateExcel dalam Layanan
Publik HSSFWorkbook CreateExcel (Daftar <CMCustomer> CUSLIST, permintaan httpservletRequest) {// Buat buku web, sesuai dengan file excel hssfworkbook workbook = new hssfworkbook (); // Tambahkan lembar di WebBook, sesuai dengan lembar dalam file Excel hssfsheet sheet = workbook.createSheet ("Tabel Informasi Pelanggan"); // atur lembar lebar kolom.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); // Tambahkan baris 0 header di lembar hssfrow row = sheet.createrow (0); // Buat sel, atur header meja, atur header meja ke tengah hssfcellstyle style = workbook.createCellstyle (); // Buat format centered style.setAlignment (hssfcellstyle.align_center); // dengan border style.setborderbottom (hssfcellstyle.border_thin); // menghasilkan font hssffont font = workbook.createFont (); // font penebalan font.setBoldWeight (hssffont.boldweight_bold); // font ukuran font.setFontheightInpoints ((pendek) 12); // Terapkan font ke gaya saat ini.setFont (font); // atur seluruh kolom ke tengah atau kiri secara terpisah 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 ("nomor seri"); cell.setCellstyle (gaya); sel = row.createCell (1); cell.setCellValue ("nama pelanggan"); cell.setCellstyle (gaya); sel = row.createCell (2); cell.setCellValue ("jenis kelamin"); cell.setCellstyle (gaya); sel = row.createCell (3); cell.setCellValue ("Status"); cell.setCellstyle (gaya); sel = row.createCell (4); cell.setCellValue ("telepon"); cell.setCellstyle (gaya); sel = row.createCell (5); cell.setCellValue ("Mailbox"); cell.setCellstyle (gaya); sel = row.createCell (6); cell.setCellValue ("alamat"); cell.setCellstyle (gaya); untuk (int i = 0; i <cuslist.size (); i ++) {string logTypeDis = ""; baris = sheet.createrow (i + 1); Cmcustomer cmcustomer = cuslist.get (i); // Buat sel dan atur nilai // kolom bernomor ada di hssfcell c1 = row.createCell (0); C1.SetCellstyle (Style2); C1.SetCellValue (I); Hssfcell c2 = row.createCell (1); c2.setCellstyle (style1); c2.setCellValue (cmcustomer.getCustomername ()); // nama pelanggan string sexstr = cmcustomer.getsex (); // jenis kelamin 0: perempuan, 1: pria string seks = ""; if ("1" .Equals (sexstr)) {sex = "Male"; } if ("0" .Equals (sexStr)) {sex = "female"; } Hssfcell c3 = row.createCell (2); // gender c3.setCellstyle (style1); C3.setCellValue (seks); String statusstr = cmcustomer.getStatus (); // Status Pelanggan 1. On-the-Job, 2. Cuti Status String = ""; if ("1" .Equals (statusstr)) {status = "on-the-job"; } if ("2" .Equals (StatUsStr)) {status = "Recign"; } Hssfcell c4 = row.createCell (3); // status c4.setCellstyle (style1); c4.setCellValue (status); String customerId = cmcustomer.getCustomerId (); // Daftar ID Pelanggan <cmPhone> phoneList = cmphonemapper.selectbycustomerId (customerId); String phone = ""; if (phoneList! = null && phoneList.size ()> 0) {for (int j = 0; j <phoneList.size (); j ++) {phone = phoneList.get (j) .getphone (); }} Hssfcell c5 = row.createCell (4); // telektan c5.setCellstyle (style1); c5.setCellValue (telepon); Daftar <cmemail> emaillist = cmemailmapper.selectall (customerid); String email = ""; if (emaillist! = null && emaillist.size ()> 0) {for (int j = 0; j <emaillist.size (); j ++) {email = emaillist.get (j) .getemail (); }} Hssfcell c6 = row.createCell (5); // emailbox c6.setcellstyle (style1); C6.SetCellValue (email); Cmaddress cmaddress = cmaddress baru (); cmaddress.setCustomerId (customerId); Daftar <Cmaddress> addersslist = cmaddressmapper.selectall (cmaddress); String 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); // alamat c7.setcellstyle (style1); C7.SetCellValue (Adderss); // Gunakan format format default.createCell (1) .setCellValue (cmcustomer.getCustomername ()); Row.CreateCell (2) .setCellValue (Sex); row.createCell (3) .setCellValue (status); row.createCell (4) .setCellValue (telepon); Row.CreateCell (5) .setCellValue (email); row.createCell (6) .setCellValue (adderss); } mengembalikan buku kerja; } 3. Halaman jsp panggilan
// Ekspor Fungsi Informasi ExporBTN () {$ .Ajax ({type: "Post", URL: "<%= Path%>/Customer/ExportCustomer.do", Success: Function (data) {window.open ('<%= Path%>/customer/exportCustomer.do');}}); }Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.