使用GUI写入xls excel文件

时间:2017-04-13 17:11:06

标签: java excel

我是一名新的java学生,我正在创建一个项目,我想使用 Jtextfields 中的一些信息写入 xls excel文件em>文件流。但是,我一直收到这个错误。每当我运行程序并尝试写入excel时,它会将excel格式更改为t

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: 
org/apache/commons/collections4/bidimap/TreeBidiMap
at org.apache.poi.hpsf.Section.<init>(Section.java:174)
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.PropertySetFactory.create(PropertySetFactory.java:90)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:225)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:181)
at org.apache.poi.POIDocument.readProperties(POIDocument.java:150)
at org.apache.poi.POIDocument.getSummaryInformation(POIDocument.java:117)
at org.apache.poi.POIDocument.writeProperties(POIDocument.java:274)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1456)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1439)
at Excel.addStudent(Excel.java:109)
at addS$2.actionPerformed(addS.java:180)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at 
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at 
vax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at 
.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Prot
at 
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at 
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThrea
at 
java.awt.EventDispatchThread.pumpEventsForFilter
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: 
org.apache.commons.collections4.bidimap.TreeBidiMap
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 50 more

以下是我的一些代码:

 public Excel() {
    FileName = FILE_NAME;
}

public Excel(String Filepath) throws FileNotFoundException, IOException{
    FileName = Filepath;

    try{
        POIFSFileSystem fs = new POIFSFileSystem(new 
 FileInputStream(FILE_NAME));
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workbook.getSheetAt(0);   
    }
    catch (Exception e1) {
        System.out.println(e1.getMessage());
    }
}

public void addStudent(Student S1) {
    String name = S1.getName();
    int age = S1.getAge();
    String dob = S1.getDOB();
    String gender = S1.getGender();
    String password = S1.getPassword();
    int grade = S1.getGradeLevel();
    int ID = S1.getID();
    Alist assignments = S1.getAssignemnts();


 try {
        POIFSFileSystem fs = new POIFSFileSystem(new 
 FileInputStream(FILE_NAME));
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workbook.getSheetAt(0);
        // Create Heading of Excel
        Row rowHeading = sheet.createRow(0);
        rowHeading.createCell(0).setCellValue("Name");
        rowHeading.createCell(1).setCellValue("Age");
        rowHeading.createCell(2).setCellValue("Date of Birth");
        rowHeading.createCell(3).setCellValue("Gender");
        rowHeading.createCell(4).setCellValue("Password");
        rowHeading.createCell(5).setCellValue("Grade Level");
        rowHeading.createCell(6).setCellValue("ID");
        rowHeading.createCell(7).setCellValue("Assignments: ");

        Row rowHeading1 = sheet.createRow(ID);
        rowHeading1.createCell(0).setCellValue(name);
        rowHeading1.createCell(1).setCellValue(age);
        rowHeading1.createCell(2).setCellValue(dob);
        rowHeading1.createCell(3).setCellValue(gender);
        rowHeading1.createCell(4).setCellValue(password);
        rowHeading1.createCell(5).setCellValue(grade);
        rowHeading1.createCell(6).setCellValue(ID);


        for(int i=0; i<7; i++){
            CellStyle stylerowHeading = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setBold(true);
            font.setFontName(HSSFFont.FONT_ARIAL);
            font.setFontHeightInPoints((short) 11);
            stylerowHeading.setFont(font);
            stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
            rowHeading.getCell(i).setCellStyle(stylerowHeading);
        }




        // Autofit
        for (int i=0; i<6; i++)
            sheet.autoSizeColumn(i);

        // Save to Excel file
        FileOutputStream out = new FileOutputStream(FILE_NAME);
        workbook.write(out);
        out.close();
        workbook.close();
        System.out.println("Excel written successfully..");

    } catch (Exception e1) {
        System.out.println(e1.getMessage());
    }
}

0 个答案:

没有答案