代码中显示HSSFWorkbook错误

时间:2014-07-23 07:57:06

标签: java testing selenium selenium-webdriver automation

创建用于从excel读取数据的代码时,代码

HSSFWorkbook wb = new HSSFWorkbook(input);

在eclipse中显示输入错误。显示的错误是"输入无法解析为类型。"我已经读过这个声明,我们正在创建一个excel实例。但是我们应该为这一步提供什么作为输入呢?

3 个答案:

答案 0 :(得分:1)

请查看POI繁忙的开发人员指南,有很多例子: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();

答案 1 :(得分:0)

您需要使用inputstream

提供excel文件的路径
FileInputStream input= null;
input= new FileInputStream(new File(path));
HSSFWorkbook wb = new HSSFWorkbook(input);

答案 2 :(得分:0)

输入应该像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;
}
相关问题