星期四, 6月 23, 2011

[Java] Read Microsoft XLS using JDBC

import java.sql.*;

public class MainWindow {
 public static void main(String[] args) {
  try {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=d:/desktop/test.xls");
   Statement st=con.createStatement();
   ResultSet rs= st.executeQuery("select * from [Sheet1$]");//Sheet1是工作表名稱
   while(rs.next()){
    System.out.println(rs.getInt("id")+" "+rs.getString("name"));
   }
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  
 }

}

Reference:
Apache POI - Code Sample
Handle Excel files

沒有留言:

張貼留言

留個話吧:)