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;
}
}