Linking to Open a file in java
Hello,
I wanted to create a link in my program that opens an excel spread sheet, seperately from the program. (i.e opens in excel). Is it possible to do this?
Thanks
Stu
Message was edited by:
studomonly
Hello,
I wanted to create a link in my program that opens an excel spread sheet, seperately from the program. (i.e opens in excel). Is it possible to do this?
Thanks
Stu
Message was edited by:
studomonly
> How do you link to open an excel file in java?
Which part are you confused about? How to create a link? How to open Excel from Java? When you searched the Internet for these topics (you *did* search, right?), what didn't work with the solutions you tried?
~
I want the user to click a button and it open and excel file. Thats what I mean by link to it. I dont want the user to be able to open the file with in my program. How could I do this?
Thanks
Stu
I want the user to click a button and it open and excel file. Thats what I mean by link to it. I dont want the user to be able to open the file with in my program.
Okay, do you know how to create a button? How about how to write an action listener? When you searched for these topics on the Internet (you *did* search, right?), what about the given solutions didn't work for you?
> How could I do this?
You haven't answered my previous questions, yet.
~
Yes I can create a button, what I have so far is that a button is pressed and a .csv file is created. That works fine. It is created in the program folder. All I want it to do is open it. Whenever I have searched for "Opening Files, Java" on the internet it comes up with articles on how to read in files into the program, which I can do. I just want the program to open the file as if the user had just double clicked on it. The file it saves it to is "printOut.csv". How would I make the program open this file?
Thanks
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
class OpenExcelDemo {
public static void main(String[] args) {
new OpenExcelDemo().go();
}
void go() {
JFileChooser jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel Files", "xls", "csv");
jfc.setFileFilter(filter);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
open(jfc.getSelectedFile());
}
}
private void open(File f) {
String[] commands = {
"rundll32",
"url.dll,FileProtocolHandler",
f.getAbsolutePath()
};
try {
Runtime.getRuntime().exec(commands);
} catch (IOException e) {
e.printStackTrace();
}
}
}
All normal caveats apply.
~