private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ JFileChooser fileChooser = new JFileChooser(); int retval = fileChooser.showSaveDialog(jButton1); if (retval == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); if (file != null) { if (!file.getName().toLowerCase().endsWith(".xls")) { file = new File(file.getParentFile(), file.getName() + ".xls"); } try { Excel exp=new Excel(); exp.exportTable(jTable1, file); Desktop.getDesktop().open(file); } catch (UnsupportedEncodingException e) {} catch (FileNotFoundException e) { System.out.println("not found"); } catch (IOException e) {} } } }catch(Exception e){ System.out.println("saved new data."); } }
class Excel { public Excel() {} public void exportTable(JTable table, File file) throws IOException { TableModel model = table.getModel(); FileWriter out = new FileWriter(file); BufferedWriter bw= new BufferedWriter(out); for(int i = 0; i < model.getColumnCount(); i++) { bw.write(model.getColumnName(i) + "\t"); } bw.write("\n"); for(int i=0; i< model.getRowCount(); i++) { for(int j=0; j < model.getColumnCount(); j++) { bw.write(model.getValueAt(i,j).toString()+"\t"); } bw.write("\n"); } bw.close(); System.out.println("Writing to" +file); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)