码迷,mamicode.com
首页 > 编程语言 > 详细

java poi读取excel文件

时间:2015-04-09 16:57:30      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

 

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;
}
}

java poi读取excel文件

标签:

原文地址:http://www.cnblogs.com/luoge/p/4409846.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!