Output laporan adalah konten yang sering terlibat dalam pengembangan aplikasi Java, dan laporan umum seringkali kurang universalitas dan tidak nyaman bagi pengguna untuk mengedit yang dipersonalisasi. Karena fitur lintas platformnya, program Java tidak dapat secara langsung memanipulasi Excel. Oleh karena itu, artikel ini membahas program Java garis pandang POI untuk membaca dan mengimpor Excel.
Struktur Proyek:
java_poi_excel
File excel digunakan:
xls
Kelas XLSMAIN .java
// Kelas ini memiliki metode utama, yang terutama bertanggung jawab untuk menjalankan program. Pada saat yang sama, kelas ini juga termasuk membaca Excel dengan POI (versi 2003) impor java.io.fileInputStream; impor java.io.ioException; import java.io.inputstream; impor java.util.arraylist; impor java.util.list; impor java.utillist; org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * * @author hongten </br> * * */kelas publik xlsmain {public static void main (string [] args) melempar ioException {xlsmain xlsmain = xlsmain baru (); Xlsdto xls = null; Daftar <XlsDTO> Daftar = xlsmain.readxls (); coba {xlsdto2excel.xlsdto2excel (daftar); } catch (Exception e) {E.PrintStackTrace (); } untuk (int i = 0; i <list.size (); i ++) {xls = (xlsdto) list.get (i); System.out.println (xls.getxh () + "" + xls.getxm () + "" + xls.getyxsmc () + "" + xls.getkcm () + "" + xls.getkcm () + "" + xls.getcj ()); } } /** * Read the content of the xls file* * @return List<XlsDto> object* @throws IOException * Input/output (i/o) exception*/ private List<XlsDto> readXls() throws IOException { InputStream is = new FileInputStream("pldrxkxxmb.xls"); HSSFWORKBOOK HSSFWORKBOOK = HSSFWORKBOOK baru (IS); Xlsdto xlsdto = null; Daftar <XlsDTO> Daftar = ArrayList baru <xlsdto> (); // Sheet Loop Sheet untuk (int numsheet = 0; numsheet <hssfworkbook.getNumberofsheets (); numsheet ++) {hssfsheet hssfsheet = hssfworkbook.getsheetat (numsheet); if (hssfsheet == null) {lanjutan; } // baris baris loop untuk (int rownum = 1; rownum <= hssfsheet.getlastrownum (); rownum ++) {hssfrow hssfrow = hssfsheet.getrow (rownum); if (hssfrow == null) {lanjutan; } xlsdto = xlsdto baru (); // Loop Colom Cell // 0 Nomor Siswa 1 Nama 2 College 3 Nama Kursus 4 Skor // Untuk (int CellNum = 0; CellNum <= 4; CellNum ++) {HSSFCell XH = HSSFROW.GetCell (0); if (xh == null) {lanjutkan; } xlsdto.setxh (getValue (xh)); Hssfcell xm = hssfrow.getcell (1); if (xm == null) {lanjutkan; } xlsdto.setxm (getValue (xm)); Hssfcell yxsmc = hssfrow.getCell (2); if (yxsmc == null) {lanjutan; } xlsdto.setyxsmc (getValue (yxsmc)); Hssfcell kcm = hssfrow.getcell (3); if (kcm == null) {lanjutan; } xlsdto.setkcm (getValue (KCM)); Hssfcell cj = hssfrow.getcell (4); if (cj == null) {lanjutan; } xlsdto.setcj (float.parsefloat (getValue (cj))); list.add (xlsdto); }} daftar pengembalian; } /** * Get the value in the Excel table* * @param hssfCell * Every grid in Excel* @return Values in each grid in Excel */ @SuppressWarnings("static-access") private String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfcell.cell_type_boolean) {// mengembalikan nilai boolean tipe return string.valueof (hssfcell.getBooleanCellValue ()); } lain if (hssfcell.getCellType () == hssfcell.cell_type_numeric) {// Kembalikan nilai numerik tipe return string.valueof (hssfcell.getNumericCellValue ()); } else {// kembalikan nilai string type return string.valueof (hssfcell.getStringCellValue ()); }}}Kelas xlsdto2excel.java
// Kelas ini terutama bertanggung jawab untuk memasukkan data ke Excel (versi 2003) impor java.io.fileoutputStream; impor java.io.outputStream; import java.util.list; Impor org.apache.poi.hssf.usermodel.hssfcell; impor org.apache.poi.hssf.usermodel.hssfrichtextString; impor org.apache.poi.hssf.usermodel.hssfsheet; impor.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache.apache. public class XlsDto2Excel { /** * * @param xls * An object of the XlsDto entity class* @throws Exception * Throws an exception during the import of Excel*/ public static void xlsDto2Excel(List<XlsDto> xls) throws Exception { // Get the total number of columns int CountColumnNum = xls.size(); // Buat Dokumen Excel HSSFWorkbook HWB = HSSFWorkbook baru (); Xlsdto xlsdto = null; // lembar sesuai dengan halaman kerja hssfsheet sheet = hwb.createSheet ("pldrxkxxmb"); Hssfrow firstrow = sheet.createrow (0); // Mulai baris dengan subscript 0 hssfcell [] firstcell = hssfcell baru [countcolumnnum]; String [] nama = string baru [countcolumnnum]; nama [0] = "Nomor Siswa"; nama [1] = "name"; Nama [2] = "College"; Nama [3] = "Nama Kursus"; nama [4] = "skor"; untuk (int j = 0; j <countcolumnnum; j ++) {firstcell [j] = firstrow.createCell (j); firstcell [j] .setCellValue (hssfrichTextString baru (nama [j])); } untuk (int i = 0; i <xls.size (); i ++) {// Buat baris hssfrow baris = sheet.createrow (i+1); // Dapatkan setiap catatan untuk dimasukkan xlsdto = xls.get (i); untuk (int colu = 0; colu <= 4; colu ++) {// loop dalam baris hssfcell xh = row.createCell (0); xh.setCellValue (xlsdto.getxh ()); Hssfcell xm = row.createCell (1); xm.setCellValue (xlsdto.getxm ()); Hssfcell yxsmc = row.createCell (2); yxsmc.setCellValue (xlsdto.getyxsmc ()); Hssfcell kcm = row.createCell (3); kcm.setCellValue (xlsdto.getkcm ()); Hssfcell cj = row.createCell (4); cj.setCellValue (xlsdto.getcj ()); (xlsdto.getMessage ()); }} // Buat aliran output file dan siapkan output spreadsheet outputStream out = new FileOutputStream ("poi2excel/pldrxkxxmb.xls"); hwb.write (out); out.close (); System.out.println ("Ekspor Database Sukses"); }}Kelas XLSDTO .java
// kelas ini adalah kelas entitas kelas publik xlsdto {/*** Nomor pemilihan kursus*/Private Integer XKH; / *** Nomor Siswa*/ Private String xh; / *** Nama*/ Private String xm; / *** kuliah*/ string pribadi yxsmc; / *** Nomor Kursus*/ Private Integer KCH; / *** Nama Kursus*/ Private String KCM; / *** skor*/ private float cj; Integer publik getXkh () {return xkh; } public void setxkh (integer xkh) {this.xkh = xkh; } public String getXh () {return xh; } public void setxh (string xh) {this.xh = xh; } public String getXm () {return xm; } public void setxm (string xm) {this.xm = xm; } public String getYxSMC () {return yxsmc; } public void setyxsmc (string yxsmc) {this.yxsmc = yxsmc; } public integer getKch () {return kch; } public void setKch (integer kch) {this.kch = kch; } public String getKCM () {return kcm; } public void setKCM (string kcm) {this.kcm = kcm; } public float getCj () {return cj; } public void setcj (float cj) {this.cj = cj; }}Output Latar Belakang:
Ekspor Basis Data berhasil
1.0 Hongten School of Information Technology Dasar Aplikasi Jaringan Komputer 80.0
2.0 Wangwu Information Technology College Computer Network Application Basics 81.0
3.0 Li Shengji Sekolah Teknologi Informasi Dasar Aplikasi Jaringan Komputer 82.0
4.0 Dasar -dasar Aplikasi Jaringan Komputer dari Sekolah Tinggi Teknologi Informasi Kelas 5 83.0
5.0 Cai Shiyun Sekolah Teknologi Informasi Dasar Aplikasi Jaringan Komputer 84.0
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.