如何使用java避免NoSuchMethodError Create()?

如何使用java避免NoSuchMethodError Create()?,java,apache-poi,Java,Apache Poi,我正在尝试将文本文件转换为Excel 在执行此操作时,会发生以下异常: Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.poifs.filesystem.POIFSFileSystem.hasPOIFSHeader(Ljava/io/InputStream;)Z at org.apache.poi.ss.usermodel.WorkbookFactory.create() 我怎样才能避免这个?

我正在尝试将文本文件转换为Excel

在执行此操作时,会发生以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: 
org.apache.poi.poifs.filesystem.POIFSFileSystem.hasPOIFSHeader(Ljava/io/InputStream;)Z  
at org.apache.poi.ss.usermodel.WorkbookFactory.create() 
我怎样才能避免这个??我正在提供我的代码片段

 public void convertCSV(String textfile) throws Exception {

            FileInputStream inputStr = new FileInputStream(new File("/home/directory/Desktop/HI/excel.xls"));

            Workbook HssfWork = WorkbookFactory.create(inputStr);

            Sheet sheet= HssfWork.getSheetAt(0);
            int rowNo = 0;
            int columnNo = 0;
                     Cell cell = null;

            // Loop through the files.
            //for(File file : csvFiles) {
                Scanner scanner = new Scanner(textfile);
                while(scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    // Gets the row and if it doesn't exist it will create it.

                    Row Row = sheet.getRow(rowNo);
                    if(Row == null)
                        Row = sheet.createRow(rowNo);

                    Scanner lineScanner = new Scanner(line);
                    lineScanner.useDelimiter("\t");
                    // While there is more text to get it will loop.
                    while(lineScanner.hasNext()) {
                        // Gets the cell in that row and if it is null then it will be created.

                        org.apache.poi.ss.usermodel.Cell tempCell =Row.getCell(columnNo,org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
                        String output = lineScanner.next();
                        // Write the output to that cell.
                        tempCell.setCellValue(output);
                        columnNo++;
                    }
                    // Resets the column count for the new row.
                    columnNo = 0;
                    rowNo++;
                }

            // Writes the file and closes everything.
            FileOutputStream out = new FileOutputStream(excelFile);
            HssfWork.write(out);
            inputStr.close();
            out.close();
        }

第一个猜测是:你得到了一个错误版本的罐子。我猜在您的类路径中至少有两个JAR,
WorkbookFactory
在一个JAR中,而
POIFSFISTEM
在另一个JAR中。检查第二个JAR的版本

NoSuchMethodException
的意思是:调用的方法(由
WorkbookFactory
调用)不存在。因此,如果您得到它,就意味着类
poifsffilesystem
确实存在,否则您将得到可怕的
NoClassDefFoundError
。因此,您必须拥有正确的库,但尚未添加另一个版本的库,
hasPOIFSHeader()
。。。或者已被删除…

正如Andrew在中指出的那样,您将获得该异常,因为您没有使用匹配的POI jar集。这有两个共同的原因。其中一个原因是您不知何故选择了一组不匹配的jar,例如
poi-ooxml-3.10-beta1-20130204.jar
poi-3.8-final.jar
,这是行不通的。或者,您已经在类路径上放置了一个匹配集,但是已经存在旧的JAR,并且您的类加载器选择了一个随机集


无论哪种方式,我都会建议您遵循以下步骤,找出您实际使用的POI罐。然后,继续工作,直到找到匹配集。POI与大多数应用程序一样,要求使用的所有JAR彼此来自同一版本

第二行工作簿HssfWork=WorkbookFactory.create(inputStr)出现错误;我怎样才能避免呢?给它一个完整的路径。他给文件输入流一个完整的路径。org.apache.poi.ss.usermodel.Workbook接口……我在pgm中使用过。。。。。。我需要提供任何其他信息吗?您是否用另一个excel文件进行了尝试?我遇到了同样的问题,这是因为工作表包含两张同名的工作表。你能建议我必须使用哪个版本的jar文件吗。。。。。。。。。。要执行此pgm的WorkbookFactory和POIFSFileSystem???@user2376026,请使用最新版本。我不知道您使用的是哪个版本的POI,那么我如何才能告诉您需要哪个版本的依赖项?阅读文档,或者如果这项工作太多,那么首先尝试使用@anshulkatta comment的最新版本