package com.cp.base.util;
import com.cp.annotation.NameAnnotation;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
final static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
/**
* 解析excel
* @param in
* @return
* @throws IOException
*/
public static List<Map<Integer,Object>> parseExcel(InputStream in) throws IOException {
List<Map<Integer,Object>> list = new ArrayList<>();
//此种创建方式兼容2003 2007
Workbook workbook = WorkbookFactory.create(in);
//获取第一个工作簿 有且只有一个
Sheet sheet = workbook.getSheetAt(0);
//获取第一行
int firstRowNum = sheet.getFirstRowNum();
Row row = null;
if (firstRowNum > 0){
//校验表头信息 是否匹配
Row firstRow = sheet.getRow(firstRowNum);
//总列数
int lastCellNum = firstRow.getLastCellNum();
//第一列
//int firstCellNum = firstRow.getFirstCellNum();
if (lastCellNum > 0){
//遍历所有的行
for (int i = firstRowNum + 1;i < sheet.getLastRowNum(); i++){
//每一行
row = sheet.getRow(i);
Map<Integer,Object> map = new HashMap<>();
//遍历所有的列
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++){
Cell cell = row.getCell(j);
Object value = getValue(cell);
map.put(j,value);
}
list.add(map);
}
}
}
return list;
}
/**
* 解析excel
* @param in
* @return
* @throws IOException
*/
public static <T> List<T> parseExcel(InputStream in,Class<T> clazz) throws IOException, IllegalAccessException, InstantiationException {
List<T> model = new ArrayList<>();
//此种创建方式兼容2003 2007
Workbook workbook = WorkbookFactory.create(in);
//获取第一个工作簿 有且只有一个
Sheet sheet = workbook.getSheetAt(0);
//获取第一行
int firstRowNum = sheet.getFirstRowNum();
Row row = null;
if (firstRowNum > 0){
//校验表头信息 是否匹配
Row firstRow = sheet.getRow(firstRowNum);
//总列数
int lastCellNum = firstRow.getLastCellNum();
logger.info("总列数:{}",lastCellNum);
//第一列
//int firstCellNum = firstRow.getFirstCellNum();
if (lastCellNum > 0){
//遍历所有的行
for (int i = firstRowNum + 1;i < sheet.getLastRowNum(); i++){
//每一行
row = sheet.getRow(i);
//创建实例
T t = clazz.newInstance();
//遍历所有的列
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++){
if (row.getCell(j) != null){
//获取所有属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields){
//允许访问私有属性值
field.setAccessible(true);
//表头 与字段对应
NameAnnotation annotation = field.getAnnotation(NameAnnotation.class);
if(annotation == null){
continue;
} else {
//获取表头
String headName = ((String) getValue(firstRow.getCell(j))).trim();
logger.info("表头:{}",headName);
logger.info("注解名称:{}",annotation.name());
if (headName.equals(annotation.name())){
final Class<?> type = field.getType();
logger.info("属性类型:{}",type.toString());
if (type.equals(String.class)){
field.set(t,row.getCell(j).getStringCellValue().trim());
}
if (type.equals(Integer.class)){
field.set(t,new Integer((Integer) getValue(row.getCell(j))));
}
if (type.equals(BigDecimal.class)){
field.set(t,new BigDecimal(getValue(row.getCell(j)).toString()));
}
}
}
}
model.add(t);
}
}
}
}
}
logger.info("解析结果:{}",model);
return model;
}
/**
* 获取不同类型的值
* @param cell
* @return
*/
private static Object getValue(Cell cell){
Object value = null;
switch (cell.getCellType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
value = cell.getNumericCellValue();
break;
default:
value = "";
break;
}
return value;
}
}
package com.cp.annotation;
import java.lang.annotation.*;
/**
* 注解 excel表头与字段对应
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NameAnnotation {
String name() default "";
}
通过poi读取Excel的内容
猜你喜欢
转载自blog.csdn.net/qq_34724270/article/details/84063473
今日推荐
周排行