1. Das Glas, das importiert werden muss
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. Hauptapi
1.Import org.apache.poi.s.usermodel.workbook, entsprechend dem Excel -Dokument;
2. import org.apache.poi.hssf.usermodel.hssfworkbook, entsprechend dem Excel -Dokument im XLS -Format;
3.import org.apache.poi.xssf.usermodel.xssfworkbook, entsprechend dem Excel -Dokument im XLSX -Format;
4.Import org.apache.poi.Ss.USMODEL.Sheet, entsprechend einem Blatt im Excel -Dokument;
5.import org.apache.poi.ss.usermodel.row, entsprechend einer Linie in einem Blatt;
6.Import org.apache.poi.ss.usermodel.cell, entsprechend einer Zelle.
3. Der Code ist wie folgt
Paket poi; import java.io.fileinputstream; import Java.io.filenotFoundException; import Java.io.ioxception; Import Java.io.inputStream; Import Java.util.ArrayList; importieren java.util.linkedhashmap; java.util.map.entry; import org.apache.poi.hssf.usmodel.hssfworkbook; import org.apache.poi.s.usmodel.cell; import org.apache.poi.s.usermodel.dateutil; org.apache.poi.ss.usmodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.ss.usmodel.Workbook; = null; Blatt Blatt = null; Row row = null; Liste <map <String, String >> list = null; String cellData = null; String filepath = "d: //test.xlsx"; String -Spalten [] = {"Name", "Alter", "Score"}; WB = ReadExcel (Filepath); if (wb! = null) {// Verwenden Sie, um Daten in der Tabellenliste zu speichern // das erste Blatt abrufen = wb.getsheetat (0); // Die maximale Anzahl von Zeilen intowum = Sheet.GetPhysicalNumberofrows () erhalten; // die erste zeile zeile = sheet.getrow (0); // Erhalten Sie die maximale Anzahl von Spalten int colnum = row.getPhysicalNumberofcells (); für (int i = 1; i <rownum; i ++) {map <String, String> map = new LinkedHashMap <String, String> (); Row = Sheet.getRow (i); if (row! map.put (Spalten [j], cellData); }} else {break; } list.add (map); }} // Ruhe durch die analysierte Liste für (map <string, string> map: list) {für (Eintrag <String, String> Eintrag: map.EntrySet ()) {System.out.print (Eintrag. } System.out.println (); }} // Excel public static Workbook ReadExcel (String filepath) {Arbeitsbuch Wb = null; if (filepath == null) {return null; } String extstring = filepath.substring (filepath.lastindexof (".")); InputStream ist = null; try {is = new FileInputStream (filepath); if (". xls" .Equals (extstring)) {return wb = new HSSFWorkbook (ist); } else if (". xlsx" .Equals (extstring)) {return wb = new XSSFWorkbook (ist); } else {return wb = null; }} catch (FilenotFoundException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } return wb; } öffentliches statisches Objekt getCellFormatValue (Zellzelle) {Object CellValue = null; if (cell! brechen; } case cell.cell_type_formula: {// beurteilen, ob die Zelle im Datumsformat ist if (DateUtil.iscellDateFormatted (cell)) {// konvertieren bis data yyyy-mm-dd cellValue = cell.getDatecellValue (); } else {// numerical cellValue = string.ValueOf (cell.getNumericcellValue ()); } brechen; } case cell.cell_type_string: {cellValue = cell.getrichstringCellValue (). getString (); brechen; } Standard: cellValue = ""; }} else {cellValue = ""; } return cellValue; }}4. Betriebsergebnisse
Stellen Sie vor dem Ausführen des Codes sicher, dass ein test.xlsx -Dokument auf der Festplatte D vorhanden ist, da sonst die Berichtsdatei nicht gefunden wird. Der Header der Tabelle im Excel -Dokument sollte String columns[] = {"name","age","score"} im Code entsprechen.
Zusammenfassen
Das oben oben ist die Methode des Java -Parsens Excel (zwei Formate von XLS und XLSX), die Ihnen vorgestellt wurden. Ich hoffe, es wird Ihnen hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird Ihnen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!