Java JTable 表格 擷取儲存路徑,檔案名稱 ,匯出excel表格

來源:互聯網
上載者:User

標籤:

在做計量泵上位機軟體時,需要將下位機傳上來的資料,存入MYSQL資料庫,顯示在java 上位機介面上,並能匯出至電腦指定位置。 

 

選擇儲存路徑和檔案名稱:

    // 處理另存檔案的菜單       public void ExportExcelFile(String Sql,String []Paras) {                    String AbsolutePath="";           // 設定對話方塊的風格        try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (Exception e1) {         // TODO Auto-generated catch block         e1.printStackTrace();        }        JFileChooser jFileChooser = new JFileChooser();      //   jFileChooser.setMultiSelectionEnabled(true);//如果要多選的話,設定這句話即可        // 設定預設的儲存檔案名稱,注意如果這樣設定了之後,後面就無法取得使用者改變之後的檔案儲存體路徑了,因為後面調用的 jFileChooser.getCurrentDirectory()擷取的路//徑就是你設定的預設檔案的路徑        // jFileChooser.setSelectedFile(new        // File("預設檔案名稱"));        int result = jFileChooser.showSaveDialog(null);              switch (result) {        case JFileChooser.APPROVE_OPTION:            System.out.println("11");           // 這一種方法是把使用者輸入的作為儲存的檔案名稱  儲存為excel表格  .xls          AbsolutePath = jFileChooser.getCurrentDirectory() + File.separator + jFileChooser.getSelectedFile().getName()               + ".xls";  //.xls 為輸出excel表格 也可以指定其他格式           SqlHelper sqlHelper=new SqlHelper();           ResultSet rs=sqlHelper.query(Sql, Paras);                               File file=new File(AbsolutePath);            try {                this.writeExcel(rs, file); //匯出資料方法            } catch (SQLException e1) {                    e1.printStackTrace();            }                        JOptionPane.showMessageDialog(null, "匯出資料成功!");                      break;        case JFileChooser.CANCEL_OPTION:                       JOptionPane.showMessageDialog(null, "取消匯出!!");          break;        case JFileChooser.ERROR_OPTION:                    //System.out.println("出錯啦!!");            break;        }                }
//匯出資料方法:
   //需要一個結果集合rs 和 file檔案
public void writeExcel(ResultSet rs,File file) throws SQLException{ WritableWorkbook workbook = null; //從rs對象中可以得到ResultSetMetaData ,從而可以得到表的列名 ResultSetMetaData rsmt=rs.getMetaData(); try{ //首先要使用Workbook類的Factory 方法建立一個可寫入的工作薄(Workbook)對象 workbook = Workbook.createWorkbook(file); } catch (IOException e){ e.printStackTrace(); } if(workbook!=null){ WritableSheet wsheet = workbook.createSheet("sheet1", 0); try { //獲得表的列名 for(int i=0;i<rsmt.getColumnCount();i++) { Label label_column1 = new Label(i, 0, rsmt.getColumnName(i+1)); wsheet.addCell(label_column1); } } catch (RowsExceededException e1) { e1.printStackTrace(); } catch (WriteException e1) { e1.printStackTrace(); } //獲得表的內容 int i=1; while(rs.next()){ for(int j=0;j<rsmt.getColumnCount();j++) { Label label1 = new Label(j, i, rs.getString(j+1)); try { wsheet.addCell(label1); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } i++; } try { //從記憶體中寫入檔案中 workbook.write(); //關閉資源,釋放記憶體 workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e){ e.printStackTrace(); } } }

 

 

 

Java JTable 表格 擷取儲存路徑,檔案名稱 ,匯出excel表格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.