1. 가져와야하는 항아리
1. Commons-Collections 4-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
1. Excel 문서에 해당하는 org.apache.poi.ss.usermodel.workbook;
2. XLS 형식의 Excel 문서에 해당하는 org.apache.poi.hssf.usermodel.hssfworkbook import;
3. xlsx 형식의 Excel 문서에 해당하는 3. emport org.apache.poi.xssf.usermodel.xssfworkbook;
4. Excel 문서의 시트에 해당하는 org.apache.poi.ss.usermodel.sheet;
5. org.apache.poi.ss.usermodel.row, 시트의 선에 해당하는;
6. 셀에 해당하는 org.apache.ss.usermodel.cell.
3. 코드는 다음과 같습니다
패키지 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.util.map; java.util.map.entry; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.s org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.s.usermodel.workbook; import org.apache.poi.xssf.usermodel.xssfworkbook; public class void main (strest testpoi) {public class works () {public class works () {public class wwork.poi.xssf.usermodel.xssfworkbook; = null; 시트 시트 = null; 행으로 = null; List <map <String, String >> list = null; 문자열 cellData = null; 문자열 filepath = "d : //test.xlsx"; 문자열 열 [] = { "name", "age", "score"}; wb = readexcel (filepath); if (wb! = null) {// 테이블 목록에 데이터를 저장하는 데 사용합니다. <new arraylist <map <String, String >> (); // 첫 번째 시트를 가져옵니다. 시트 = wb.getSheetat (0); // 최대 행 수를 가져옵니다 int rownum = sheet.getPhysicalNumberOfrows (); // 첫 번째 행을 가져옵니다. get.getrow (0); // 열의 최대 수를 가져옵니다 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; map.put (열 [j], celldata); }} else {break; } list.add (지도); }} // (맵 <문자열, 문자열>지도 : list) {for (enther <string, string> entry : map.entryset ()) {system.out.print (enther.getKey ()+":"+eTry.GetValue ()+","); } system.out.println (); }} // Excel 읽기 공개 정적 통합 문서 readexcel (String Filepath) {Workbook WB = NULL; if (filepath == null) {return null; } 문자열 ExtString = FilePath.SubString (filepath.lastIndexof ( ".")); 입력 스트림은 = null입니다. try {is = new FileInputStream (FilePath); if ( ". xls".equals (extring)) {return wb = new 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; } public static 객체 getCellFormatValue (셀 셀) {Object CellValue = null; if (cell! = null) {// 셀 유형 스위치 (cell.getCellType ()) {case cell.cell_type_numeric : {cellValue = string.valueof (cell.getNumericCellValue ()); 부서지다; } case cell.cell_type_formula : {// 셀이 날짜 형식인지 판단 if (dateUtil.iscellDateFormatted (Cell)) {// 날짜 형식으로 변환 yyyy-mm-dd cellValue = cell.getDateCellValue (); } else {// 숫자 cellValue = string.valueof (cell.getnumericcellValue ()); } 부서지다; } case cell.cell_type_string : {cellValue = cell.getRichstringCellValue (). getString (); 부서지다; } 기본값 : cellValue = ""; }} else {cellValue = ""; } return cellValue; }}4. 운영 결과
코드를 실행하기 전에 디스크 d에 test.xlsx 문서가 있는지 확인하십시오. 그렇지 않으면 보고서 파일을 찾을 수 없습니다. Excel 문서의 테이블 헤더는 코드의 String columns[] = {"name","age","score"} 에 해당해야합니다.
요약
위의 것은 Java Parsing Excel (XL 및 XLSX의 두 형식)이 소개되는 방법입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!