从JButton – Java Swing打开Excel表格

我正在devise一个使用swing的窗体。如果报表button被单击在我的窗体中,它必须从我的D驱动器打开预定义的格式excel工作表。哪些代码必须在button动作监听器中写入?

谢谢 ..

我build议在awt中查看Desktop类。 这里有一个方法可以帮助你。

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

您可以使用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));