roger666888 2008-09-09 15:24
浏览 233
已采纳

如何通过java代码直接打开自己主机上的一个Excel表格

如何通过java代码直接打开自己主机上的一个Excel表格,不是表格中的一些数据而是整个Excel表格都要打开!

  • 写回答

1条回答 默认 最新

  • xuxiaolei 2008-09-10 11:46
    关注

    不知道是不是这个意思,看下面的代码
    [code="java"]
    import java.io.*;

    public class ExecuteCommandBean {

    private String command;
    private Process process;
    private String responseText;
    private InputStream is;
    private InputStreamReader isr;
    private BufferedReader br;
    
    public ExecuteCommandBean(String command) {
        this.command = command;
    }
    
    public void execute() throws Exception {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        process.waitFor();
    
        is = process.getInputStream();
        isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
    
        StringBuffer buffer = new StringBuffer();
        String line = null;
    
        while( (line = br.readLine()) != null ) {
            buffer.append(line + "\n");
        }
    
        responseText = buffer.toString();
    }
    
    public String getResponseText() throws Exception {
        return responseText;
    }
    
    public void close() {
    
        try {
            if(br != null) { br.close(); }
        } catch(Exception e) {
            e.printStackTrace();
        }
    
        try {
            if(isr != null) { isr.close(); }
        } catch(Exception e) {
            e.printStackTrace();
        }
    
        try {
            if(is != null) { is.close(); }
        } catch(Exception e) {
            e.printStackTrace();
        }
    
        try {
            if(process != null) { process.destroy(); }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
        //windows下
        String command = "excel c:/books.xls";
        ExecuteCommandBean ecb = new ExecuteCommandBean(command);
        ecb.execute();
        System.out.println(ecb.getResponseText());
    }
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog