0

Could you tell me how can I load JTable from .txt or .xls file? I used the algoriithm below to save JTable content as .xls:

public void saveTable()throws Exception
{

    BufferedWriter bfw = new BufferedWriter(new FileWriter("Data.xls"));
  for(int i = 0 ; i < jTable1.getColumnCount() ; i++)
  {
    bfw.write(jTable1.getColumnName(i));
    bfw.write("\t");
  }

  for (int i = 0 ; i < jTable1.getRowCount(); i++)
  {
    bfw.newLine();
    for(int j = 0 ; j < jTable1.getColumnCount();j++)
    {
      bfw.write((String)(jTable1.getValueAt(i,j)));
      bfw.write("\t");;
    }
  }
  bfw.close();
Unheilig
  • 16,196
  • 193
  • 68
  • 98
KumarHero
  • 3
  • 1
  • 1

1 Answers1

0
  1. Read the first line from the file into a String variable.
  2. Use the String.split(...) method to split the string into an Array containing the column names.
  3. Create a DefaultTableModel using the new DefaultTableModel(columnName, 0) constructor.
  4. Read the next line in the file into a String variable
  5. Use the String.split(...) method to split the string into an Array containing the data for each column
  6. Use the addRow(...) method of the DefaultTableModel to add the Array from step 5 to the TableModel
  7. Repeat steps 4-6 until all the records have been read.
camickr
  • 321,443
  • 19
  • 166
  • 288