问题描述
我创建了一个程序,该程序会生成一个excel电子表格(.xls),然后询问用户是否要立即打开它(如果是,则使用java.awt.Desktops open()命令这样做).这在Windows XP中工作正常,但是当我尝试使用Windows 7时却无法正常工作.下面是我的代码示例...
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); }
}
}
}
在Windows 7上,这将成功打印到文件,它显示openFile对话框,然后显示错误对话框.这不应该发生,因为要进入openFile对话框,应该同时支持Desktop和Desktop.open().可能与尝试打开".xls"文件而不是".xlsx"文件有关,但对于每种文件类型,仍应将excel设置为默认值...
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 excel 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?
推荐答案
这听起来像是标准的Vista/7 UAC问题.您可能想尝试在控制面板"->用户帐户"->打开或关闭用户帐户控制"中打开用户帐户控制(UAC)".
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.
这篇关于在Java中使用Desktop打开文件时出错(Windows 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!