近期项目需要用到数据导入,虽然之前用过,但是并不是很熟悉,而且使用的是jxl.jar读取的,可是jxl.jar貌似不能读取高版本的xlsx文件,所以这次采用了poi来读取,读取其实很简单,只要遵循其读取规则便可读取指定的excel文件,下面是其用到的jar包:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
上面几个jar包缺一不可,有时候会出现ClassNotFound之类的错误,是因为jar缺少或不兼容的原因,上面jar包已经过测试,可以使用。
读取excel文件代码如下:
public class ExcelUtils {
// 对外提供读取excel文件的接口
public static List<List<Object>> readExcel(File file) throws IOException {
String fName = file.getName();
String extension = fName.lastIndexOf(".") == -1 ? "" : fName
.substring(fName.lastIndexOf(".") + 1);
if ("xls".equals(extension)) {// 2003
System.err.println("读取excel2003文件内容");
return read2003Excel(file);
} else if ("xlsx".equals(extension)) {// 2007
System.err.println("读取excel2007文件内容");
return read2007Excel(file);
} else {
throw new IOException("不支持的文件类型:" + extension);
}
}
/**
* 读取2003excel
*
* @param file
* @return
*/
private static List<List<Object>> read2003Excel(File file)
throws IOException {
List<List<Object>> dataList = new ArrayList<List<Object>>();
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
Object val = null;
DecimalFormat df = new DecimalFormat("0");// 格式化数字
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
for (int i = sheet.getFirstRowNum(); i < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> objList = new ArrayList<Object>();
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
val = null;
objList.add(val);
continue;
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
val = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
val = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
val = df.format(cell.getNumericCellValue());
} else {
val = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
val = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
val = "";
break;
default:
val = cell.toString();
break;
}
objList.add(val);
}
dataList.add(objList);
}
return dataList;
}
/**
* 读取excel表头
*
* @param file
* @return
* @throws IOException
*/
public static String[] readExcelHead(File file) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.getRow(0);
String[] buff = new String[row.getLastCellNum()];
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
cell = row.getCell(i);
buff[i] = cell.getStringCellValue();
}
return buff;
}
/**
* 读取2007excel
*
* @param file
* @return
*/
private static List<List<Object>> read2007Excel(File file)
throws IOException {
List<List<Object>> dataList = new ArrayList<List<Object>>();
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;
Object val = null;
DecimalFormat df = new DecimalFormat("0");// 格式化数字
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
for (int i = sheet.getFirstRowNum(); i < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> objList = new ArrayList<Object>();
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
val = null;
objList.add(val);
continue;
}
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
val = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
val = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
val = df.format(cell.getNumericCellValue());
} else {
val = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
val = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
val = "";
break;
default:
val = cell.toString();
break;
}
objList.add(val);
}
dataList.add(objList);
}
return dataList;
}
}