利用POI插件导入excel 读取合并行数据(上)
Posted yizhizhangblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用POI插件导入excel 读取合并行数据(上)相关的知识,希望对你有一定的参考价值。
图为要导入的excel格式
分析一下:
前一部分数据是读取 合并行 存入一张 “会见” 表 ,后面蓝色的 非合并行 存入 “会见人信息” 表。
先说后台方法,(读取本地文件例子)
public void importJsInfo() throws IOException { Boolean bool = false; Record record =new Record(); String name ="1.xlsx"; FileInputStream f = new FileInputStream(new File("C:\\Users\\foresee\\Desktop\\1.xlsx")); record = readXls(f,name); //读取excel方法 if(record.get("list") != null ) { List<Hjrxx> hjrxxList = record.get("hjrxxList"); List<Hj> hjList = record.get("list"); bool = HjService.service.saveJsxx(hjrxxList,hjList); //把读取结果封装在record对象里,并保存两张表的业务逻辑 } renderText(String.valueOf(bool)); }
下面是 :上面调用的读取excel方法 readXls (注意下身份证长数字和日期格式处理)
/** * 读取excel * @throws IOException */ private Record readXls(InputStream inputStream,String fileName) throws IOException { Record record = new Record(); boolean isE2007 = false; //判断是否是excel2007格式 if(fileName.endsWith("xlsx")){ isE2007 = true; } int rowIndex = 0; int columnIndex = 0; InputStream input = inputStream; //建立输入流 Workbook wb = null; //根据文件格式(2003或者2007)来初始化 if(isE2007){ wb = new XSSFWorkbook(input); }else{ wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(0); //获得第一个表单 List<CellRangeAddress> cras = importHj.getCombineCell(sheet); //isMergedRegion(Sheet sheet,int row ,int column);判断是不是合并单元格 int count = sheet.getLastRowNum()+1;//总行数 List<Hj> hjs = new ArrayList<>(); List<Hjrxx> hjrxxs = new ArrayList<>(); //这两个LIst最后要放入record这个对象里,作为方法的返回值 for(int i = 2; i < count;i++){ rowIndex = i; Row row = sheet.getRow(i); Hj hj = new Hj(); if(importHj.getCellValue(row.getCell(0)) != " ") { String sfzh = NumberToTextConverter.toText(row.getCell(0).getNumericCellValue()); //根据表格获得的身份证查出另一个字段值,存入数据库 Record jbxx = JbxxService.service.findByBrSfzh(sfzh); hj.set(Hj.column_br_id, jbxx.get("br_id")); } //如果该行是日期,同样是需要先处理,否则会报错 if (HSSFDateUtil.isCellDateFormatted(row.getCell(3))) {// 处理日期格式、时间格式 SimpleDateFormat sdf = null; sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = row.getCell(3).getDateCellValue(); Date jssj = row.getCell(4).getDateCellValue(); String a =sdf.format(date);//根据需要取时间,date类型和String类型 String b =sdf.format(jssj); System.out.println(a); hj.set(Hj.column_kssj, ToolDateTime.parse(a, ToolDateTime.pattern_ymd_hm)); hj.set(Hj.column_jssj, ToolDateTime.parse(b, ToolDateTime.pattern_ymd_hm)); } hj.set(Hj.column_hjlx,"01"); //会见实体类 hj.set(Hj.column_pzbm,importHj.getCellValue(row.getCell(1))); hj.set(Hj.column_pzr,importHj.getCellValue(row.getCell(2))); hj.set(Hj.column_jbmj,importHj.getCellValue(row.getCell(5))); hj.set(Hj.column_bz,importHj.getCellValue(row.getCell(6))); hj.set(Hj.column_createtm, ToolDateTime.getSqlTimestamp(new Date())); hj.set(Hj.column_updatetm, ToolDateTime.getSqlTimestamp(new Date())); hj.set(Hj.column_validmk, Hj.VALIDMK_1); Hjrxx hjrxx = null; if(importHj.isMergedRegion(sheet,i,0)){ int lastRow = importHj.getRowNum(cras,sheet.getRow(i).getCell(0),sheet); for(;i<=lastRow;i++){ row = sheet.getRow(i); hjrxx = new Hjrxx(); //会见人信息实体类 hjrxx.set("hjrxm",importHj.getCellValue(row.getCell(7))); hjrxx.set("sfzh",importHj.getCellValue(row.getCell(8))); hjrxx.set("lxfs",importHj.getCellValue(row.getCell(9))); hjrxx.set("szdw",importHj.getCellValue(row.getCell(10))); hjrxx.set("hjdz",importHj.getCellValue(row.getCell(11))); hjrxx.set(Hjrxx.column_createtm, ToolDateTime.getSqlTimestamp(new Date())); hjrxx.set(Hjrxx.column_updatetm, ToolDateTime.getSqlTimestamp(new Date())); hjrxx.set(Hjrxx.column_validmk, Hjrxx.VALIDMK_1); hjrxxs.add(hjrxx); } i--; }else{ row = sheet.getRow(i); hjrxx = new Hjrxx(); hjrxx.set("hjrxm",importHj.getCellValue(row.getCell(7))); hjrxx.set("sfzh",importHj.getCellValue(row.getCell(8))); hjrxx.set("lxfs",importHj.getCellValue(row.getCell(9))); hjrxx.set("szdw",importHj.getCellValue(row.getCell(10))); hjrxx.set("hjdz",importHj.getCellValue(row.getCell(11))); hjrxx.set(Hjrxx.column_createtm, ToolDateTime.getSqlTimestamp(new Date())); hjrxx.set(Hjrxx.column_updatetm, ToolDateTime.getSqlTimestamp(new Date())); hjrxx.set(Hjrxx.column_validmk, Hjrxx.VALIDMK_1); hjrxxs.add(hjrxx); } hjs.add(hj); } record.set("list", hjs); record.set("hjrxxList",hjrxxs); return record; }
上面用到的工具类,单独放一个文件,直接调用(工具类从网上抄的,没改动)
注:该文件为本地一个文件。下篇写上传一个文件,并导入数据库。
package com.platform.mvc.aq.hj; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; public class importHj { /** * 获取单元格的值 * @param cell getCombineCell * @return */ public static String getCellValue(Cell cell){ if(cell == null) return ""; if(cell.getCellType() == Cell.CELL_TYPE_STRING){ return cell.getStringCellValue(); }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){ return String.valueOf(cell.getBooleanCellValue()); }else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){ return cell.getCellFormula() ; } return ""; } /** * 合并单元格处理,获取合并行 * @param sheet * @return List<CellRangeAddress> */ public static List<CellRangeAddress> getCombineCell(Sheet sheet) { List<CellRangeAddress> list = new ArrayList<CellRangeAddress>(); //获得一个 sheet 中合并单元格的数量 int sheetmergerCount = sheet.getNumMergedRegions(); //遍历所有的合并单元格 for(int i = 0; i<sheetmergerCount;i++) { //获得合并单元格保存进list中 CellRangeAddress ca = sheet.getMergedRegion(i); list.add(ca); } return list; } static int getRowNum(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet){ int xr = 0; int firstC = 0; int lastC = 0; int firstR = 0; int lastR = 0; for(CellRangeAddress ca:listCombineCell) { //获得合并单元格的起始行, 结束行, 起始列, 结束列 firstC = ca.getFirstColumn(); lastC = ca.getLastColumn(); firstR = ca.getFirstRow(); lastR = ca.getLastRow(); if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { xr = lastR; } } } return xr; } /** * 判断单元格是否为合并单元格,是的话则将单元格的值返回 * @param listCombineCell 存放合并单元格的list * @param cell 需要判断的单元格 * @param sheet sheet * @return */ public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet) throws Exception{ int firstC = 0; int lastC = 0; int firstR = 0; int lastR = 0; String cellValue = null; for(CellRangeAddress ca:listCombineCell) { //获得合并单元格的起始行, 结束行, 起始列, 结束列 firstC = ca.getFirstColumn(); lastC = ca.getLastColumn(); firstR = ca.getFirstRow(); lastR = ca.getLastRow(); if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { Row fRow = sheet.getRow(firstR); Cell fCell = fRow.getCell(firstC); cellValue = getCellValue(fCell); break; } } else { cellValue = ""; } } return cellValue; } /** * 获取合并单元格的值 * @param sheet * @param row * @param column * @return */ public String getMergedRegionValue(Sheet sheet ,int row , int column){ int sheetMergeCount = sheet.getNumMergedRegions(); for(int i = 0 ; i < sheetMergeCount ; i++){ CellRangeAddress ca = sheet.getMergedRegion(i); int firstColumn = ca.getFirstColumn(); int lastColumn = ca.getLastColumn(); int firstRow = ca.getFirstRow(); int lastRow = ca.getLastRow(); if(row >= firstRow && row <= lastRow){ if(column >= firstColumn && column <= lastColumn){ Row fRow = sheet.getRow(firstRow); Cell fCell = fRow.getCell(firstColumn); return getCellValue(fCell) ; } } } return null ; } /** * 判断指定的单元格是否是合并单元格 * @param sheet * @param row 行下标 * @param column 列下标 * @return */ static boolean isMergedRegion(Sheet sheet,int row ,int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress range = sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastColumn = range.getLastColumn(); int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); if(row >= firstRow && row <= lastRow){ if(column >= firstColumn && column <= lastColumn){ return true; } } } return false; } }
以上是关于利用POI插件导入excel 读取合并行数据(上)的主要内容,如果未能解决你的问题,请参考以下文章
java利用poi技术导入批量excel数据,并且分段存入数据库怎么解决