簡體   English   中英

從JButton打開Excel工作表-Java Swing

[英]Open excel sheet from JButton - Java Swing

我正在使用swing設計表單。如果在表單中單擊了報告按鈕,則必須從D驅動器中打開預定義格式的excel表格。在按鈕動作偵聽器中必須編寫哪些代碼?

謝謝 ..

我建議看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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM