用POI 3.17读取EXCEL数据
Posted wf-zhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用POI 3.17读取EXCEL数据相关的知识,希望对你有一定的参考价值。
导入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; } }
以上是关于用POI 3.17读取EXCEL数据的主要内容,如果未能解决你的问题,请参考以下文章