La sortie du rapport est un contenu qui est souvent impliqué dans le développement des applications Java, et les rapports généraux manquent souvent d'universalité et ne sont pas pratiques pour les utilisateurs de modifier personnalisés. En raison de ses fonctionnalités multiplateformes, les programmes Java ne peuvent pas manipuler directement Excel. Par conséquent, cet article traite du programme Java de la ligne de vision POI pour la lecture et l'importation Excel.
Structure du projet:
java_poi_excel
Fichiers Excel utilisés:
xls
Classe xlsmain .java
// Cette classe a une méthode principale, qui est principalement responsable de l'exécution du programme. En même temps, cette classe comprend également la lecture Excel avec POI (version 2003) Importer java.io.fileInputStream; Importer java.io.ioexception; import java.util.list; org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; / ** * * @author hongten </br> * * * / public class xlsmain {public static void main (String [] args) lève ioException {xlsmain xlsmain = new xlsmain (); Xlsdto xls = null; List <xlsdto> list = xlsmain.readxls (); essayez {xlsdto2excel.xlsdto2Excel (list); } catch (exception e) {e.printStackTrace (); } pour (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 ()); }} / ** * Lisez le contenu du fichier xls * * @return List <xlsdto> objet * @throws ioException * input / output (i / o) exception * / private list <xlsdto> readxls () lance ioException {inputStream is = new FileInputStream ("pldrxkxxmb.xls"); HSSFWorkBook HSSFWorkBook = new HSSFWorkBook (IS); Xlsdto xlsdto = null; List <xlsdto> list = new ArrayList <xlsdto> (); // feuille de feuille de boucle pour (int numSheet = 0; numSheet <hssfworkbook.getNumberOfSheets (); numSheet ++) {hssfsheet hssfsheet = hssfworkbook.getSheetAt (numSheet); if (hssfSheet == null) {continuant; } // Loop Row Row for (int rownum = 1; rownum <= hssfsheet.getLastRowm (); rownum ++) {hssfrow hssfrow = hssfsheet.getrow (rownum); if (hssfrow == null) {continue; } xlsdto = new xlsdto (); // LOOP COLUMN CELL // 0 Étudiant Numéro 1 Nom 2 College 3 Nom du cours 4 Score // Pour (int CellNum = 0; Cellnum <= 4; Cellnum ++) {HssfCell xh = hssfrow.getCell (0); if (xh == null) {continue; } xlsdto.setxh (getValue (xh)); HssfCell xm = hssfrow.getCell (1); if (xm == null) {continue; } xlsdto.setxm (getValue (xm)); HssfCell yxsmc = hssfrow.getCell (2); if (yxsmc == null) {continue; } xlsdto.setyxsmc (getValue (yxsmc)); HssfCell kcm = hssfrow.getCell (3); if (kcm == null) {continue; } xlsdto.setkcm (getValue (kcm)); HSSFCELL CJ = HSSFROW.GETCELL (4); if (cj == null) {continue; } xlsdto.setcj (float.parsefloat (getValue (cj))); list.add (xlsdto); }} Retour List; } / ** * Obtenez la valeur dans la table Excel * * @param hssfcell * Chaque grille dans Excel * @return Values dans chaque grille dans Excel * / @SuppressWarnings ("static-access") private string getValue (hssfcell hssfcell) {if (hssfcell.getcelltype () == hssfcell.cell_type_boolean) {// renvoie la valeur du type booléen return string.valueof (hssfcell.getBooleanCellValue ()); } else if (hssfcell.getCellType () == hssfcell.cell_type_numeric) {// renvoie la valeur du type numérique return string.valueof (hssfcell.getNumericCellValue ()); } else {// renvoie la valeur du type de chaîne return string.valueof (hssfcell.getStringCellValue ()); }}}Classe xlsdto2excel.java
// Cette classe est principalement responsable de l'insertion de données dans Excel (version 2003) Importer java.io.fileoutputStream; Importer java.io.outputStream; import java.util.list; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfrichTextString; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.Usermodel.hsfwewer; classe publique xlsdto2Excel {/ ** * * @param xls * Un objet de la classe d'entité xlsdto * @throws exception * lance une exception pendant l'importation d'Excel * / public static void xlsdto2excel (list <xlsdto> xls) lance l'exception {// obtenir le nombre total de colonnes int compte // Créer un document Excel hssfworkbook hwb = new hssfworkbook (); Xlsdto xlsdto = null; // La feuille correspond à une page de travail hssfsheet sheet = hwb.createSheet ("pldrxkxxmb"); Hssfrow firstrow = sheet.createrow (0); // Démarrez la ligne avec l'indice 0 hssfCell [] FirstCell = new HSSFCELL [countColumnNum]; String [] names = new String [countColumnNum]; noms [0] = "numéro d'étudiant"; noms [1] = "name"; noms [2] = "College"; noms [3] = "nom de cours"; noms [4] = "score"; pour (int j = 0; j <countColumnNum; j ++) {firstCell [j] = firstrow.createCell (j); FirstCell [J] .SetCellValue (nouveau HSSFrichTextString (noms [J])); } pour (int i = 0; i <xls.size (); i ++) {// Créer une ligne de hssfrow row = sheet.createrow (i + 1); // demande chaque enregistrement inséré xlsdto = xls.get (i); for (int colu = 0; colu <= 4; colu ++) {// boucle dans une ligne 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 ()); }} // Créez un flux de sortie de fichier et préparez la feuille de calcul de sortie OutputStream out = new FileOutputStream ("POI2Excel / pldrxkxxmb.xls"); hwb.write (out); out.close (); System.out.println ("Database Export réussi"); }}Classe xlsdto .java
// Cette classe est une classe d'entité classe publique xlsdto {/ ** * Numéro de sélection de cours * / entier privé XKH; / ** * numéro d'étudiant * / chaîne privée xh; / ** * nom * / chaîne privée xm; / ** * collège * / chaîne privée yxsmc; / ** * Numéro de cours * / entier privé Kch; / ** * Nom du cours * / chaîne privée KCM; / ** * score * / float privé CJ; entier public getxkh () {return xkh; } public void setxkh (entier 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 entier getkch () {return kch; } public void setkch (entier 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; }}Sortie d'arrière-plan:
Exportation de la base de données avec succès
1.0 Hongten School of Information Technology Network Computer Network Basics 80.0
2.0 Wangwu Information Technology College College Network Application Basics 81.0
3.0 LI Shengji École de technologie de l'information Application de réseau informatique Basiques 82.0
4.0 Bases de l'application du réseau informatique de la classe 5 College of Information Technology 83.0
5.0 CAI Shiyun School of Information Technology Network Application Application Basics 84.0
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.