从JButton打开excel表 - Java Swing

时间:2013-04-04 09:34:24

标签: java swing excel

我正在使用swing设计一个表单。如果在我的表单中单击了报表按钮,它必须从我的D盘打开预定义格式的excel表。什么代码必须在按钮动作监听器中写?

谢谢..

2 个答案:

答案 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

中的默认程序打开Excel
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));