poi读取excel
来源:互联网 发布:淘宝网怎么游专营店 编辑:程序博客网 时间:2024/06/06 01:19
pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>
POIUtil.java
package org.aidan.util;import org.apache.commons.beanutils.BeanUtils;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;import java.io.IOException;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class POIUtil { /** * 读2017版的excel文件,后缀名为xlsx的excel文件 */ public static <T> List<T> readExcel(String exclePath, Class<T> c) { List<T> ts = null; if (exclePath.endsWith("xlsx")) { ts = readExcel2007(exclePath, c); } else if (exclePath.endsWith("xls")) { ts = readExcel2003(exclePath, c); } return ts; } private static <T> List<T> readExcel2007(String exclePath, Class<T> c) { // XSSFWorkbook List<T> ts = new ArrayList<>(); FileInputStream fis = null; try { fis = new FileInputStream(exclePath); XSSFWorkbook sheets = new XSSFWorkbook(fis); XSSFSheet sheet = sheets.getSheetAt(0); Iterator<Row> it = sheet.rowIterator(); Field[] fields = c.getDeclaredFields(); Constructor<T> constructor = c.getDeclaredConstructor(); forEach(it, fields, constructor, ts); } catch (IOException e) { } catch (NoSuchMethodException e) { } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return ts; } /** * 读2013版的excel文件,后缀名为xls的excel文件 */ private static <T> List<T> readExcel2003(String exclePath, Class<T> c) { // XSSFWorkbook List<T> ts = new ArrayList<>(); FileInputStream fis = null; try { fis = new FileInputStream(exclePath); HSSFWorkbook sheets = new HSSFWorkbook(fis); HSSFSheet sheet = sheets.getSheetAt(0); Iterator<Row> it = sheet.rowIterator(); Field[] fields = c.getDeclaredFields(); // Constructor<T> constructor = c.getDeclaredConstructor(); forEach(it, fields, constructor, ts); } catch (IOException e) { } catch (NoSuchMethodException e) { } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return ts; } private static <T> void forEach(Iterator<Row> it, Field[] fields, Constructor<T> constructor, List<T> ts) { while (it.hasNext()) { Row row = it.next(); T t = null; try { t = constructor.newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } Iterator<Cell> cIt = row.cellIterator(); for (int i = 0; (i < fields.length) && cIt.hasNext(); i++) { Cell cell = cIt.next(); if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { try { BeanUtils.setProperty(t, fields[i].getName(), cell.getStringCellValue()); } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { try { BeanUtils.setProperty(t, fields[i].getName(), cell.getNumericCellValue()); } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } } else { } } ts.add(t); } }}
测试:
List<User> users = POIUtil.readExcel(exclePath, User.class);
阅读全文
0 0
- POI-----POI读取Excel实例
- POI读取EXCEL
- POI读取EXCEL教程
- POI读取EXCEL
- poi 读取excel
- 通过POI读取Excel
- POI读取Excel
- POI 读取 Excel
- POI读取Excel浅谈
- POI 读取EXcel
- POI读取EXCEL教程
- POI读取Excel
- POI读取Excel
- POI读取Excel浅谈
- POI读取EXCEL教程
- Java POI 读取Excel
- java poi读取excel
- POI读取Excel浅谈
- java-Base64加码解码类
- Android NDK学习笔记2-环境搭建篇
- JavaScript中的JSON
- Cannot assign to 'self' outside of a method in the init family
- spark--python数据分析脚本
- poi读取excel
- slow_http_dos tomcat
- 修改MySQL提示符
- 重装系统之后电脑无法识别磁盘怎么办
- JavaAgent无法替换运行中的字节码
- 最新期货交易时间一览表(日盘+夜盘)2017年3月更新
- pvanet训练自己的数据过程记录
- poj1284 Primitive Roots:欧拉函数+原根
- MySQL语句的规范