Suseendran P
Suseendran P

Reputation: 557

Open excel sheet from JButton - Java Swing

I'm designing a form using swing.If report button was clicked in my form it have to open the predefined format excel sheet from my D drive.What code have to write in button action listener ??

Thanks ..

Upvotes: 0

Views: 6049

Answers (2)

David
David

Reputation: 20063

I'd recommend looking at the Desktop class in awt. There's a method here that should help you.

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

Upvotes: 4

Jayamohan
Jayamohan

Reputation: 12924

You can use Runtime class to open the excel using the default program in Windows

String filePath = "D:\\test.xls";
Process p = 
  Runtime.getRuntime()
   .exec("rundll32 url.dll,FileProtocolHandler " + filePath);

You can also use Desktop API as below.

String filePath = "D:\\test.xls";
Desktop dt = Desktop.getDesktop();
dt.open(new File(filePath));

Upvotes: 0

Related Questions