转自
JDK版本:jdk-6u14-windows-i586.exe
POI版本:poi-bin-3.7-20101029.zip
错误信息:
java.lang.ArrayIndexOutOfBoundsException: -32735
at java.util.ArrayList.get(ArrayList.java:324)
at org.apache.poi.hssf.model.WorkbookRecordList.get(WorkbookRecordList.java:50)
at org.apache.poi.hssf.model.InternalWorkbook.getExFormatAt(InternalWorkbook.java:826)
at org.apache.poi.hssf.usermodel.HSSFCell.<init>(HSSFCell.java:198)
at org.apache.poi.hssf.usermodel.HSSFRow.createCellFromRecord(HSSFRow.java:204)
at org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet(HSSFSheet.java:184)
at org.apache.poi.hssf.usermodel.HSSFSheet.<init>(HSSFSheet.java:121)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:275)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:188)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:170)
at com.sumavision.common.util.ExcelPOI.getExcleRows(ExcelPOI.java:45)
at com.sumavision.common.util.ExcelPOI.main(ExcelPOI.java:18)
源码:ExcelPOI .java
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExcelPOI {
public static void main(String[] args) {
try {
int count = ExcelPOI.getExcleRows("D:/2011.03.14-24.xls",0);
System.out.println("count="+count);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Excle某个工作簿所有行数
*
* @param filePath
* Excel文件路径
* @param index
* 第一张工作表的索引是0
* @return int
* @throws FileNotFoundException
* @throws IOException
*/
public static int getExcleRows(String filePath, int index)
throws FileNotFoundException, IOException {
// 创建对Excel工作簿文件的引用
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filePath));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
// 在Excel文档中,第一张工作表的缺省索引是0
// 其语句为:
HSSFSheet sheet = workbook.getSheetAt(index);
// HSSFSheet sheet = workbook.getSheet("Sheet1");
// 获取到Excel文件中的所有行数
return sheet.getPhysicalNumberOfRows();
}
}