Runtime r=Runtime.getRuntime();
Process p=null;
String cmd[]={"excel的可执行文件名",参数};  //参数就是你的.xls文件
try{
    p=r.exec(cmd);
    p.waitFor();
}
     catch(Exception e){
           System.out.println("execute fail."+e);
     }

解决方案 »

  1.   

    public class ex_3 {
      public static void main(String args[]){
        Runtime r = Runtime.getRuntime();
        Process p = null;
        
        try{
          p = r.exec("notepad");
        }catch(Exception e){
          System.out.println("Error executing notepad.");
        }
      }
    }没有你说的那几个参数吧.
      

  2.   

    假设a.xls在f:\下存放,
    import java.lang.Runtime;
    public class TestExe{
     public String command="C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.exe  f:\\a.xls";
      public TestExe(){
    try{
      Runtime o=java.lang.Runtime.getRuntime();
      o.exec(command);
    }
    catch(Exception e){
    }
      }
      public static void main(String argv[]){
    new TestExe();
      }
    }
      

  3.   

    多谢三位,不过:
    exec(String command)
    exec(String[] cmdarray) 
    最终调用的是:exec(String[] cmdarray, String[] envp, File dir),只不过envp和dir都被设定为Null了,我想知道:
    1)envp如果不设定为Null时,都允许设定为什么值,可以怎样使用;
    2)cmdarray,envp,dir都不为Null时,怎样搭配使用。
      

  4.   

    看文档:
    cmdarray - array containing the command to call and its arguments.
    envp - array of strings, each element of which has environment variable settings in format name=value.
    dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process
      

  5.   

    public Process exec(String[] cmdarray,
                        String[] envp,
                        File dir)
                 throws IOExceptionExecutes the specified command and arguments in a separate process with the specified environment and working directory. 
    If there is a security manager, its checkExec method is called with the first component of the array cmdarray as its argument. This may result in a security exception. Given an array of strings cmdarray, representing the tokens of a command line, and an array of strings envp, representing "environment" variable settings, this method creates a new process in which to execute the specified command. If envp is null, the subprocess inherits the environment settings of the current process. The working directory of the new subprocess is specified by dir. If dir is null, the subprocess inherits the current working directory of the current process. 
    Parameters:
    cmdarray - array containing the command to call and its arguments.
    envp - array of strings, each element of which has environment variable settings in format name=value.
    dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. 
    Returns:
    a Process object for managing the subprocess. 
    Throws: 
    SecurityException - if a security manager exists and its checkExec method doesn't allow creation of a subprocess. 
    NullPointerException - if cmdarray is null 
    IndexOutOfBoundsException - if cmdarray is an empty array (has length 0). 
    IOException - if an I/O error occurs.
      

  6.   

    我知道的一段代码,不知对你有没有帮助.
    public class cmdie 
    {
    public static void main(String[] args)
    {
    try{
    String message="www.sina.com.cn";
    String cmd[]={"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE",message};
    Process p=Runtime.getRuntime().exec(cmd);
    p.waitFor();
    System.out.println ("程序执行完毕!");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }}
    在windows操作系统下执行
      

  7.   

    envp[] 环境变量,如path=c:\java\bindir 工作目录cmd[] 命令及参数