Java如何读取由txt改后缀成的xls或xlsx

起床搬砖 起床搬砖 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是我读取一个Excel的代码,但是这个路径下的Excel是由txt改后缀成xls或xlsx的Excel文件然后就报这个错:org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x006900680053FEFF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
各位大神有这种经历吗?给点demo或者教教我怎么读取这类的Excel文件,谢谢了。

public static Workbook readExcel(String filePath) {
Workbook wb = null;
if (filePath == null) {
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
return wb = new XSSFWorkbook(is);
} else {
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
起床搬砖
Java SE 62,621 成员 307,251 内容