开发者

How to link an excel file with the application software using Java swing

I have to link an excel file with a application software which I am developing.The exce开发者_运维百科l file will contain questionnaire for conducting surveys.I have this code which is only able to open a Jpanel to select the file.After I select the file nothing is happening.I wanted to be able to generate a template based on the questions that are in the excel file (like extracting the questions from the excel file and creating a template from it) and which I have to upload on the web later.could you please help me with this?

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;


public class SelectFile extends JFrame{


    public static void main(String[]args){
                    JFrame frame = new JFrame();
                    frame.setLayout(null);

                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setTitle("Select File for Linking");
                    frame.setSize(400, 100);
                    Container container = frame.getContentPane();
                    container.setLayout(new GridBagLayout());

                    final JTextField text=new JTextField(20);

                    JButton b=new JButton("Select File");
                    text.setBounds(20,20,120,20);
                    b.setBounds(150,20,80,20);

                   // b.setText("<html><font color='blue'><u>Select File</u></font></html>");
                    b.setHorizontalAlignment(SwingConstants.LEFT);
                    //b.setBorderPainted(false);
                    //b.setOpaque(false);
                   // b.setBackground(Color.lightGray);
                    b.addActionListener(new ActionListener() {
                      public void actionPerformed(ActionEvent e){
                                JFileChooser fc = new JFileChooser();
                                fc.addChoosableFileFilter(new OnlyExt());

                                int returnval = fc.showOpenDialog(null);
                                if (returnval == JFileChooser.APPROVE_OPTION) {
                                File file = fc.getSelectedFile();
                                text.setText(file.getPath());
                                } 
                            }
                    });
                    container.add(text);
                    container.add(b);
                    frame.setVisible(true);
            }
    }
        class OnlyExt extends javax.swing.filechooser.FileFilter{
            public boolean accept(File file) {
        if (file.isDirectory()) return false;
        String name = file.getName().toLowerCase();
        return (name.endsWith(".xls"));
        }
        public String getDescription() { return "Excel ( *.xls)"; }
        }


Apache POI http://poi.apache.org/ provides an API for reading / writing Excel Files.


Look over this source for some tips.

import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.border.EmptyBorder;

public class SelectFile {

    public static void main(String[]args) {

        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Select File for Linking");
                // don't use null layouts.
                //frame.setLayout(null);

                // create a panel so we can add a border
                JPanel container = new JPanel(new FlowLayout(3));
                container.setBorder(new EmptyBorder(10,10,10,10));
                frame.setContentPane(container);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // instead call pack() after components are added
                //frame.setSize(400, 100);

                final JTextField text=new JTextField(20);

                JButton b=new JButton("Select File");

                // irrelevant unless button stretched by layout
                //b.setHorizontalAlignment(SwingConstants.LEFT);
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser fc = new JFileChooser();
                        String desc = "Excel ( *.xls)";
                        String[] types = {".xls"};
                        fc.addChoosableFileFilter(
                            new FileNameExtensionFilter(desc, types));

                        int returnval = fc.showOpenDialog(null);
                        if (returnval == JFileChooser.APPROVE_OPTION) {
                            File file = fc.getSelectedFile();
                            text.setText(file.getPath());
                            try {
                                // 1.6+
                                Desktop.getDesktop().edit(file);
                            } catch(Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                });
                container.add(text);
                container.add(b);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

BTW - The JFrame here would probably be better converted to a JDialog or JOptionPane.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜