开发者

java open csv file using excel

I am making a project for college and have开发者_如何学C made a program which creates csv files. I would like there to be a button which you can click which then opens the csv file with excel. Thanks


Knowing that MsOffice is installed on the system, you should be able to open a document with it from command line using the command

excel myDoc.csv

to execute such a command from java, you could use this snapshot:

File myCSVFile; //reference to your file here 
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
    Process pp = run.exec(execString);
} catch(Exception e) {
    e.printStackTrace();
}

This is somewhat rough and needs styling, of course, but generally it should work. Besides, to be more graceful you could also check Windows registry, using the java.util.prefs.Preferences class, to know if MsOffice is installed and, if yes, where. But, please, be aware, if you are reckoning for MsExcel (as I understood from your post), this will automatically cancel Java's multiplatform approach. Hopefully, this helps :)


If you are using Java 6 you can use the Desktop class. Read also Opening, Editing, and Printing a File


You can use JExcel API. It will be very easy for you.


For whatever reason, execString's provided did not work for me, but the one below worked:

String execString = "cmd /c start excel \"" + filePathString + "\"";

With the other exeString's I kept getting an exception saying that the runtime cannot find the file - start or excel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜