List lsadd = new ArrayList();
List li = new ArrayList();
jxl.Workbook rwb = null;
try{
//file是表单提交过来的Excel
InputStream is = new FileInputStream(file);
//获取Excel
rwb = Workbook.getWorkbook(is);
//获取Excel中的工作表(sheet)
Sheet rs = rwb.getSheet(0);
//获取有多少行数据
int rsRows = rs.getRows();
for(int i = 1;i < rsRows;i++){
ls = new ArrayList();
//获取第1列第i行的数据
String cell1 = rs.getCell(0,i).getContents();
//获取第2列第i行的数据
String cell7 = rs.getCell(1,i).getContents();
//获取第3列第i行的数据
String cellname = rs.getCell(2,i).getContents();
String[] strs = cell7.split("\\.");
int longs = strs.length;
lsadd.add(cell7);
//判断Excel列中有没有空值
cell = rs.getCell(0, i);
if(State.equals("1")){
if(longs >= 3){
String fileclass = "第"+(i)+"行Excel中层次结构超过二级!";
dom.element("error", fileclass);
dom.forward("entry/importentry");
return;
}
}
if(rs.getCell(0, i).getType() == CellType.EMPTY || rs.getCell(1, i).getType() == CellType.EMPTY || rs.getCell(2, i).getType() == CellType.EMPTY){
state = 1;
System.out.println("第"+(i)+"行******Excel中的格式不正确,存在空列!******");
String fileclass = "第"+(i)+"行Excel中的格式不正确,存在空列!";
dom.element("error", fileclass);
if(State.equals("0")){
dom.forward("entry/importexcel");
}
if(State.equals("1")){
dom.forward("entry/importentry");
}
return;
}
//判断Excel文件中有没有重复的数据
if(li.contains(cell1) && longs == 1){
System.out.println("第"+(i)+"行******Excel中有重复数据!******");
state = 1;
String fileclass = "第"+(i)+"行Excel中有重复数据!";
dom.element("error", fileclass);
if(State.equals("0")){
dom.forward("entry/importexcel");
}
if(State.equals("1")){
dom.forward("entry/importentry");
}
return;
}
li.add(cell1);
//判断Excel中层次结构的格式
if(longs > 1){
String a = cell7.substring(0,cell7.lastIndexOf("."));
if(!lsadd.contains(a)){
System.out.println("第"+(i)+"行Excel中层次结构格式不正确");
state = 1;
String fileclass = "第"+(i)+"行Excel中层次结构格式不正确!";
dom.element("error", fileclass);
if(State.equals("0")){
dom.forward("entry/importexcel");
}
if(State.equals("1")){
dom.forward("entry/importentry");
}
return;
}
}
}
//循环写入数据库