带注解的excel解析工具

常用的excel 解析 解析之后只能自己再根据列顺序去调整,然后把值设置到bean中,这个用注解的方式,注解到bean上去。框架使用的事poi框架

public class ExcelUtils {
    public static List read(InputStream inputStream, int startRow) throws Exception{
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        List<List> returnDate = new ArrayList<>();
        for (int i=startRow;i<sheet.getLastRowNum();i++){
            HSSFRow row = sheet.getRow(i);
            ArrayList<String> data = new ArrayList<>();
            for (int j=0;j<row.getLastCellNum();j++){
                HSSFCell cell = row.getCell(j);
                cell.setCellType(CellType.STRING);
                String stringCellValue = cell.getStringCellValue();
                if (j==4){
                    Date javaDate = DateUtil.getJavaDate(Double.parseDouble(stringCellValue));
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(format.format(javaDate));
                }
                data.add(stringCellValue);
            }
            returnDate.add(data);
        }
        return returnDate;
    }
public  static <T> List<T> readP(InputStream inputStream, int startRow,Class<T> tClass)throws Exception{
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
    List<T> returnDate = new ArrayList<>();
    for (int i=startRow;i<sheet.getLastRowNum();i++){
        HSSFRow row = sheet.getRow(i);
        T t = tClass.newInstance();
        Field[] declaredFields = tClass.getDeclaredFields();

        for (Field file:declaredFields) {
            if (file.isAnnotationPresent(MyExcelProperty.class)) {
                MyExcelProperty myExcelProperty = file.getAnnotation(MyExcelProperty.class);
                int index = myExcelProperty.index();
                String format = myExcelProperty.format();
                HSSFCell cell = row.getCell(index);
                cell.setCellType(CellType.STRING);
                String stringCellValue = cell.getStringCellValue();
                if (!StringUtils.isEmpty(format) &&
                            file.getGenericType().toString().equals("class java.lang.String")) {
                        Date javaDate = DateUtil.getJavaDate(Double.parseDouble(stringCellValue));
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                        String format1 = simpleDateFormat.format(javaDate);
                        PropertyDescriptor pd = new PropertyDescriptor(file.getName(), tClass);
                        Method writeMethod = pd.getWriteMethod();
                        writeMethod.invoke(t, format1);
                    } else if (file.getGenericType().toString().equals("class java.lang.String")) {
                        PropertyDescriptor pd = new PropertyDescriptor(file.getName(), tClass);
                        Method writeMethod = pd.getWriteMethod();
                        writeMethod.invoke(t, stringCellValue);
                    }

            }


        }

        returnDate.add(t);
    }
    return returnDate;

    }
    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("E:\\test\\test.xls");
//        List read = read(fileInputStream, 1);
        List<Test> tests = readP(fileInputStream, 1, Test.class);
        System.out.println(tests);
//        System.out.println(read);
    }
}



@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyExcelProperty {

    int index() default 99999;

    String format() default "";
}`

使用方法在main方法中,只实现了读没有实现写,而且对事件的处理也不是很合理,有升级版的再更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值