在Java Swing应用程序中导入Excel(.xls)

时间:2011-09-05 10:19:29

标签: java excel swing

如何将Microsoft Excel(XLS)文件导入Java Swing应用程序?

3 个答案:

答案 0 :(得分:5)

您可以使用几个开源库来读取Apache POI等Excel文件。

一旦将excel文件读入某个数据结构,就可以将其写入JTable的DataModel中。或者,你可以编写一个自定义的DataModel,它将从excel文件中动态读取,但是一旦你理解了你选择的excel文件读取库,我就会单独执行这两个步骤并增加复杂性。

答案 1 :(得分:3)

这是一个简单的教程,使用Tab-Separated Values(TSV)格式将数据从JTable导出到excel文件中:How To Export Records From JTable To MS Excel

应用程序提供“导出”按钮,然后触发一个对话框(JFileChooser)以帮助用户指定文件位置/目标。

enter image description here

JTable实现

enter image description here

导出的Excel文件(手动调整的列宽)

答案 2 :(得分:2)

希望此代码能为您提供帮助。

import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ExcelSheetReader{
   private static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
   private static final String DATABASE_URL = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;DriverID=22;READONLY=false";

   private static final String FILEPATH = "C:/Documents and Settings/web/Desktop/Employee.xlsx";

   private static Connection con = null;
   private static Statement stmt = null;

   private Connection getConnection(File file) throws ClassNotFoundException, SQLException{
       Class.forName(DRIVER_NAME);
       Connection con = DriverManager.getConnection(String.format(DATABASE_URL, file.getAbsolutePath()));
       return con;
   }

     public List<String> getData(File file, String []columns) throws SQLException {

       List<String> list = null;
          try {
              ResultSet rs = getRecord(file, "select emailid from [Sheet1$]");
              ResultSetMetaData rsmd = rs.getMetaData();
              System.out.println(rsmd.getColumnCount());

             while (rs.next()) {
                 if (list==null)
                        list = new  ArrayList<String>();
                 for (int i=0; i<columns.length; i++) {
                     list.add(rs.getString(columns[i]));
                 }
             }
             rs.close();
           } catch (Exception e) {
            }
          finally {
             closeConnection();
             return list;
          }
   }
   private void closeConnection() throws SQLException {
       if (stmt!=null) {
           stmt.close();
           stmt = null;
       }

       if (con!=null) {
           con.close();
           con = null;
       }

}
ResultSet getRecord(File file, String query) throws ClassNotFoundException, SQLException {
        con = getConnection(file);
       stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery(query);
    return rs;
}
public static void main(String[] args) {
       /*ExcelSheetReader reader = new ExcelSheetReader();
       String[] columns = {"emailid"};
       File file = new File(FILEPATH);
       List<String> list = null;
    try {
        list = reader.getData(file,columns);
    } catch (SQLException e) {
        e.printStackTrace();
    }
       for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        String string = (String) iterator.next();
        System.out.println(string); 
       }
       */
       ExcelSheetReader reader = new ExcelSheetReader();
        File file = new File(FILEPATH);
        if (file.exists()) {
            try {
                ResultSet rs = reader.getRecord(file, "select emailid from [Sheet1$]");
                ResultSetMetaData rsmd = rs.getMetaData();
                int count = rsmd.getColumnCount();
                //System.out.println(count);
                while (rs.next()) {
                    for (int i=1; i<=count; i++) {
                        System.out.println(rsmd.getColumnName(i)+": "+rs.getString(rsmd.getColumnName(i)));
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
   }
}