请教一个问题,有关JAVA打开EXCEL文件的

newyu1127 2007-07-20 10:56:21
我的程序需要用EXCEL打开一个文件,用Runtime.getRuntime().exec来打开,
目前我机子上的EXCEL.EXE位置为:
C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE
代码为:
String addr="C:\\Downloads\\TEST.xls";
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE " +addr);
可以正常打开,问题是程序布置到客户端时,无法确认用户机子上的
EXCEL.EXE是在哪个路径下,这怎么办呢?我试过直接
Runtime.getRuntime().exec("EXCEL.EXE " +addr);这样是无法打开的.
如何取得机子上EXCEL.EXE的路径呢?
望赐教,最好是写个简单的例子,谢谢.
...全文
1730 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuokai 2007-07-24
  • 打赏
  • 举报
回复
---------------------------------------------
String addr = "C:\\Downloads\\TEST.xls";
Runtime.getRuntime().exec("cmd /c start "+addr);
----------------------------------------------
路径中有空格就不行了的。。。。。比如C:\documents and setting\a.xls
可以试试看。
newyu1127 2007-07-24
  • 打赏
  • 举报
回复
TO sureyor()
正解OK了,结帐,谢谢。
sureyor 2007-07-24
  • 打赏
  • 举报
回复
String addr = "C:\\Downloads\\TEST.xls";
Runtime.getRuntime().exec("cmd /c start "+addr);
newyu1127 2007-07-24
  • 打赏
  • 举报
回复
自己UP了。
zhuokai 2007-07-23
  • 打赏
  • 举报
回复
怎嚜感覺這麼麻煩,直接這樣就可以了啊:

String addr="C:\\Downloads\\TEST.xls";
Runtime.getRuntime().exec("cmd /c start \"" +addr + "\"");

在cmd下用start就可以了啊,當然跟操作系統有關,windows98應該是command,linux\unix下要查察...
joejoe1991 2007-07-23
  • 打赏
  • 举报
回复
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

api中没有找到这些类。。
newyu1127 2007-07-23
  • 打赏
  • 举报
回复
弹出对话框,让用户进行选择exe的路径是最简单的方法,但被领导拒掉了。
TO sureyor()
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
是要下什么包吗?还有是不是把你程序中的 RegistryKey subKey = software.openSubKey("SubKeyName");换成software.openSubKey("EXCEL.EXE");然后String subKey1Value = subKey.getStringValue("subKey1");
String subKey2Value = subKey.getStringValue("subKey2");

这两个相加就是具体的路径吗?
newyu1127 2007-07-23
  • 打赏
  • 举报
回复
zhuokai(海鸥) ( ) 信誉:100 2007-07-23 12:02:27 得分: 0


怎嚜感覺這麼麻煩,直接這樣就可以了啊:

String addr="C:\\Downloads\\TEST.xls";
Runtime.getRuntime().exec("cmd /c start \"" +addr + "\"");

在cmd下用start就可以了啊,當然跟操作系統有關,windows98應該是command,linux\unix下要查察...
=========================
根本不行,运行结果只是跳出了CMD窗口,标题为C:\Downloads\TEST.xls,而没有打开文件,光标停在当前程序包所在的路径下,即C:\openxls\AmTest> 我试过了只有继续手动输入C:\Downloads\TEST.xls,然后回车才能打开这个文件。为什么会这样?是不是有什么地方写的不对,我的系统为XP。

fengmingjie 2007-07-20
  • 打赏
  • 举报
回复
弹出对话框,让用户进行选择exe的路径
sureyor 2007-07-20
  • 打赏
  • 举报
回复
从注册表读取:
package org.solol.test;

import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

/**
* @author solo L
*
*/
public class JNIRegistryTest {

/**
* @param args
*/
public static void main(String[] args) {
//创建注册表项并设置相应的值
try {
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey("SOFTWARE");

RegistryKey subKey = software.createSubKey("SubKeyName", "");

subKey.setValue(new RegStringValue(subKey, "subKey1",
"subKey1Value"));
subKey.setValue(new RegStringValue(subKey, "subKey2",
"subKey2Value"));
subKey.closeKey();
} catch (NoSuchKeyException e) {
e.printStackTrace();
} catch (RegistryException e) {
e.printStackTrace();
}

//打开注册表项并读出相应的值
try {
RegistryKey software = Registry.HKEY_LOCAL_MACHINE.
openSubKey("SOFTWARE");
RegistryKey subKey = software.openSubKey("SubKeyName");
String subKey1Value = subKey.getStringValue("subKey1");
String subKey2Value = subKey.getStringValue("subKey2");
System.out.println(subKey1Value);
System.out.println(subKey2Value);
subKey.closeKey();
} catch (NoSuchKeyException e) {
e.printStackTrace();
} catch (RegistryException e) {
e.printStackTrace();
}
}
}
lanseliuying 2007-07-20
  • 打赏
  • 举报
回复
我个人认为要在整个磁盘下寻Excel.exe的路径不是一个好的解决办法。
你可以加段代码在客户端让客户来选择好了。

非要这么不可的话,我有段C++代码,你可以参考一下。作些改进。


void TForm1::FindFile(AnsiString Dir) // Dir 是路径 如"c:\\" 或 "D:\\"
{
TSearchRec sr;
int iAttributes = 0;
iAttributes |= faAnyFile;
if ( FindFirst( Dir+"*.*", iAttributes, sr) == 0 )
{
try
{ AnsiString dirStr,fileName;
do
{
if ( sr.Name == "." || sr.Name == ".." )
continue;
if ( sr.Attr == faDirectory )
{
FindFile (Dir+sr.Name+"\\");//找到一个目录
}
else
{
Application->ProcessMessages();
fileName=sr.Name;
dirStr=Dir;
if (fileName.Pos("mang3160.pbd")>0)
{
mydir= Dir;
}
}
Application->ProcessMessages();
}while ( FindNext (sr) == 0);
Application->ProcessMessages();
FindClose(sr);
}
catch(Exception &e)
{
FindClose(sr);
}
}
}
husl 2007-07-20
  • 打赏
  • 举报
回复
俺们上浩方 浩方要找魔兽路径
1:给个浏览对话框,指路径给它
2:让它搜盘,不过慢的很

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧