memperkenalkan
Jakarta Poi adalah satu set Java API untuk mengakses dokumen yang diformat Microsoft. Jakarta POI terdiri dari banyak komponen, termasuk HSSF untuk mengoperasikan file format Excel dan HWPF untuk mengoperasikan kata. Di antara berbagai komponen, hanya HSSF untuk mengoperasikan Excel yang relatif matang. Beranda Resmi http://poi.apache.org/index.html, dokumentasi API http://poi.apache.org/apidocs/index.html
menyelesaikan
Komentar lengkap telah ditambahkan ke kode.
Impor java.io.fileInputStream; import java.io.fileoutputStream; impor java.io.inputStream; import java.io.outputStream; impor impor. org.apache.poi.hssf.usermodel.hssfcellstyle; impor org.apache.poi.hssf.usermodel.hssfrow; impor org.apache.poi.hssf.usermodel.hssfsheet; impor org.apache.poi.poi.poyshel.hssfsheet; impor org.apache.poi.poi.pousshelshe.hssheet; impor org.apache.poi.pouss ExcelOperate {public static void main (string [] args) {// buat tabel excel createExcel (getStudent ()); // Baca Daftar Tabel Excel <Sidaya> Daftar = ReadExcel (); System.out.println (list.toString ()); } / *** Inisialisasi data** @return data* / Daftar statis pribadi <Sahasiswa> getStudent () {Daftar <Sahasiswa> Daftar = ArrayList baru <spinition> (); Siswa Siswa1 = Siswa Baru ("Xiao Ming", 8, "Kelas Dua"); Siswa Siswa2 = Siswa Baru ("Xiao Guang", 9, "Kelas Dua"); Siswa Siswa3 = Siswa Baru ("Xiao Hua", 10, "Kelas Empat"); List.add (Student1); list.add (student2); list.add (student3); daftar pengembalian; } / ** * Buat Excel * * @param Daftar * Data * / Private Static Void CreateExcel (Daftar <Sahasiswa> Daftar) {// Buat file excel hssfworkbook workbook = new hssfworkbook (); // Buat lembar kerja hssfsheet sheet = workbook.createSheet ("Tabel Siswa Satu"); // Tambahkan baris header hssfrow hssfrow = sheet.createrow (0); // atur format sel ke pusat hssfcellstyle cellstyle = workbook.createCellstyle (); cellstyle.setalignment (hssfcellstyle.align_center); // Tambahkan konten header hssfcell headcell = hssfrow.createCell (0); headcell.setCellValue ("name"); headcell.setcellstyle (CellStyle); headcell = hssfrow.createCell (1); headcell.setCellValue ("usia"); headcell.setcellstyle (CellStyle); headcell = hssfrow.createCell (2); headcell.setCellValue ("grade"); headcell.setcellstyle (CellStyle); // Tambahkan konten data untuk (int i = 0; i <list.size (); i ++) {hssfrow = sheet.createrow ((int) i+1); Siswa Siswa = List.get (i); // Buat sel dan atur nilai sel hssfcell = hssfrow.createCell (0); cell.setCellValue (student.getName ()); cell.setCellStyle (CellStyle); sel = hssfrow.createCell (1); cell.setCellValue (student.getage ()); cell.setCellStyle (CellStyle); sel = hssfrow.createCell (2); cell.setCellValue (student.getGrade ()); cell.setCellStyle (CellStyle); } // Simpan file excel coba {outputStream outputStream = new fileoutputStream ("d: /students.xls"); workbook.write (outputStream); outputStream.close (); } catch (Exception e) {E.PrintStackTrace (); }} / ** * Baca Excel * * @Return Data Collection * / Daftar Statis Pribadi <Sahasiswa> ReadExcel () {Daftar <Sahasiswa> Daftar = ArrayList baru <Sahen> (); HSSFWorkbook Workbook = NULL; coba {// baca file excel inputStream inputStream = new fileInputStream ("d: /students.xls"); workbook = HSSFWorkbook baru (InputStream); inputStream.close (); } catch (Exception e) {E.PrintStackTrace (); } // Loop Worksheet untuk (int numsheet = 0; numsheet <workbook.getNumberofsheets (); numsheet ++) {hssfsheet hssfsheet = workbook.getsheetat (numsheet); if (hssfsheet == null) {lanjutan; } // baris loop untuk (int rownum = 1; rownum <= hssfsheet.getlastrownum (); rownum ++) {hssfrow hssfrow = hssfsheet.getrow (rownum); if (hssfrow == null) {lanjutan; } // Simpan konten di sel ke dalam koleksi siswa siswa = siswa baru (); Hssfcell cell = hssfrow.getcell (0); if (cell == null) {lanjutkan; } student.setname (cell.getStringCellValue ()); sel = hssfrow.getcell (1); if (cell == null) {lanjutkan; } student.setage ((int) cell.getNumericCellValue ()); sel = hssfrow.getcell (2); if (cell == null) {lanjutkan; } student.setGrade (cell.getStringCellValue ()); list.add (siswa); }} daftar pengembalian; }}Lampirkan Kode Kelas Siswa
siswa kelas publik {nama string pribadi; usia int pribadi; grade string pribadi; siswa publik () {} siswa publik (nama string, usia int, grade string) {super (); this.name = name; this.age = usia; this.grade = grade; } public string getName () {return name; } public void setName (name string) {this.name = name; } public int getage () {usia kembali; } public void setage (int usia) {this.age = usia; } public String getGrade () {return grade; } public void setGrade (string grade) {this.grade = grade; } @Override Public String ToString () {return "Student [name =" + name + ", usia =" + usia + ", grade =" + grade + "]"; }} Hasil tes
Tabel Excel yang diekspor
Siswa
Cetak Data Baca Excel
[Siswa [nama = xiao ming, usia = 8, kelas = kelas dua], siswa [nama = xiao guang, usia = 9, kelas = 3], siswa [nama = xiao hua, usia = 10, grade = 4]]]
Meringkaskan
Di atas adalah seluruh konten artikel ini. Saya berharap konten artikel ini akan membantu studi atau pekerjaan Anda. Jika Anda memiliki pertanyaan, Anda dapat meninggalkan pesan untuk berkomunikasi.