首先在控制面板進行數據源ODBC登記java
package duexcel;sql
import java.io.*;數據庫
import java.sql.*;spa
public class duexcel {excel
public static void main(String[] args) throws SQLException{對象
Connection con = null;get
try{it
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Class.forName()就是用來調用JdbcOdbcDriver,直白的就是加載和註冊一個驅動程序。io
con = DriverManager.getConnection( "jdbc:odbc:ExcelJDBC" );//定於數據源class
Statement st = con.createStatement();//經過connection數據庫連接對象 建立一個statement對象數據庫操做對象
ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();//獲取列數
System.out.println ("表格列數"+numberOfColumns );
System.out.println( rsmd.getColumnName(1)+ "," + rsmd.getColumnName(2));
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
rs.close();
st.close();
}
catch(Exception ex) {
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
finally {
con.close();
}
}
}