注釈付き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メソッドでは、読み書きのみが実現され、イベントの処理も合理的ではなく、アップグレード版の再更新があります