From 5ba8639379e6ccda58dbe0381219a47ebd223ed8 Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Tue, 13 Dec 2016 09:50:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/excelUtils/ExcelOperation.java | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index 87a20de5..777c511b 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -152,11 +152,11 @@ public class ExcelOperation { * @throws InstantiationException * @throws IllegalAccessException */ - public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, Object obj, String... filter) throws InstantiationException, IllegalAccessException { + public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, String fileName, List rowErr, Object obj, String... filter) throws InstantiationException, IllegalAccessException { List filters = new ArrayList(); Class classz = obj.getClass(); filters.addAll(Arrays.asList(filter)); - List list = null; + List list = new ArrayList();; //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; @@ -176,7 +176,7 @@ public class ExcelOperation { //获得当前行 row = sheet.getRow(i); cell = row.getCell(excelInitCell); - if ("-".equals(getValue(cell))) { + if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { continue; } //实例化 @@ -203,23 +203,43 @@ public class ExcelOperation { isAddObj = true; String type = tmpField.getGenericType().toString(); //int型 - if (type.contains("Integer") ||type.contains("int")) { - double dnum = Double.valueOf(cellString); - int inum = (int) dnum; - tmpField.set(newObj, inum); - } - //double型 - else if (type.contains("Double") ||type.contains("double")) { - tmpField.set(newObj, Double.valueOf(cellString)); - } - //boolean型 - else if (type.contains("boolean") ||type.contains("Boolean")) { - tmpField.set(newObj, Boolean.valueOf(cellString)); - } - else { - tmpField.set(newObj, cellString); - } + try{ + if (type.contains("Integer") ||type.contains("int")) { + double dnum = Double.valueOf(cellString); + int inum = (int) dnum; + tmpField.set(newObj, inum); + } + //double型 + else if (type.contains("Double") ||type.contains("double")) { + tmpField.set(newObj, Double.valueOf(cellString)); + } + //boolean型 + else if (type.contains("boolean") ||type.contains("Boolean")) { + tmpField.set(newObj, Boolean.valueOf(cellString)); + } + else { + tmpField.set(newObj, cellString); + } + }catch(Exception e){ + isAddObj = false; + int errRownum = i+1; + String tmpErrString = fileName+" 第"+errRownum+"行信息有误,注意数字属性,该行请重新填写"; + if (!rowErr.contains(tmpErrString)) { + rowErr.add(tmpErrString); + } + break; + } } + else { + // 记录 excel中为空 的数据--且该条记录不记录进数据库 + isAddObj = false; + int errRownum = i+1; + String tmpErrString = fileName+" 第"+errRownum+"行信息不完整,该行请重新填写"; + if (!rowErr.contains(tmpErrString)) { + rowErr.add(tmpErrString); + } + break; + } } }