I created a program that generates an excel spreadsheet (.xls), then asks if the user wants to open it immediately (if so, it uses java.awt.Desktops open() command to do so). This is working fine in windows xp, but when I tried with windows 7 it didn't work. Below is a sample of my code...
Desktop myDesk = null;
//if printed to file successfully and java.awt.Desktop is supported
if (printed && Desktop.isDesktopSupported())
{
myDesk = Desktop.getDesktop();
if (myDesk.isSupported(Desktop.Action.OPEN))
{
//ask to open file
int openFile = JOptionPane.showConfirmDialog(null, "File successfully
created.\nWould you like the excel file to open?",
"open file?", JOptionPane.YES_NO_OPTION);
//try to open file
if (openFile == JOptionPane.YES_OPTION)
{
try { myDesk.open(myFile); }
catch (IOException e){ JOptionPane.showMessageDialog(null, "Problem
opening file automatically, please open it
yourself.", "Error", JOptionPane.ERROR_MESSAGE); }
}
}
}
On windows 7 this successfully prints to file, it shows the openFile dialogue, then shows the error dialogue. This shouldn't happen, as in order to get to the openFile dialogue Desktop and Desktop.open() should both be supported. It could possibly have something to do with trying to open a ".xls" file instead of ".xlsx" file, but exce开发者_开发知识库l should still be set as default for either file type...
So any ideas about why this is happening? And either how to fix it or if there's an alternate way to open a file that works better universally?
This sounds like a standard Vista/7 UAC problem. You might want to try turning of User Account Control (UAC) in Control Panel->User Accounts->Turn User Account Control on or off.
精彩评论