Karena persyaratan proyek, data perlu diekspor ke tabel Excel, dan item ekspor dapat dipilih dan diunduh. Kerangka kerja Spring+MyBatis+SpringMVC yang digunakan oleh proyek ini digunakan untuk mengekspor Excel menggunakan Apache PoI. Silakan gunakan POI di Baidu. Tanpa basa -basi lagi, silakan tambahkan kode.
Kode EksporExcelutil
Paket com.rixin.common.util; import java.io.outputStream; impor java.lang.reflect.method; import java.net.urlencoder; import java.util.collection; import java.utilator; import javax.servlet.http.htp.htp.htp.htp.hpserva; org.apache.poi.hssf.usermodel.hssfcell; impor org.apache.poi.hssf.usermodel.hssfcellstyle; import org.apache.poi.hssf.usermodel.hssfrow; impor org.apache.poi.poi.poi org.apache.poi.hssf.usermodel.hssfworkbook; impor org.apache.poi.ss.usermodel.font;/** * Javaee Export Excel Class Berdasarkan Poi * * @Author [email protected] * @see POI POI */Public Class @ParExcel {@parpor @porpel * Nama file * Nama file seperti: "Tabel Siswa" * @param Excelheader * Excel Table Header Array, Menyimpan string format "Nama#", "Nama" adalah baris header excel, dan "nama" adalah nama bidang objek * @param distalist * Koleksi Data @Buku. Pengecualian */Public Static <T> HSSFWorkbook Export (httpservletResponse Response, string fileName, string [] excelheader, collection <t> distalist) melempar Exception {// Atur request response.setContentType ("Application/Application/VND.MS-EXCEL"); response.setHeader ("Disposisi Konten", "Lampiran; FileName =" + Urlencoder.encode (nama file + ".xls", "UTF-8")); // Buat buku kerja, sesuai dengan file excel hssfworkbook wb = hssfworkbook baru (); // Tetapkan gaya judul hssfcellstyle titlestyle = wb.createCellstyle (); // Setel gaya sel perbatasan titlestyle.setbordertop (hssfcellstyle.border_thin); // garis atas garis tepi tipis titlestyle.setborderbottom (hssfcellstyle.border_thin); // titlestyle edge titlestyle. titlestyle.setborderright (hssfcellstyle.border_thin); // garis tepi tipis perbatasan kanan // atur sel perataan sel titlestyle.setalignment (hssfcellstyle.align_center); // horizontal center titlestyle.setVerticalAlignment (hssfcellstyle.vertical_center); // Vertikal Center // Set Font Style Font TitleFont = WB.CreateFont (); titlefont.setFontheightInpoints ((pendek) 15); // tinggi font titlefont.setFontName ("bold"); // font gaya titlestyle.setfont (titlefont); // Tambahkan lembar di buku kerja, sesuai dengan lembar dalam file Excel hssfsheet sheet = wb.createSheet (fileName); // judul string array [] titlearray = string baru [excelheader.length]; // nama bidang array string [] fieldArray = string baru [excelheader.length]; untuk (int i = 0; i <excelheader.length; i ++) {string [] temparray = excelheader [i] .split ("#"); // array sementara split# titlearray [i] = temparray [0]; FieldArray [i] = Temparray [1]; } // Tambahkan judul baris dalam lembar hssfrow baris = sheet.createrow ((int) 0); // Jumlah baris dimulai pada 0 hssfcell sequencecell = row.createCell (0); // kolom sel dimulai pada 0 dan menambahkan nomor urutan sequencecell.setCellValue ("nomor serial"); Sequencecell.setCellstyle (titlestyle); Sheet.AutOsizeColumn (0); // Secara otomatis mengatur lebar // Tetapkan nilai ke baris judul untuk (int i = 0; i <titlearray.length; i ++) {hssfcell titlecell = row.createCell (i +1); // Bit 0 ditempati oleh nomor serial, so +1 titlecell. titlecell.setcellstyle (titlestyle); sheet.autosizeColumn (i + 1); // Bit 0 ditempati oleh nomor seri, jadi +1} // Gaya data perlu diatur secara terpisah karena gaya judul dan data yang berbeda, jika tidak, ia akan menimpa HSSFCellStyle DataTyle = WB.CreateCellstyle (); // Set Data Border DataTyle.setborderbottom (hssfcellstyle.border_thin); DataTyle.SetBordERTOP (HSSFCELLSTYLE.BORDER_THIN); DataTyle.SetBorderleft (hssfcellstyle.border_thin); DataTyle.SetBorderRight (hssfcellstyle.border_thin); // atur center style datastyle.setAlignment (hssfcellstyle.align_center); // Horizontal Center DataTyle.setVerticalAlignment (hssfcellstyle.vertical_center); // Vertikal Center // Setel data font font datafont = wb.createFont (); DataFont.SetFontheightInpoints ((pendek) 12); // font tinggi datafont.setFontName ("安"); // font datastyle.setfont (datafont); // lintasi data pengumpulan dan hasilkan iterator baris data <t> it = datalist.iterator (); INT INDEX = 0; while (it.hasnext ()) {index ++; // 0 bit ditempati SO +1 ROW = sheet.createrow (index); // Tetapkan nilai ke nomor urutan hssfcell sequenceCellValue = row.createCell (0); // Nilai angka urutan selalu merupakan sekuenseCellValue kolom ke -0.setCellValue (index); SequenceCellValue.SetCellstyle (DataTyle); sheet.autosizeColumn (0); T t = (t) it.next (); // Menggunakan refleksi, secara dinamis panggil metode getxxxx () yang sesuai berdasarkan array nama bidang yang diteruskan, dapatkan nilai atribut untuk (int i = 0; i <fieldarray.length; i ++) {hssfcell datacell = row.createCell (i+1); DataCell.SetCellStyle (DataTyle); sheet.autosizeColumn (i + 1); String fieldName = fieldArray [i]; String getMethodName = "get" + fieldname.substring (0, 1) .touppercase () + fieldname.substring (1); // Dapatkan kelas metode getxxx () yang sesuai <?? memperluas objek> tcls = t.getClass (); // generik adalah objek dan subkelas dari semua objek metode getMethod = tcls.getMethod (getMethodname, kelas baru [] {}); // dapatkan nilai objek yang sesuai = getMethod.invoke (t, objek baru [] {}); datacell.setCellValue (value.toString ()); // Tetapkan nilai ke kolom saat ini}}} outputStream outputStream = response.getoutputStream (); // Open Stream WB.write (outputStream); // hssfwork write stream wb.close (); // outputStream); outputStream.close (); // tutup aliran pengembalian wb; } // xssfcellstyle.align_center Center Alignment // xssfcellstyle.align_left Left Alignment // xssfcellstyle.align_right Alignment kanan // xssfcellstyle.vertical_top alignment // xssfcelltyle.vertikal_top XSSFCellStyle.VERTICAL_BOTTOM in the right // CellStyle.BORDER_DOUBLE double-sided line // CellStyle.BORDER_THIN thin edge // CellStyle.BORDER_MEDIUM medium edge // CellStyle.BORDER_DASHED dotted edge // CellStyle.BORDER_HAIR small dot dotted edge // CellStyle.BORDER_THICK thick edge}
Kode pengontrol
@RequestMapping ("/exportProject.do") public void ExportProject (respons httpservletResponse, string ekspor) {string [] excelheader = export.split (","); Daftar <poject> ProjectList = ProjectService.getProjects (NULL); coba {ExportExcelutil.Export (Respons, "Tabel Proyek", Excelheader, ProjectList); } catch (Exception e) {E.PrintStackTrace (); }} Bagian dari kode JSP
... <div style = "margin-top: 5px;"> <span> <input type = "centang kotak" value = "nama proyek#name" name = "export" checked = "checked"> </span> <input type = "text" value = "name proyek" dinonaktifkan = "cacat"> </div> ...
Gambar di bawah ini
Di atas adalah semua tentang artikel ini, saya harap ini akan membantu untuk pembelajaran semua orang.