poi不能读取excel

rvenric rvenric 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的代码是这样
	
public static void main(String[] args) throws Exception{

String excelText = TestPoi.extractTextFromXLS("D:\\desk\\company\\文档\\AAAA.xls");
System.out.println(excelText);
}


private static String extractTextFromXLS(String path)
throws IOException,Exception {

StringBuffer content = new StringBuffer();
try{
InputStream is = new FileInputStream(path);
HSSFWorkbook workbook = new HSSFWorkbook(is); //创建对Excel工作簿文件的引用

for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); //获得一个sheet

for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一行

for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); //获得列值

if(aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
content.append(aCell.getNumericCellValue());
}else if(aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
content.append(aCell.getBooleanCellValue());
}else {
content.append(aCell.getStringCellValue());
}
}
}
}
}
}
}
}catch(org.apache.poi.poifs.filesystem.OfficeXmlFileException e){
content.append(extractTextFromXLS2007(path));
}catch(Exception e){
e.printStackTrace();
}

return content.toString();
}

但是workbook.getNumberOfSheets(); 这里就出错了,也就是HSSFWorkbook workbook = new HSSFWorkbook(is); 这里出不数据来,我把AAAA.xls这个文档另存为另一个文档或者随便修改一点东西,然后程序又正确了,这个是怎么回事啊。我有很多文档啊,不能一个个的另存为啊。
...全文
评论 12
收藏 已收藏
分享
返回顶部
rvenric
Web 开发 81,108 成员 341,721 内容