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);
}
}
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.