First, the necessary package dependencies
1, using maven will automatically import its dependencies, so only need to import the 2007 version of the package, other packages are automatically imported, including 2003 required jar package.
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
2, if the need for direct pilot packet, the following packets download all, introduced manually.
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.3</version> </dependency>
Two, excel read
1, data templates
2, demo code
@SuppressWarnings ( "Resource" ) public static void main (String [] args) { // address where Excel String path = "E: Demo \\ \\ \\ one.xlsx Excel" ; File File = new new File (path) ; InputStream in = null ; the try { in = new new FileInputStream (file); // file extension String = path.substring of the type (path.indexOf () + 1 "." ); Workbook wb; // according to choose different suffix 2003/2007 edition version of realization, respectively XLS / XLSX IF("xlsx".equals(type) || "XlSX".equals(type)) { wb = new XSSFWorkbook(in); }else if("xls".equals(type) || "XLS".equals(type)) { wb = new HSSFWorkbook(in); }else { System.out.println("文件后缀格式有误"); return; } //获取sheet Sheet sheet = wb.getSheetAt(0); //总行数 int rsRows = sheet.getPhysicalNumberOfRows(); // total number of columns int rsColumn = sheet.getRow (0 ) .getPhysicalNumberOfCells (); // store the two-dimensional array excel String [] [] = Data new new String [rsRows] [rsColumn]; // Row Row; the Cell Cell ; for ( int I = 0; I <rsRows; I ++ ) { for ( int J = 0; J <rsColumn; J ++ ) { Cell = sheet.getRow (I) .getCell (J); // the type of non-string cells arranged String cell.setCellType (CellType.STRING); Data [I] [J]= cell.getStringCellValue(); } } //打印结果 for(int i=0;i<data.length;i++) { for(int j=0;j<data[i].length;j++) { System.out.print(data[i][j]+"\t"); } System.out.println(); } } catch (FileNotFoundException e) { System.out.println("读取文件失败"); e.printStackTrace(); } catch (IOException e) { System.out.println("创建workbook失败"); e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
result:
name address the above mentioned id 1 Jiangsu and Shanghai 2 li Earth 3 ki Mars