今天发现之前的那个导入有问题,只是支持2003,并没有支持2007及以上版本。poi 是支持目前excel的所有
版本的。只不过不同版本所使用的读取方法名不一样
也是发现网上很多的都是博文都是没有判断版本的,包括我之前借鉴的文章。
File file = new File(path);
getData(file, 0); //path是路径,
重要代码
public String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打开HSSFWorkbook
// POIFSFileSystem fs = new POIFSFileSystem(in);
// HSSFWorkbook wb = new HSSFWorkbook(fs);
// HSSFCell cell = null;
// HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
// XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
// Workbook wb = null;
int excel=0;
InputStream inp= in;
if(! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
if(POIFSFileSystem.hasPOIFSHeader(inp)) {
System.out.println("2003及以下");
excel=2;
}
if(POIXMLDocument.hasOOXMLHeader(inp)) {
System.out.println("2007及以上");
excel=3;
}
// if (!path.endsWith(".xls")) {
// excel =1;
// }
//
if(excel==2){
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
Map<Object, Object> headMap = new HashMap<Object, Object>();
inputID=0;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);// 不取0行 表头
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {// 不算标题 的行数 st.getLastRowNum()不算表头的数目
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
addressList = new AddressList();
boolean _b = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { // 循环列
cell = row.getCell(columnIndex);
String sb="";
if (cell != null&&!cell.equals("")) {
DecimalFormat df = new DecimalFormat("0");// 格式化 number String字符
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING://String
sb = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC: //数字
sb = df.format(cell.getNumericCellValue()); //将科学计数的转换
break;
case HSSFCell.CELL_TYPE_FORMULA: // 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
sb = cell.getStringCellValue();
} else {
sb = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK: //空值
break;
case HSSFCell.CELL_TYPE_ERROR: //异常类型直接空
sb = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //布尔类型
sb = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
sb = "";
}
String value=sb;
//String value = cell.toString().trim();
if (rowIndex == 0) {
if (value.toString().equals("姓名")) {
headMap.put(columnIndex, "name");
}else if (value.toString().equals("职务")) {
headMap.put(columnIndex, "zhiwu");
}
else if (value.toString().equals("办公室电话")) {
headMap.put(columnIndex, "oph");
} else if (value.toString().equals("手机号码")) {
headMap.put(columnIndex, "tel");
} else if (value.toString().equals("传真")) {
headMap.put(columnIndex, "fax");
} else if (value.toString().equals("邮箱")) {
headMap.put(columnIndex, "emall");
}
else if (value.toString().equals("生日")) {
headMap.put(columnIndex, "birthday");
}
else if (value.toString().equals("地址")) {
headMap.put(columnIndex, "address");
} else if (value.toString().equals("说明")) {
headMap.put(columnIndex, "comments");
} else if (value.toString().equals("性别")) {
headMap.put(columnIndex, "sex");
}
} else {
if (headMap.get(columnIndex) != null) {
String _value = headMap.get(columnIndex).toString();
if (_value.equals("name")) {
_b = true;
addressList.setName(value);
}else if (_value.equals("zhiwu")) {
_b = true;
String abc = jdbc.GetObject("select id from Code where codeName like '"+value+"'", "id");
if(abc==null||abc==""){
abc="0";
}
addressList.setZhiwu(Integer.parseInt(abc));
}
else if (_value.equals("oph")) {
_b = true;
addressList.setOph(value);
} else if (_value.equals("tel")) {
_b = true;
addressList.setTel(value);
} else if (_value.equals("fax")) {
_b = true;
addressList.setFax(value);
} else if (_value.equals("emall")) {
_b = true;
addressList.setEmall(value);
} else if (_value.equals("sex")) {
_b = true;
if (value.toString().equals("男")) {
value = "0";
} else {
value = "1";
}
addressList.setSex(Integer.parseInt(value));
}
else if (_value.equals("birthday")) {
_b = true;
if(value!=null&&!value.equals("")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value);
addressList.setBirthday(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
addressList.setBirthday(null);
}
}else{
addressList.setBirthday(null);
}
}
else if (_value.equals("address")) {
_b = true;
addressList.setAddress(value);
}
else if (headMap.get(columnIndex).toString()
.equals("comments")) {
_b = true;
addressList.setComments(value);
}
}
}
}
}
if (_b) {
addressList.setPid(pid);
addressList.setType(type);
addressList.setUserid(userid);
addressList.setSort(0);
addressListService.save(addressList); //
inputID++;
}
}
System.out.println("wwwwwwwwwwww "+inputID);
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}else{
FileInputStream input = new FileInputStream(new File(path)); //读取的文件路径
XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(input));
XSSFCell cell = null;
// 根据不同的data放置不同的表头
Map<Object, Object> headMap = new HashMap<Object, Object>();
inputID=0;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
XSSFSheet st = wb.getSheetAt(sheetIndex);// 不取0行 表头
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {// 不算标题 的行数 st.getLastRowNum()不算表头的数目
XSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
addressList = new AddressList();
boolean _b = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { // 循环列
cell = row.getCell(columnIndex);
String sb="";
if (cell != null&&!cell.equals("")) {
DecimalFormat df = new DecimalFormat("0");// 格式化 number String字符
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING://String
sb = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC: //数字
sb = df.format(cell.getNumericCellValue()); //将科学计数的转换
break;
case HSSFCell.CELL_TYPE_FORMULA: // 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
sb = cell.getStringCellValue();
} else {
sb = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK: //空值
break;
case HSSFCell.CELL_TYPE_ERROR: //异常类型直接空
sb = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //布尔类型
sb = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
sb = "";
}
String value=sb;
//String value = cell.toString().trim();
if (rowIndex == 0) {
if (value.toString().equals("姓名")) {
headMap.put(columnIndex, "name");
}else if (value.toString().equals("职务")) {
headMap.put(columnIndex, "zhiwu");
}
else if (value.toString().equals("办公室电话")) {
headMap.put(columnIndex, "oph");
} else if (value.toString().equals("手机号码")) {
headMap.put(columnIndex, "tel");
} else if (value.toString().equals("传真")) {
headMap.put(columnIndex, "fax");
} else if (value.toString().equals("邮箱")) {
headMap.put(columnIndex, "emall");
}
else if (value.toString().equals("生日")) {
headMap.put(columnIndex, "birthday");
}
else if (value.toString().equals("地址")) {
headMap.put(columnIndex, "address");
} else if (value.toString().equals("说明")) {
headMap.put(columnIndex, "comments");
} else if (value.toString().equals("性别")) {
headMap.put(columnIndex, "sex");
}
} else {
if (headMap.get(columnIndex) != null) {
String _value = headMap.get(columnIndex).toString();
if (_value.equals("name")) {
_b = true;
addressList.setName(value);
}else if (_value.equals("zhiwu")) {
_b = true;
String abc = jdbc.GetObject("select id from Code where codeName like '"+value+"'", "id");
if(abc==null||abc==""){
abc="0";
}
addressList.setZhiwu(Integer.parseInt(abc));
}
else if (_value.equals("oph")) {
_b = true;
addressList.setOph(value);
} else if (_value.equals("tel")) {
_b = true;
addressList.setTel(value);
} else if (_value.equals("fax")) {
_b = true;
addressList.setFax(value);
} else if (_value.equals("emall")) {
_b = true;
addressList.setEmall(value);
} else if (_value.equals("sex")) {
_b = true;
if (value.toString().equals("男")) {
value = "0";
} else {
value = "1";
}
addressList.setSex(Integer.parseInt(value));
}
else if (_value.equals("birthday")) {
_b = true;
if(value!=null&&!value.equals("")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value);
addressList.setBirthday(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
addressList.setBirthday(null);
}
}else{
addressList.setBirthday(null);
}
}
else if (_value.equals("address")) {
_b = true;
addressList.setAddress(value);
}
else if (headMap.get(columnIndex).toString()
.equals("comments")) {
_b = true;
addressList.setComments(value);
}
}
}
}
}
if (_b) {
addressList.setPid(pid);
addressList.setType(type);
addressList.setUserid(userid);
addressList.setSort(0);
addressListService.save(addressList); //
inputID++;
}
}
System.out.println("wwwwwwwwwwww "+inputID);
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
System.out.println("zzzzzzzzzzzzzzz "+inputID);
return returnArray;
}
}
/**
* 去掉字符串右边的空格
*
* @param str
* 要处理的字符串
* @return 处理后的字符串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}