package com.xhgjky.ssh.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class JxlReadExcel {
/**
* JXL解析Excel
* @return
* */
public static List<String> main(String path) {
// TODO Auto-generated method stub
List<String> cx = new ArrayList<String>();
List<String> flag = new ArrayList<String>();
String s = null;
int h;
int l;
try {
//创建workbook
// Workbook workbook = Workbook.getWorkbook(new File("C:/Users/Administrator/Desktop/poi_test.xls"));
Workbook workbook = Workbook.getWorkbook(new File(path));
//获取工作表sheet
Sheet sheet = workbook.getSheet(0);
//获取数据
boolean found = false;
for (int i = 1; i < sheet.getRows() && !found; i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
Cell cell = sheet.getCell(j, i);
//判断必填项是否填了,填的格式是否正确
if(i > 1){
h = i + 1;
l = j + 1;
if(j == 0){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}else {
boolean f = (cell.getContents().trim()).matches("^[A-Za-z0-9]+$");
if(f == false){
s = "第"+h+"行、第"+l+"列格式错误,请确认后重新上传!";
found = true;
break;
}
}
}else if(j == 1){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}
}else if(j == 2){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}else {
boolean f = (cell.getContents().trim()).matches("^[A-Za-z0-9]+$");
if(f == false){
s = "第"+h+"行、第"+l+"列格式错误,请确认后重新上传!";
found = true;
break;
}
}
}else if(j == 3){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}else {
if(!("变电施工").equals(cell.getContents().trim())&&!("线路施工").equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列格式错误,请确认后重新上传!";
found = true;
break;
}
}
}
else if(j == 4){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}
}
else if(j == 5){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}
}else if(j == 6){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}
}else if(j == 7){
if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
s = "第"+h+"行、第"+l+"列不能为空,请确认后重新上传!";
found = true;
break;
}else {
if(!(cell.getContents().trim()).matches("\\d*")){
s = "第"+h+"行、第"+l+"列格式错误,请确认后重新上传!";
found = true;
break;
}
}
}
}
// System.out.print(cell.getContents()+" ");
cx.add(cell.getContents());
}
// System.out.println();
}
workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(s == null){
return cx;
}else {
flag.add("err");
flag.add(s);
return flag;
}
}
}
转载于:https://my.oschina.net/yongqingfan/blog/535215