excel信息的读取异常记录

web_backend_develope
chenlw 8 years ago
parent e375d19336
commit 8c568273f0

@ -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<String> filters = new ArrayList<String>();
Class<?> classz = obj.getClass();
filters.addAll(Arrays.asList(filter));
List list = new ArrayList<Object>();;
List list = new ArrayList<Object>();
//读取工作表
XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
XSSFRow row;
@ -173,10 +180,22 @@ public class ExcelOperation {
//如果有数据
if (rowNum > excelInitRow) {
list = new ArrayList<Object>();
//校验是否是 模板?
if (!checkTemplate(sheet, excelInitRow)) {
String name = fileName;
String value = " 该模板不符合要求! ";
HashMap<String, Object> map = new HashMap<String, Object>();
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<String, Object> map = new HashMap<String, Object>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("value", value);
// boolean isadd = true;
@ -238,7 +258,7 @@ public class ExcelOperation {
// }
// }
// if (isadd) {
rowErr.add((HashMap<String, Object>) map);
rowErr.add(map);
// }
break;
}
@ -249,7 +269,7 @@ public class ExcelOperation {
int errRownum = i+1;
String name = fileName;
String value = " 第"+errRownum+"行信息不完整,该行请重新填写";
Map<String, Object> map = new HashMap<String, Object>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("value", value);
// boolean isadd = true;
@ -259,7 +279,7 @@ public class ExcelOperation {
// }
// }
// if (isadd) {
rowErr.add((HashMap<String, Object>) 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<Integer, String> 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<Integer, String> map3 = new HashMap<Integer, String>();
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<Integer, String> map2 = new HashMap<Integer, String>();
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;
}
}

Loading…
Cancel
Save