package com.njcn.utils;


import org.apache.poi.ss.usermodel.*;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author hongawen
 * @Date: 2018/11/12 10:29
 */
public class ExcelReaderUtil {


    public String getDatasByrc(int beginRow, Map<Integer,String> beanpros, String classPathName, List<Object> results, Sheet sheet){
        try {
            Class clazz = Class.forName("com.njcn.pojo.data.EventDetailExcelBody");
            Set<Integer> set = beanpros.keySet() ;
            Row row = sheet.getRow(beginRow);
            while(row!=null) {
                Object obj = clazz.newInstance() ;
                for(Integer key:set){
                    String value = "" ;
                    if(row!=null){
                        Cell cell = row.getCell(key) ;
                        if(cell!=null){
                            int type = cell.getCellType() ;
                            if(type == Cell.CELL_TYPE_STRING){
                                value = cell.getStringCellValue() ;
                            }else if(type==Cell.CELL_TYPE_NUMERIC||type==Cell.CELL_TYPE_FORMULA){
                                value = String.valueOf(cell.getNumericCellValue());
                            }else if(type==Cell.CELL_TYPE_BOOLEAN){
                                value = String.valueOf(cell.getBooleanCellValue()) ;
                            }
                        }
                    }
                    PropertyDescriptor pd = new PropertyDescriptor(beanpros.get(key),clazz);
                    Method wM = pd.getWriteMethod();
                    wM.invoke(obj, value);
                }
                results.add(obj);
                row = sheet.getRow(++beginRow);
            }
        } catch (Exception e) {
            return "用于接受结果的bean不存在";
        }

        return null ;
    }

}
