2011-07-28 58 views
0

我必須將excel文件與我正在開發的應用程序軟件鏈接起來。excel文件將包含用於進行調查的問卷調查。我有這樣的代碼,打開一個Jpanel來選擇文件。選擇文件後沒有任何事情發生。我希望能夠根據excel文件中的問題生成一個模板(例如從excel文件中提取問題並創建一個模板從它),我不得不在網上上傳。請你幫我這個?如何使用Java swing將Excel文件與應用程序軟件鏈接起來

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)"; } 
     } 
+2

當然,nothign正在發生,你只選擇文件名,但你sem不要對文件做任何事情。無論如何,你的問題完全不清楚,你說的「將excel文件鏈接到你的應用程序」意味着什麼?你想啓動Excel並讓它打開文件?你想與Excel交談以對文件做些什麼嗎? –

+0

@Angel O'Sphere +1好問題。我準備跳到一些結論,但我仍然有興趣聽到答案。 –

+0

「鏈接excel文件」意味着excel文件將包含調查問卷。我必須使用我正在開發的軟件閱讀這些問卷,並將其存儲在數據庫中。之後,我必須開發基於這些問題來自數據庫,並且必須在網上上傳這個模板。所以我無法做到這些軟件的這些部分,即閱讀,存儲在數據庫中,然後創建模板 – Abhinav

回答

1

查看該源代碼的一些提示。

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 - 這裏的JFrame可能會更好轉換爲JDialogJOptionPane

+0

我不相信,我不相信,那裏仍然存在+1 – mKorbel

相關問題