1. Le pot qui doit être importé
1.COMMONS-COLLECTIONS4-4.1.jar
2.POI-3.17-Beta1.jar
3.poi-ooxml-3.17-beta1.jar
4.poi-ooxml-schemas-3.17-beta1.jar
5.xmlbeans-2.6.0.jar
2. API principale
1.IMPORT ORG.APACHE.POI.SS.USERMODEL.Workbook, correspondant au document Excel;
2.Import org.apache.poi.hssf.usermodel.hssfworkbook, correspondant au document Excel au format xls;
3.Import org.apache.poi.xssf.usermodel.xssfworkbook, correspondant au document Excel au format xlsx;
4.IMPORT ORG.APACHE.POI.SS.USERMODEL.Sheet, correspondant à une feuille dans le document Excel;
5.IMPORT ORG.APACHE.POI.SS.USERMODEL.ROW, correspondant à une ligne dans une feuille;
6.IMPORT ORG.APACHE.POI.SS.USERMODEL.CELL, correspondant à une cellule.
3. Le code est le suivant
package poi; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.linkedhashmap; import java.util.list; import java.utitil.map; import; java.util.map.entry; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.s.usermodel.dateutil; import org.apache.poi.sss.UserModel.Row; org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Testpoi { public static void main(String[] args) { Workbook wb = null; Feuille de feuille = null; Row Row = NULL; List <map <string, string >> list = null; String CellData = null; String filepath = "d: //test.xlsx"; Colonnes de chaîne [] = {"name", "age", "score"}; wb = readExcel (filepath); if (wb! = null) {// Utiliser pour stocker des données dans la table List = new ArrayList <map <string, string >> (); // Obtenez la première feuille de feuille = wb.getSheetAt (0); // Obtenez le nombre maximum de lignes int rownum = sheet.getPhysicalNumberofrows (); // Obtenez la première ligne de ligne = sheet.getRow (0); // Obtenez le nombre maximum de colonnes int colnum = row.getPhysicalNumberofCells (); for (int i = 1; i <rownum; i ++) {map <string, string> map = new LinkedHashMap <String, String> (); row = sheet.getRow (i); if (row! = null) {for (int j = 0; j <colnum; j ++) {CellData = (string) getCellFormatValue (row.getCell (j)); map.put (colonnes [J], CellData); }} else {Break; } list.add (map); }} // Tranquility via la liste analysée pour (map <string, string> map: list) {for (entry <string, string> entry: map.entryset ()) {System.out.print (entry.getKey () + ":" + entry.getValue () + ","); } System.out.println (); }} // Read Excel Public Static Workbook ReadExcel (String FilePath) {Workbook WB = NULL; if (filePath == null) {return null; } String extString = filepath.substring (filePath.LastIndexof (".")); InputStream est = null; try {is = new FileInputStream (filePath); if (". xls" .equals (extString)) {return wb = nouveau hssfworkbook (is); } else if (". xlsx" .equals (extString)) {return wb = new xssfworkbook (is); } else {return wb = null; }} catch (filenotFoundException e) {e.printStackTrace (); } catch (ioException e) {e.printStackTrace (); } return wb; } Objet statique public getCellFormatValue (Cell Cell) {objet CellValue = null; if (cell! = null) {// juge de type de cellule Switch (Cell.getCellType ()) {Case Cell.Cell_Type_Numeric: {CellValue = String.Valueof (Cell.GetNumericCellValue ()); casser; } Case Cell.Cell_Type_Formula: {// juger si la cellule est dans le format de date if (dateUtil.iscellDateFormatted (Cell)) {// converti au format date yyyy-mm-dd cellvalue = cell.getDateCellValue (); } else {// Numerical CellValue = String.ValueOf (Cell.GetNumericCellValue ()); } casser; } case Cell.Cell_Type_String: {CellValue = Cell.getRichStringCellValue (). GetString (); casser; } Default: CellValue = ""; }} else {CellValue = ""; } return CellValue; }}4. Résultats de l'opération
Avant d'exécuter le code, assurez-vous qu'il existe un document test.xlsx sur le disque D, sinon le fichier de rapport ne sera pas trouvé; L'en-tête de la table dans le document Excel doit correspondre String columns[] = {"name","age","score"} dans le code.
Résumer
Ce qui précède est la méthode d'Excel Java Parsing (deux formats de XLS et XLSX) présentés. J'espère que cela vous sera utile. Si vous avez des questions, veuillez me laisser un message et l'éditeur vous répondra à temps. Merci beaucoup pour votre soutien au site Web Wulin.com!