java动态获取Excel中的文本信息

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author zbf
 * @DATA 2019/9/4 15:14
 * @ClassIntroduction
 *
 *
 * <dependency>
 *     <groupId>org.apache.poi</groupId>
 *     <artifactId>poi-ooxml</artifactId>
 *     <version>3.17</version>
 * </dependency>
 */
public class ExcelUtil {

    /**
     * 读取excel文件
     * 注意:在使用的过程中,文件中每一列的的信息必需和对象属性一 一对应
     * 读取excel 中的文本信息
      * @param pathUrl 文本路径
     * @param sheet 需要读取的sheet
     * @param row 数据从哪一行开始读取
     * @param tClass 对象
     * @param format 指定格式中的时间格式 默认时间格式yyyy-MM-dd HH:mm:ss
     * @param <T>
     * @return 
     */
  public static<T> List<T> getFileRead(String pathUrl, int sheet, int row, Class<T> tClass, String format){
      List<T> result=new ArrayList<>();
      File file = new File(pathUrl);
      FileInputStream fis=null;
      XSSFWorkbook xfb=null;
      try {
          fis= new FileInputStream(file);
          //获得工作薄
          xfb= new XSSFWorkbook(fis);
          //获得工作表
          XSSFSheet sheetAt = xfb.getSheetAt(sheet);
          //获得工作表的总行数
          int sheetRow = sheetAt.getPhysicalNumberOfRows();
          while (row<sheetRow){
              //获得总列数
              int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();
              //获得属性数组
              Field[] fields = tClass.getDeclaredFields();
              T t = tClass.newInstance();
              for (int i=0;i<physicalNumberOfCells;i++){
                  XSSFCell cell = sheetAt.getRow(row).getCell(i);
                  if (cell==null){
                    continue;
                  }
                  //获得sheet中值,都是设置成string类型进行获取
                  cell.setCellType(CellType.STRING);
                  String stringCellValue =cell.getStringCellValue();
                  //读取excel中的值,如果为空,直接进入下一列
                  if (null==stringCellValue || "".equals(stringCellValue)){
                      continue;
                  }
                  //获得属性名
                  String name = fields[i].getName();
                  //得到set方法
                  name=name.substring(0,1).toUpperCase()+name.substring(1);
                  String tMethodName="set"+name;

                  String[] split = fields[i].getType().getName().split("\\.");
                  //得到数据类型
                  String type= split[split.length-1];

                  Method method = tClass.getDeclaredMethod(tMethodName, fields[i].getType());
                  //如果还有其它类型,只需要在这里进行相应的补充就可以
                  switch (type){
                      case "Date":
                          method.invoke(t, getDate(stringCellValue,format));
                          break;
                      case "Integer":
                          method.invoke(t,Integer.valueOf(stringCellValue));
                          break;
                      case "Byte":
                          method.invoke(t,Byte.valueOf(stringCellValue));
                          break;
                      case "Double":
                          method.invoke(t,Double.valueOf(stringCellValue));
                          break;
                      case "BigDecimal":
                          method.invoke(t, BigDecimal.valueOf(stringCellValue.contains("\\.")?Double.valueOf(stringCellValue):Integer.valueOf(stringCellValue)));
                          break;
                      default:
                          method.invoke(t,stringCellValue);
                          break;
                  }
              }
              result.add(t);
              //读取下一行
              row++;
          }
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (InstantiationException e) {
          e.printStackTrace();
      } catch (NoSuchMethodException e) {
          e.printStackTrace();
      } catch (InvocationTargetException e) {
          e.printStackTrace();
      } catch (ParseException e) {
          e.printStackTrace();
      } finally {
          try {
              xfb.close();
              fis.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
   return result;
  }

    /**
     * 转换时间格式
     * @param dataStr 时间字符串
     * @param format 需要转换的格式信息
     * @return
     * @throws ParseException
     */
  private static Date getDate(String dataStr,String format) throws ParseException {
      if (null==format || "".equals(format)){
          format="yyyy-MM-dd HH:mm:ss";
      }
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
     return simpleDateFormat.parse(dataStr);
  }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值