Apache POI:打开模板,写,另存为.崩溃并创建损坏的文件



我一直在编写一个程序来在花名册上编写名称,并编写了查找和排序的大多数方法。该方法的目的是打开现有文件,在单元格中写入并保存为其他文件。我认为问题可能是模板文件,因为它由2张纸组成。一张纸是收集所有名称,第二张纸具有带有文本框的图像,链接到第一页,以打印到文本框

public static void FindTemplate(String Session) throws FileNotFoundException, IOException
{
    if(Session.toLowerCase().contains("Level 1".toLowerCase())) 
         // generic roster as an else
    {
        FileInputStream In = new FileInputStream("Directory\Templates\A Template.xls");
        HSSFWorkbook wb = new HSSFWorkbook(In);
        HSSFSheet sheet = wb.getSheetAt(0);
        Cell cell = null; 
        cell = sheet.getRow(0).getCell(0);

        cell.setCellValue("Found it");//just as a test for now
        In.close();

        wb.write(new FileOutputStream("WA1.xls"));
        wb.close();

    }
}

当我运行时,我会发现一个我不明白的大错误。错误是在wb.write(new fileOutputStream(" wa1.xls"));我以前在其他方法中保存过这样的文件。还创建了一个新文件,但已损坏。

Error:
 WAException in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/bidimap/TreeBidiMap
at org.apache.poi.hpsf.Section.<init>(Section.java:178)
at org.apache.poi.hpsf.MutableSection.<init>(MutableSection.java:41)
at org.apache.poi.hpsf.PropertySet.init(PropertySet.java:494)
at org.apache.poi.hpsf.PropertySet.<init>(PropertySet.java:196)
at org.apache.poi.hpsf.MutablePropertySet.<init>(MutablePropertySet.java:44)
at org.apache.poi.hpsf.SpecialPropertySet.<init>(SpecialPropertySet.java:47)
at org.apache.poi.hpsf.DocumentSummaryInformation.<init>(DocumentSummaryInformation.java:99)
at org.apache.poi.hpsf.PropertySetFactory.create(PropertySetFactory.java:116)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:236)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:197)
at org.apache.poi.POIDocument.readPropertySet(POIDocument.java:175)
at org.apache.poi.POIDocument.readProperties(POIDocument.java:158)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.updateEncryptionInfo(HSSFWorkbook.java:2295)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1506)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1428)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1414)
at rosterWrite.FindTemplate(rosterWrite.java:79)
at rosterWrite.main(rosterWrite.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.bidimap.TreeBidiMap
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more

错误消息是Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.bidimap.TreeBidiMap,这意味着您的项目缺乏Apache Commons-Collections库的TreeBidiMap类。如果使用Maven,只需在此页面显示的POM.xml中添加Commons-Collection库即可。如果没有,您需要从官方网站下载图书馆并将其放入您的项目。

最新更新