在使用將表格數據導出到EXCEL文件的時候不知所錯了,也不知道該怎麽下手 
在網上看見有這麽一段代碼可以將TABLE裏面的數據導出到用戶指定的文件裏面,可是在 操作的時候不知道下面一个 TableUtil.sendToCSVFile(tableModel.getRowsWithHeaders(), f);這一段應該就是主要得到TBALE裏面的數據料。 只是我的數據表格繪製很簡單就使用TABLE的table.setModel(new javax.swing.table.DefaultTableModel(數據,表頭));這樣得到的數據。如果要換到下面進行導出,該怎麽做呢?還請高手指點一二,,,      // open a JFileChooser, and let 
    // the user choose where to save 
    // the file to. 
    JFileChooser chooser = new JFileChooser(); 
    chooser.setFileSelectionMode(chooser.FILES_ONLY); 
    chooser.setMultiSelectionEnabled(false);     ExtensionFilter extFilter = new ExtensionFilter(); 
    extFilter.addExtension(".csv"); 
    chooser.setFileFilter(extFilter); 
    if (lastFileChosen != null) 
    { 
      log.writeDebug(3, "Setting last dir to " 
          + lastFileChosen.getAbsolutePath()); 
      chooser.setCurrentDirectory(lastFileChosen); 
    } 
    int returnVal = chooser.showSaveDialog(this); 
    if (returnVal == chooser.APPROVE_OPTION) 
    { 
      String file = chooser.getSelectedFile().getAbsolutePath(); 
      if (!file.endsWith(".csv")) 
        file = file + ".csv";       log.writeDebug("File chooser choose: " + file);       File newFile = null; 
      try 
      { 
        File f = new File(file); 
        lastFileChosen = f; 
        newFile = TableUtil.sendToCSVFile(tableModel 
            .getRowsWithHeaders(), f); 
      } catch (IOException ex) 
      { 
        log.printStackTrace(ex);         String message = "An error occured saving this file. Make sure \n" 
            + "that the file chosen is writable.";         // MessageWindow.showWarning(this, 
        // message, ex); 
        ExceptionDebugger.debug(this, message, ex);         return; 
      }       Environment env = Environment.getEnvironment(); 
      if (env.isWintel()) 
      { 
        String sendToSystemMessage = "On windows systems that are properly configured, it will \n" 
            + "be possible to send the file just saved to the system, and \n" 
            + "it will be opened by any spreadsheet registered with " 
            + "the system.\n\n" 
            + "Would you like to send this file to the system now?"; 
        int sendToSystem = JOptionPane.showConfirmDialog(this, 
            sendToSystemMessage, "Send to System?", 
            JOptionPane.YES_NO_OPTION);         if (sendToSystem == JOptionPane.YES_OPTION) 
        { 
          // Send the url to 
          // the system with 
          // start. 
          env.sendToSystem(newFile.getAbsolutePath()); 
        } 
      } else 
      { 
        // The file has been 
        // saved, we're done. 
        String message = "Data successfully saved to: \n" 
            + newFile.getAbsolutePath();         JOptionPane.showConfirmDialog(this, message, 
            "Save successful.", JOptionPane.OK_OPTION); 
      } 
    } 

解决方案 »

  1.   

    TableUtil.sendToCSVFile(tableModel.getRowsWithHeaders(), f);  what's the tableModel?
    and what's the getRowsWithHeaders method to return?
    貌似TableUtil的作者没有使用TableModel接口编程答案是你无法使用这段代码达到你的目的
    除非你了解到tableModel.getRowsWithHeaders()在sendToCSVFile的含义
      

  2.   

    tableModel是以AbstractTableModel為接口返回的是ArrayList數組形式
    如果程序裡面不使用newFile = TableUtil.sendToCSVFile(tableModel.getRowsWithHeaders(), f); 
    這裡面的TableUtil.sendToCSVFile(tableModel.getRowsWithHeaders(), f);這段代碼? 
    要實現把table裡面的數據給導出來?該怎麼實現呢? ? ? 
    newFile能否直接去得到Table裡面的數據? ? ? ? 
    可否可以?
      

  3.   

    查了下swing实现TableModel(1.6)的类并没有提供getRowsWithHeaders()你看到的是不是AbstractTableModel tableModel = ....我们要知道的是他的具体实现你跟踪下tableModel.getRowsWithHeaders()的实现然后以次作为参考把自己的TableModel里的具体数据也这样处理;再将结果作为参数运行TableUtil.sendToCSVFile
      

  4.   

    具體看了一下. 
    裡面都是以數組方式去取Headers與Row,重新在試試,希望能成功
      

  5.   


    你随便传一个new String[]{"foo","bar"}进去 看看啥效果
      

  6.   

    具體還是可以的. 
    現在主要的問題是,在NEWFILE的時候怎麼導出JTABLE裡面的數據到CSV文件裡面? 
    有這方面的例子嗎? 
    有提供點謝謝了, 
    thanks
      

  7.   

    newFile = TableUtil.sendToCSVFile(tableModel  
                .getRowsWithHeaders(), f);  不知道返回newFile是啥意思
    看method name的意思 应该是把这些数据写入f的意思
    其实csv格式很简单的 你自己写个好了 不需要用他的方法==我看下
      

  8.   

    public void toCSV(TableModel model,OutputStream out) throws IOException{

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));

    for(int i=0;i<model.getColumnCount();i++){
    writer.write(model.getColumnName(i));
    writer.write(",");
    }

    writer.newLine();

    for(int row=0;row<model.getRowCount();row++){
    for(int i=0;i<model.getColumnCount();i++){
    writer.write(model.getValueAt(row, i).toString());
    writer.write(",");
    }
    writer.newLine();
    }


    writer.flush();
    writer.close();

    }
      

  9.   

    newFile是將TBALE裡面的數據寫入到文件裡面. 
    我使用JXL將文件直接輸入過.什麼都沒有提示.現在只是想把他用以提示是方式讓用戶直接選擇輸出路徑. 
    File只要得到tablel裡面的數據就可以直接輸出了. 
    table獲取數據方式我是使用
    setModel(new javax.swing.table.DefaultTableModel(數據行,表頭)); 
    數據行是直接rs.next() 
    得到一行數據
    如將這個表格給導出數據為文件csv?
      

  10.   


    // open a JFileChooser, and let  
        // the user choose where to save  
        // the file to.  
        JFileChooser chooser = new JFileChooser();  
        chooser.setFileSelectionMode(chooser.FILES_ONLY);  
        chooser.setMultiSelectionEnabled(false);      ExtensionFilter extFilter = new ExtensionFilter();  
        extFilter.addExtension(".csv");  
        chooser.setFileFilter(extFilter);  
        if (lastFileChosen != null)  
        {  
          log.writeDebug(3, "Setting last dir to "  
              + lastFileChosen.getAbsolutePath());  
          chooser.setCurrentDirectory(lastFileChosen);  
        }  
        int returnVal = chooser.showSaveDialog(this);  
        if (returnVal == chooser.APPROVE_OPTION)  
        {  
          String file = chooser.getSelectedFile().getAbsolutePath();  
          if (!file.endsWith(".csv"))  
            file = file + ".csv";        log.writeDebug("File chooser choose: " + file);        //File newFile = null;  
          try  
          {  
            File f = new File(file);  
            lastFileChosen = f;  
            toCSV(tableModel,new FileOutputStream(f));
            //newFile = TableUtil.sendToCSVFile(tableModel  
              //  .getRowsWithHeaders(), f);  
          } catch (IOException ex)  
          {  
            log.printStackTrace(ex);          String message = "An error occured saving this file. Make sure \n"  
                + "that the file chosen is writable.";          // MessageWindow.showWarning(this,  
            // message, ex);  
            ExceptionDebugger.debug(this, message, ex);          return;  
          }        Environment env = Environment.getEnvironment();  
          if (env.isWintel())  
          {  
            String sendToSystemMessage = "On windows systems that are properly configured, it will \n"  
                + "be possible to send the file just saved to the system, and \n"  
                + "it will be opened by any spreadsheet registered with "  
                + "the system.\n\n"  
                + "Would you like to send this file to the system now?";  
            int sendToSystem = JOptionPane.showConfirmDialog(this,  
                sendToSystemMessage, "Send to System?",  
                JOptionPane.YES_NO_OPTION);          if (sendToSystem == JOptionPane.YES_OPTION)  
            {  
              // Send the url to  
              // the system with  
              // start.  
              env.sendToSystem(newFile.getAbsolutePath());  
            }  
          } else  
          {  
            // The file has been  
            // saved, we 're done.  
            String message = "Data successfully saved to: \n"  
                + newFile.getAbsolutePath();          JOptionPane.showConfirmDialog(this, message,  
                "Save successful.", JOptionPane.OK_OPTION);  
          }  
        }  你试试吧我没试过 呵呵