導入jar 包
<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>
//准備數據類

package com.wf.zhang.test; public class Person { private String name; private Integer age; private String Adress; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAdress() { return Adress; } public void setAdress(String adress) { Adress = adress; } public Person() { } public Person(String name, Integer age, String adress) { super(); this.name = name; this.age = age; Adress = adress; } @Override public String toString() { return String.format("Person [name=%s, age=%s, Adress=%s]", name, age, Adress); } }
方式一 方法好多過時的
效果

package com.wf.zhang.test; 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; import java.util.Map.Entry; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class POIUtil { //按照路徑 列名讀取文件 public static List<Map<String, String>> readExcel(String filePath, String columns[]) { Sheet sheet = null; Row row = null; Row rowHeader = null; List<Map<String, String>> list = null; String cellData = null; Workbook wb = null; if (filePath == null) { return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if (".xls".equals(extString)) { wb = new HSSFWorkbook(is); } else if (".xlsx".equals(extString)) { wb = new XSSFWorkbook(is); } else { wb = null; } if (wb != null) { // 用來存放表中數據 list = new ArrayList<Map<String, String>>(); // 獲取第一個sheet sheet = wb.getSheetAt(0); // 獲取最大行數 int rownum = sheet.getPhysicalNumberOfRows(); // 獲取第一行 rowHeader = sheet.getRow(0); row = sheet.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; j < colnum; j++) { if (columns[j].equals(getCellFormatValue(rowHeader.getCell(j)))) { cellData = (String) getCellFormatValue(row.getCell(j)); map.put(columns[j], cellData); } } } else { break; } list.add(map); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; } /** * 獲取單個單元格數據 * @param cell * @return */ public static Object getCellFormatValue(Cell cell) { Object cellValue = null; if (cell != null) { // 判斷cell類型 switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: { cellValue = String.valueOf(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_FORMULA: { // 判斷cell是否為日期格式 if (DateUtil.isCellDateFormatted(cell)) { // 轉換為日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); } else { // 數字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING: { cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } } else { cellValue = ""; } return cellValue; } //測試類 public static void main(String[] args) { String filePath = "C:/Users/admin/Desktop/students.xls"; String columns[] = { "學號", "姓名", "年齡", "生日" }; List<Map<String, String>> list = POIUtil.readExcel(filePath, columns); // 遍歷解析出來的list for (Map<String, String> map : list) { for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getKey() + ":" + entry.getValue() + ","); } System.out.println(); } } }
方式二 推薦使用
效果
package com.wf.zhang.test; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; public class ReadExcel { //調用方法 public static void main(String[] args) { importXLS(); } //讀取的方法 public static List<Student> importXLS(){ ArrayList<Student> list = new ArrayList<Student>(); try { //1、獲取文件輸入流 InputStream inputStream = new FileInputStream("C:/Users/admin/Desktop/students.xls"); //2、獲取Excel工作簿對象 HSSFWorkbook workbook = new HSSFWorkbook(inputStream); //3、得到Excel工作表對象 HSSFSheet sheetAt = workbook.getSheetAt(0); //4、循環讀取表格數據 for (Row row : sheetAt) { //首行(即表頭)不讀取 if (row.getRowNum() == 0) { continue; } //讀取當前行中單元格數據,索引從0開始 int id = (int) (row.getCell(0).getNumericCellValue()); String name = row.getCell(1).getStringCellValue(); int age = (int) row.getCell(2).getNumericCellValue(); String birth = row.getCell(3).getStringCellValue(); Date date = DateUtil.parseYYYYMMDDDate(birth); Student st = new Student(); st.setId(id); st.setName(name); st.setAge(age); st.setBirth(date); list.add(st); } //打印 System.out.println(list.toString()); //5、關閉流 workbook.close(); } catch (IOException e) { e.printStackTrace(); } return list; } }