java poi注解使用_利用注解和POI导入Excel

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

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.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import poi.demo.annotation.ExcelField;

public class ExcelUtil {

private Workbook wb;

private Sheet sheet;

private int headerNum;//标题行号

public ExcelUtil(String fileName, int headerNum, int sheetIndex) throws FileNotFoundException, IOException{

this(new File(fileName), headerNum, sheetIndex);

}

public ExcelUtil(File file, int headerNum, int sheetIndex) throws FileNotFoundException, IOException {

this(file.getName(), new FileInputStream(file), headerNum, sheetIndex);

}

public ExcelUtil(String fileName, InputStream is, int headerNum, int sheetIndex) throws IOException {

// TODO Auto-generated constructor stub

if(fileName.toLowerCase().endsWith("xls")){

this.wb = new HSSFWorkbook(is);

}else if(fileName.toLowerCase().endsWith("xlsx")){

this.wb = new XSSFWorkbook(is);

}else{

throw new RuntimeException("文档格式不正确!");

}

this.sheet= this.wb.getSheetAt(sheetIndex);

this.headerNum=headerNum;

}

/**

*@title getDataList

*@description 获取转化后的Excel数据列表

*@param clazz

*@return

*@throws InstantiationException

*@throws IllegalAccessException List

*/

public List getDataList(Class clazz) throws InstantiationException, IllegalAccessException{

List annotationList = new ArrayList();

//获取带有注解的属性

Field[] fs = clazz.getDeclaredFields();

for(Field f : fs){

ExcelField ef = f.getAnnotation(ExcelField.class);

if(ef != null){

annotationList.add(new Object[]{ef,f});

}

}

//排序

Collections.sort(annotationList, new Comparator() {

public int compare(Object[] o1, Object[] o2) {

return new Integer(((ExcelField)o1[0]).sort()).compareTo(new Integer(((ExcelField)o2[0]).sort()));

}

});

List dataList = new ArrayList();

for(int i=this.getDataRowNum();i<=this.getLastDataRowNum();i++){

E e = (E)clazz.newInstance();

int column = 0;

Row row = this.getRow(i);

for(Object[] obj : annotationList){

Object val = this.getCellValue(row, column++);

if(val != null){

@SuppressWarnings("unused")

ExcelField ef = (ExcelField)obj[0];

if(obj[1] instanceof Field){

ReflectionUtils.invokeSetter(e, ((Field)obj[1]).getName(), val);

}

}

}

dataList.add(e);

}

return dataList;

}

/**

*@title getRow

*@description 获取行数据

*@param rowNum

*@return Row

*/

public Row getRow(int rowNum){

return this.sheet.getRow(rowNum);

}

/**

*@title getDataRowNum

*@description 获取数据行号

*@return int

*/

public int getDataRowNum(){

return this.headerNum+1;

}

/**

*@title getLastDataRowNum

*@description 获取数据最后一行行号

*@return int

*/

public int getLastDataRowNum(){

return this.sheet.getLastRowNum()+headerNum;

}

/**

*@title getCellValue

*@description 获取单元格的值

*@param row

*@param column

*@return Object

*/

public Object getCellValue(Row row, int column){

Object val = "";

try{

Cell cell = row.getCell(column);

if (cell != null){

if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){

val = cell.getNumericCellValue();

}else if (cell.getCellType() == Cell.CELL_TYPE_STRING){

val = cell.getStringCellValue();

}else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){

val = cell.getCellFormula();

}else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){

val = cell.getBooleanCellValue();

}else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){

val = cell.getErrorCellValue();

}

}

}catch (Exception e) {

return val;

}

return val;

}

public Workbook getWb() {

return wb;

}

public void setWb(Workbook wb) {

this.wb = wb;

}

public Sheet getSheet() {

return sheet;

}

public void setSheet(Sheet sheet) {

this.sheet = sheet;

}

public int getHeaderNum() {

return headerNum;

}

public void setHeaderNum(int headerNum) {

this.headerNum = headerNum;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值