实例所需bean
ExcelColumn
excel读取写入所需注解
import java.lang.annotation.*;
/**
* Created by admin on 2017/1/9.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelColumn {
String value() default "";
/**
*从1开始
* @return
*/
int col() default 0;
}
ExcelUtils
封装了读取写入方法
import com.cs.test.utils.DateFormatter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* Created by admin on 2017/1/17.
*/
public class ExcelUtils {
private final static Logger log = LoggerFactory.getLogger(ExcelUtils.class);
private final static String EXCEL2003 = "xls";
private final static String EXCEL2007 = "xlsx";
public static <T> List<T> readExcel(String path, Class<T> cls){
List<T> dataList = new ArrayList<>();
Workbook workbook = null;
try {
if(path.endsWith(EXCEL2007)){
FileInputStream is = new FileInputStream(new File(path));
workbook = new XSSFWorkbook(is);
}
if(path.endsWith(EXCEL2003)){
FileInputStream is = new FileInputStream(new File(path));
workbook = new HSSFWorkbook(is);
}
if(workbook != null){
//类映射 注解 value-->bean columns
Map<String, List<Field>> classMap = new HashMap<>();
Fi