3

I have created an Excel file from my JTable. The same button that creates it also opens it. But it gives a prompt :

The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted user before opening the file. Do you want to open the file?

When I click OK the file opens. I am using Office 2007 and am saving the file as .xls. Is there a way of stopping the prompt from the code level? Here is the code that creates and opens the file:

if (obj == btnExport) {
  File f = new File("Student Results");
  f.mkdir();
  try {
    TableModel model = DataBaseTable.getModel();
    FileWriter excel = new FileWriter("Student Results/" + exam  + "_"+ level + "_" + year + ".xls");
    for(int i = 0; i < model.getColumnCount(); i++){
      excel.write(model.getColumnName(i) + "\t");
    }

    excel.write("\n");

    for(int i=0; i< model.getRowCount(); i++) {
      for(int j=0; j < model.getColumnCount(); j++) {
        excel.write(model.getValueAt(i,j).toString()+"\t");
      }
      excel.write("\n");
    }

    excel.close();
    JOptionPane.showMessageDialog(this, "File Exported to " + f.getAbsolutePath(),
    "Success", JOptionPane.INFORMATION_MESSAGE);
    File opn = new File("Student Results/" + exam  + "_"+ level + "_" + year + ".xls");
    Desktop.getDesktop().open(opn);

  } catch (Exception se) {
    JOptionPane.showMessageDialog(this, se,
    "Error", JOptionPane.ERROR_MESSAGE);
  }
}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • That Does not have Divisions and the date requires to be separated into Columns and rows – Stanley Mungai Oct 25 '12 at 14:15
  • Can you explain your last comment in more detail? I don't understand it. (*That Does not have Divisions and the date requires to be separated into Columns and rows*) – Duncan Jones Oct 25 '12 at 14:17
  • 1
    Ok I had not seen the Part for substituting comma for `\t`. Yep it worked fine Thanks Daniel Cook. GIVE IT AS ANSWER SO I CAN ACCEPT – Stanley Mungai Oct 25 '12 at 14:20

1 Answers1

3

This MSDN gives a couple methods (group policy, or registry manipulation) for how you could suppress the warning, but I don't think either are particularly practical.

I would recommend making a CSV instead, since you are just writing a file. Those are also opened by Excel. I believe all you'd have to do is subsituted a comma for both of your \ts in your code.

Daniel
  • 12,982
  • 3
  • 36
  • 60