public class ExcelReader{
public static void main(String[] args) throws Exception {
List<WebDto> list = getList(new WebDto(), "D:\\web-test.xls");
for (WebDto webDto : list) {
System.err.println(webDto);
}
}
public static <T extends Serializable> List<T> getList(T a,String fileUrl) throws Exception {
List<T> arrayList = new ArrayList<>();
Map<Integer, List<HSSFRow>> rows = getRows(fileUrl);
Collection<List<HSSFRow>> values = rows.values();
for (List<HSSFRow> list : values) {
for (HSSFRow hssfRow : list) {
T t = clone(a);
int numberOfCells = hssfRow.getPhysicalNumberOfCells();
for (int i = 0; i < numberOfCells; i++) {
Serializable value = getValue(hssfRow, i);
List<Field> field = getifield(t);
if (value==null) {
continue;
}
Field field2 = field.get(i);
field2.setAccessible(true);
field2.set(t, value);
}
arrayList.add((T) t);
}
}
return arrayList;
}
/**
* 获取对象的所有字段
* @param a
* @return
*/
protected static List<Field> getifield(Object a){
Field[] fields = a.getClass().getDeclaredFields();
List<Field> list = new ArrayList<>();
if (list!=null) {
for (Field field :fields) {
String name = field.getName();
if (!"serialVersionUID".equals(name)) {
list.add(field);
}
}
}
return list;
}
//根据行和列几列读取excel中的值
protected static Serializable getValue(HSSFRow row,int i){
HSSFCell cell = row.getCell(i);
if (cell==null) {
return null;
}
Serializable value;
int type = cell.getCellType();
switch (type) {
case Cell.CELL_TYPE_NUMERIC:
value = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
default:
value = cell.getStringCellValue();
break;
}
return value;
}
/**
* 根据excel的文件路径,返回所有工作簿上的行信息
* @param fileName
* @return
* @throws Exception
*/
protected static Map<Integer,List<HSSFRow>> getRows(String fileName) throws Exception{
Map<Integer,List<HSSFRow>> rowes=new HashMap<>();
File file=new File(fileName);
HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(file));
for(int i=0; i<book.getNumberOfSheets(); i++) {
List<HSSFRow> rowList= new ArrayList<>();
HSSFSheet sheetAt = book.getSheetAt(i);
int rows = sheetAt.getPhysicalNumberOfRows();
for(int j=1 ;j<rows;j++){
HSSFRow row = sheetAt.getRow(j);
if (row==null) {
continue;
}
rowList.add(row);
}
rowes.put(i, rowList);
}
return rowes;
}
/**
* 克隆對象
* @param obj
* @return
*/
public static <T> T clone(T obj){
T result=null;
try {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(obj);
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
result=(T)ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
static class WebDto implements Serializable{
private static final long serialVersionUID = -1470427074967045384L;
//city
private Serializable name;
//行政区域
private Serializable area;
private Serializable gaArea;
public Serializable getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Serializable getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public Serializable getGaArea() {
return gaArea;
}
public void setGaArea(Serializable gaArea) {
this.gaArea = gaArea;
}
@Override
public String toString() {
return "WebDto [name=" + name + ", area=" + area
+ ", gaArea=" + gaArea + "]";
}
public WebDto(Serializable name, Serializable area, Serializable gaArea) {
super();
this.name = name;
this.area = area;
this.gaArea = gaArea;
}
public WebDto() {
super();
}
}
}
public static void main(String[] args) throws Exception {
List<WebDto> list = getList(new WebDto(), "D:\\web-test.xls");
for (WebDto webDto : list) {
System.err.println(webDto);
}
}
public static <T extends Serializable> List<T> getList(T a,String fileUrl) throws Exception {
List<T> arrayList = new ArrayList<>();
Map<Integer, List<HSSFRow>> rows = getRows(fileUrl);
Collection<List<HSSFRow>> values = rows.values();
for (List<HSSFRow> list : values) {
for (HSSFRow hssfRow : list) {
T t = clone(a);
int numberOfCells = hssfRow.getPhysicalNumberOfCells();
for (int i = 0; i < numberOfCells; i++) {
Serializable value = getValue(hssfRow, i);
List<Field> field = getifield(t);
if (value==null) {
continue;
}
Field field2 = field.get(i);
field2.setAccessible(true);
field2.set(t, value);
}
arrayList.add((T) t);
}
}
return arrayList;
}
/**
* 获取对象的所有字段
* @param a
* @return
*/
protected static List<Field> getifield(Object a){
Field[] fields = a.getClass().getDeclaredFields();
List<Field> list = new ArrayList<>();
if (list!=null) {
for (Field field :fields) {
String name = field.getName();
if (!"serialVersionUID".equals(name)) {
list.add(field);
}
}
}
return list;
}
//根据行和列几列读取excel中的值
protected static Serializable getValue(HSSFRow row,int i){
HSSFCell cell = row.getCell(i);
if (cell==null) {
return null;
}
Serializable value;
int type = cell.getCellType();
switch (type) {
case Cell.CELL_TYPE_NUMERIC:
value = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
default:
value = cell.getStringCellValue();
break;
}
return value;
}
/**
* 根据excel的文件路径,返回所有工作簿上的行信息
* @param fileName
* @return
* @throws Exception
*/
protected static Map<Integer,List<HSSFRow>> getRows(String fileName) throws Exception{
Map<Integer,List<HSSFRow>> rowes=new HashMap<>();
File file=new File(fileName);
HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(file));
for(int i=0; i<book.getNumberOfSheets(); i++) {
List<HSSFRow> rowList= new ArrayList<>();
HSSFSheet sheetAt = book.getSheetAt(i);
int rows = sheetAt.getPhysicalNumberOfRows();
for(int j=1 ;j<rows;j++){
HSSFRow row = sheetAt.getRow(j);
if (row==null) {
continue;
}
rowList.add(row);
}
rowes.put(i, rowList);
}
return rowes;
}
/**
* 克隆對象
* @param obj
* @return
*/
public static <T> T clone(T obj){
T result=null;
try {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(obj);
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
result=(T)ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
static class WebDto implements Serializable{
private static final long serialVersionUID = -1470427074967045384L;
//city
private Serializable name;
//行政区域
private Serializable area;
private Serializable gaArea;
public Serializable getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Serializable getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public Serializable getGaArea() {
return gaArea;
}
public void setGaArea(Serializable gaArea) {
this.gaArea = gaArea;
}
@Override
public String toString() {
return "WebDto [name=" + name + ", area=" + area
+ ", gaArea=" + gaArea + "]";
}
public WebDto(Serializable name, Serializable area, Serializable gaArea) {
super();
this.name = name;
this.area = area;
this.gaArea = gaArea;
}
public WebDto() {
super();
}
}
}