diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index ea5cbde6..5303697a 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.util.HSSFColor; @@ -18,12 +19,18 @@ import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.core.io.ClassPathResource; + +import com.platform.service.impl.PreDataInfoServiceImpl; +import com.platform.utils.Configs; +import com.platform.utils.Constant; /** * @author chen * excel2007操作 */ public class ExcelOperation { + public final static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(ExcelOperation.class); /** 写入excel * @param workbook 模板 @@ -158,7 +165,7 @@ public class ExcelOperation { List filters = new ArrayList(); Class classz = obj.getClass(); filters.addAll(Arrays.asList(filter)); - List list = new ArrayList();; + List list = new ArrayList(); //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; @@ -173,10 +180,22 @@ public class ExcelOperation { //如果有数据 if (rowNum > excelInitRow) { list = new ArrayList(); + //校验是否是 模板? + if (!checkTemplate(sheet, excelInitRow)) { + String name = fileName; + String value = " 该模板不符合要求! "; + HashMap map = new HashMap(); + map.put("name", name); + map.put("value", value); + rowErr.add(map); + } //遍历每行数据 for (int i = excelInitRow; i < rowNum; i++) { //获得当前行 row = sheet.getRow(i); + if (null == row) { + break; + } cell = row.getCell(excelInitCell); if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { continue; @@ -223,12 +242,13 @@ public class ExcelOperation { tmpField.set(newObj, cellString); } }catch(Exception e){ + log.error(e); isAddObj = false; int errRownum = i+1; String name = fileName; String value = " 第"+errRownum+"行信息有误,注意数字属性,该行请重新填写"; - Map map = new HashMap(); + HashMap map = new HashMap(); map.put("name", name); map.put("value", value); // boolean isadd = true; @@ -238,7 +258,7 @@ public class ExcelOperation { // } // } // if (isadd) { - rowErr.add((HashMap) map); + rowErr.add(map); // } break; } @@ -249,7 +269,7 @@ public class ExcelOperation { int errRownum = i+1; String name = fileName; String value = " 第"+errRownum+"行信息不完整,该行请重新填写"; - Map map = new HashMap(); + HashMap map = new HashMap(); map.put("name", name); map.put("value", value); // boolean isadd = true; @@ -259,7 +279,7 @@ public class ExcelOperation { // } // } // if (isadd) { - rowErr.add((HashMap) map); + rowErr.add(map); // } break; } @@ -344,4 +364,61 @@ public class ExcelOperation { } return num; } + + public static boolean checkTemplate(XSSFSheet sheet, int excelInitRow) { + boolean isThisTemplate = true; + //模板 标题 行 + for(Integer i : ExcelHeadLine.keySet()){ + HashMap map = ExcelHeadLine.get(i); + XSSFRow row = sheet.getRow(excelInitRow -i); + for (Integer key : map.keySet()) { + XSSFCell cell = row.getCell(key); + String cellString = getValue(cell); + if (!cellString.equals(map.get(key))) { + return false; + } + } + } + return isThisTemplate; + } + + public static boolean initTemplate(int excelInitRow, int totalcoll) { + ClassPathResource res = new ClassPathResource(Constant.SYSTEM_INFO_EXCEL_TEMPLATE_FIEL_PATH); + XSSFSheet sheet = null; + boolean isThisTemplate = true; + + //模板 标题 行 + try { + XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream()); + //读取工作表 + sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); + XSSFRow row = sheet.getRow(excelInitRow -3); + HashMap map3 = new HashMap(); + for (int j = 0; j < totalcoll; j++) { + XSSFCell cell = row.getCell(j); + if (null == cell) { + break; + } + String cellString = getValue(cell); + map3.put(j, cellString); + } + ExcelHeadLine.put(3, map3); + XSSFRow row2 = sheet.getRow(excelInitRow -2); + HashMap map2 = new HashMap(); + for (int j = 0; j < totalcoll; j++) { + XSSFCell cell = row2.getCell(j); + if (null == cell) { + break; + } + String cellString = getValue(cell); + map2.put(j, cellString); + } + ExcelHeadLine.put(2, map2); + } catch (IOException e) { + isThisTemplate = false; + log.error(e); + } + + return isThisTemplate; + } }