怎么在JAVA程序中设置一个按钮 当点击该按钮时打开(启动)Excel电子表格
最好附代码 并有详细的解释 
拜托大家 不胜感激

解决方案 »

  1.   

    package com.tibco.frame;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileWriter;
    import java.io.IOException;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class ButtonFrame extends JFrame{ private JPanel infoPanel = new JPanel();

    public ButtonFrame() {
    this.setTitle("Web Service");
    this.setSize(400, 200);


    this.add(getInfoPanel());
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JPanel getInfoPanel() {
    if(infoPanel == null){
    infoPanel = new JPanel();
    }
    JLabel nameLabel = new JLabel("Click!");
    infoPanel.add(nameLabel);

    JButton commitButton = new JButton("Start");
    commitButton.addActionListener(new
    ActionListener(){ public void actionPerformed(ActionEvent arg0) {
    try {
    //这里填你的excel文件名
    openExcel("f:\\1.txt");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    });
    infoPanel.add(commitButton);
    return infoPanel;
    } /**
     * open file
     * @param excelFileName
     * @throws IOException 
     */
    public void openExcel(String excelFileName) throws IOException {
    FileWriter   writer   =   new   FileWriter( "c:\\mysql.bat"); 
    writer.write(excelFileName);
    writer.close();
    String command = excelFileName;
    try {
    Process   child   =   Runtime.getRuntime().exec("c:\\mysql.bat");
    } catch (IOException e) {
    System.out.println("Process error!");
    e.printStackTrace();

    }

    public static void main(String[] args) {
    ButtonFrame frame = new ButtonFrame();
    }
    }