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); } }
java动态获取Excel中的文本信息
最新推荐文章于 2024-05-17 20:15:57 发布