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

[243 byte] By [studomonlya] at [2007-11-27 11:24:40]
# 1

> Is it possible to do this?

Yes.

http://tinyurl.com./e6m6

~

yawmarka at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 2

Haha ok point made... ok How do you link to open an excel file in java?

Thanks

Stu

studomonlya at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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?

~

yawmarka at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 4

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

studomonlya at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

~

yawmarka at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 6

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

studomonlya at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

~

yawmarka at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 8

That works great thanks alot for your support!!

studomonlya at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...
# 9

> That works great thanks alot for your support!!

You're welcome!

~

yawmarka at 2007-7-29 15:59:42 > top of Java-index,Java Essentials,Java Programming...