public void readExcel2003()
{
// 先获取文件的流
FileInputStream fis = null;
try
{
String filePath = System.getProperty("user.dir")+File.separator+"config"+File.separator+"2003.xls";
File file = new File("FilePath");
if (!file.exists())
{
// 当文件不存在,则直接返回null,并记录日志
loger.error("The file is not exist!");
return ;
}
fis = new FileInputStream(filePath);
// 得到EXCEL文件的workbook
HSSFWorkbook workbook = new HSSFWorkbook(fis);
// 得到EXCEL的sheet页,默认在第1个sheet页中
HSSFSheet sheet = workbook.getSheetAt(0);
// 得到sheet页中的总行数
int rowNum = sheet.getLastRowNum();
if (rowNum <= 0)
{
// 由于第一行是标题行,当sheet中的行数小于等于1的时候,表示该sheet中没有数据信息
loger.error("The file has no source!");
return ;
}
HSSFRow row = null; //行
HSSFCell cell = null; // 列
String value = "";
// 因为第一行是标题,所以从第二行开始循环获取信息
for (int i = 1; i <= rowNum; i++)
{
// 获得每一行数据
row = sheet.getRow(i);
if (null == row)
{
// 当此行为null的时候继续下一轮循环
continue;
}
cell = row.getCell(0); //得到第1列
if (null == cell)
{
loger.info("第 " + i + " 行第一列没有数据!");
// 当第一列为空就需要进入下一轮循环
continue;
}
else
{
value = cell.getStringCellValue().trim();
if (null == value || value.isEmpty())
{
// 当第一列中没有数据就需要进入下一轮循环
continue;
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != fis)
{
try
{
//最后关闭FileInputStream资源
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}