public void excel(){ String excelPath = "C:\\Users\\dell\\Desktop\\9月.xlsx"; Gjzb gjzb = new Gjzb(); try { File excel = new File(excelPath); if (excel.isFile() && excel.exists()) { //判断文件是否存在 String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!! Workbook wb; //根据文件后缀(xls/xlsx)进行判断 if ("xls".equals(split[1])) { FileInputStream fis = new FileInputStream(excel); //文件流对象 wb = new HSSFWorkbook(fis); } else if ("xlsx".equals(split[1])) { wb = new XSSFWorkbook(excel); } else { System.out.println("文件类型错误!"); return; } Sheet sheetAt = wb.getSheetAt(0); int firstRowIndex = sheetAt.getFirstRowNum()+7; //第一行是列名,所以不读 int lastRowIndex = sheetAt.getLastRowNum(); System.out.println("firstRowIndex: "+firstRowIndex); System.out.println("lastRowIndex: "+lastRowIndex); for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { System.out.println("rIndex: " + rIndex); Row row = sheetAt.getRow(rIndex); if (row != null) { int firstCellIndex = row.getFirstCellNum(); int lastCellIndex = row.getLastCellNum(); gjzb.setUuid(UuidUtil.getUuid()); gjzb.setReportdate("201909"); gjzb.setCtime("2019-09-23T11:49:37"); // gjzb.setUnit(row.getCell(1)==null?null:row.getCell(1)+""); gjzb.setBygzys(row.getCell(2)==null?null:row.getCell(2)+""); gjzb.setByxygz(row.getCell(3)==null?null:row.getCell(3)+""); gjzb.setByjmclvgz(row.getCell(4)==null?null:row.getCell(4)+""); gjzb.setByjmclgz(row.getCell(5)==null?null:row.getCell(5)+""); gjzb.setByzjcgz(row.getCell(6)==null?null:row.getCell(6)+""); gjzb.setByaqjl(row.getCell(7)==null?null:row.getCell(7)+""); gjzb.setBywslygz(row.getCell(8)==null?null:row.getCell(8)+""); gjzb.setByqt(row.getCell(9)==null?null:row.getCell(9)+""); gjzb.setByjfxj(row.getCell(10)==null?null:row.getCell(10)+""); gjzb.setBygzze(row.getCell(11)==null?null:row.getCell(11)+""); gjzb.setLjgzys(row.getCell(12)==null?null:row.getCell(12)+""); gjzb.setLjxygz(row.getCell(13)==null?null:row.getCell(13)+""); gjzb.setLjjmclvgz(row.getCell(14)==null?null:row.getCell(14)+""); gjzb.setLjjmclgz(row.getCell(15)==null?null:row.getCell(15)+""); gjzb.setLjzjcgz(row.getCell(16)==null?null:row.getCell(16)+""); gjzb.setLjaqjl(row.getCell(17)==null?null:row.getCell(17)+""); gjzb.setLjwslygz(row.getCell(18)==null?null:row.getCell(18)+""); gjzb.setUnituuid(row.getCell(19)==null?null:row.getCell(19)+""); gjzb.setSort(row.getCell(20)==null?null:Double.valueOf(row.getCell(20)+"")); gjzb.setLjqt(row.getCell(21)==null?null:row.getCell(21)+""); gjzb.setLjjfxj(row.getCell(22)==null?null:row.getCell(22)+""); gjzb.setLjgzze(row.getCell(23)==null?null:row.getCell(23)+""); gjzbMapper.insert(gjzb); } } } }catch (Exception e) { e.printStackTrace(); } }
java解析excel并保存数据库
最新推荐文章于 2023-03-03 13:48:09 发布