【问题标题】:HSSFWorkbook error shown in code代码中显示的 HSSFWorkbook 错误
【发布时间】:2014-07-23 07:57:06
【问题描述】:
在创建从excel读取数据的代码时,对于代码
HSSFWorkbook wb = new HSSFWorkbook(input);
在 Eclipse 中显示输入错误。显示的错误是“输入无法解析为类型”。我已经读到,通过这个声明,我们正在创建一个 excel 实例。但是我们应该为这一步提供什么输入?
【问题讨论】:
标签:
java
testing
selenium
selenium-webdriver
automation
【解决方案1】:
请查看 POI busy 开发者指南,有很多例子:http://poi.apache.org/spreadsheet/quick-guide.html
您需要 InputStream、OPCPackage 或 NPOIFSFileSytem 才能读取(这是 NPOIFSFileSytem 的示例):
// HSSFWorkbook, File
NPOIFSFileSytem fs = new NPOIFSFileSystem(new File("file.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot());
....
fs.close();
【解决方案2】:
需要使用inputstream提供excel文件的路径,
FileInputStream input= null;
input= new FileInputStream(new File(path));
HSSFWorkbook wb = new HSSFWorkbook(input);
【解决方案3】:
输入应该是类似 FileInputStream 的
String fileName="/home/hduser/file.xls";
FileInputStream file = new FileInputStream( new File( fileName ) );
String fileExtension = FilenameUtils.getExtension( fileName );
Workbook channelBook = getChannelWorkBook( fileExtension, file );
/**
* Get Workbook from extension
*/
private Workbook getChannelWorkBook( String fileExtension, FileInputStream file ) throws IOException
{
if ( fileExtension.equalsIgnoreCase( "XLS" ) )
return new HSSFWorkbook( file );
if ( fileExtension.equalsIgnoreCase( "XLSX" ) )
return new XSSFWorkbook( file );
return null;
}