0

我正在尝试使用 Apache-poi-3.9 在 XLS 中创建下拉列表。以下代码我写了::

public class TestMacroTemplate {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String args[]) throws FileNotFoundException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Data Validation");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DVConstraint dvConstraint = DVConstraint
                .createExplicitListConstraint(new String[] { "10", "20", "30" });
        DataValidation dataValidation = new HSSFDataValidation(addressList,
                dvConstraint);
        dataValidation.setSuppressDropDownArrow(false);
        sheet.addValidationData(dataValidation);
        FileOutputStream fileOut = new FileOutputStream("XLCellDropDown.xls");
        try {
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

但它给出了以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.addValidationData(Lorg/apache/poi/ss/usermodel/DataValidation;)V
    at ejb.TestMacroTemplate.main(TestMacroTemplate.java:31)

相同的代码适用于 Apache-poi-3.2

请帮我。

谢谢,尼拉夫

4

2 回答 2

3

Apache POI 有一个关于这个问题的常见问题解答。我会从那里引用,因为它会解决你的问题

我的代码使用了一些新功能,可以正常编译,但在出现“MethodNotFoundException”、“NoSuchMethodError”或“IncompatibleClassChangeError”时会失败

您几乎可以肯定在您的类路径中有旧版本的 POI。相当多的运行时和其他软件包将发布旧版本的 POI,因此这是一个很容易在您没有意识到的情况下遇到的问题。

识别有问题的早期 jar 文件的最佳方法是使用几行 java.util.jar 文件。这些将加载核心 POI 类之一,并报告它的来源。

ClassLoader classloader =
   org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
         "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("Core POI came from " + path);
于 2013-03-18T10:25:55.350 回答
1

它工作正常Apache poi 3.9,我已经测试过了。只包括这些罐子

poi-scratchpad-3.9-20121203.jar
poi-3.9-20121203.jar
poi-examples-3.9-20121203.jar
poi-excelant-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
于 2013-03-18T09:47:09.530 回答