利用POI外掛匯入excel 讀取合併行資料
阿新 • • 發佈:2018-11-11
圖為要匯入的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) throwsIOException { 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; } }