从JTable Java问题

时间:2015-12-15 07:08:24

标签: java excel jtable

我正在尝试将jTable输出导出到Excel, 输出文件在路径上创建但没有我的数据,我得到了Exception 我试了很多,找到了我的任何工作代码,但总是我有不同的问题。请检查下面的代码并给我一个答案

Exception in thread "AWT-EventQueue-0" java.lang.AbstractMethodError: gnu.xml.dom.DomDocument.getXmlStandalone()Z
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:377)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:131)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:699)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
at org.apache.poi.openxml4j.opc.StreamHelper.saveXmlInStream(StreamHelper.java:80)
at org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.marshallRelationshipPart(ZipPartMarshaller.java:174)
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:464)
at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1441)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:212)
at newtimesheet.ImpMyWork.jButton7ActionPerformed(ImpMyWork.java:1021)
at newtimesheet.ImpMyWork.access$1900(ImpMyWork.java:44)
at newtimesheet.ImpMyWork$17.actionPerformed(ImpMyWork.java:461)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
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 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
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$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我正在使用带有以下代码的Apache POI-3.13

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    TableModel model = jTable1.getModel();
    for (int i = 0; i < model.getColumnCount(); i++) {
        row.createCell(i).setCellValue(model.getColumnName(i));
    }
    for (int i = 0; i < model.getRowCount(); i++) {
        row = sheet.createRow(i + 1);
        for (int j = 0; j < model.getColumnCount(); j++) {
            row.createCell(j).setCellValue(
                    model.getValueAt(i, j).toString()
            );
        }
    }
    FileOutputStream fileOut;
    try {
        fileOut = new FileOutputStream("D:\\filename.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ImpMyWork.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ImpMyWork.class.getName()).log(Level.SEVERE, null, ex);
    }

}

1 个答案:

答案 0 :(得分:0)

我在这里有两个问题

第一期我找到了这个答案

1-当您运行它时从类路径中编译代码时,在类路径上使用不同的JAR会导致某种配置问题。您可以在此链接中查看它

exception creating xml file

我使用了一些与Apache POI-3.13库冲突的java流程图库,所以我删除它并且它已经工作了

2-你应该替换这些代码行

  row.createCell(j).setCellValue(
  model.getValueAt(i, j).toString()

通过

   if (model.getValueAt(i, j) != null) {
   val = model.getValueAt(i, j).toString();
   } else {
  val = "NULL"; //you can change value to any thing you want to represent null values
          }
   row.createCell(j).setCellValue(val);

避免空指针异常错误

那是因为如果在数据库中允许空值,则会出现空指针异常