我正在使用swing设计一个表单。如果在我的表单中单击了报表按钮,它必须从我的D盘打开预定义格式的excel表。什么代码必须在按钮动作监听器中写?
谢谢..
答案 0 :(得分:4)
我建议在awt中查看Desktop类。有一种方法here可以帮助你。
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class OpenFile {
public static void main(String[] args) {
File file = new File("c:\\appNominalJournalFix.txt");
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
您可以使用Runtime
类使用Windows
String filePath = "D:\\test.xls";
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler " + filePath);
您也可以使用Desktop
API,如下所示。
String filePath = "D:\\test.xls";
Desktop dt = Desktop.getDesktop();
dt.open(new File(filePath));