使用JDBC ODBC从Excel读取数据

时间:2014-09-16 13:33:03

标签: excel jdbc odbc

有人可以解释如何使用JDBC ODBC从excel表中读取数据而不使用DSN。

2 个答案:

答案 0 :(得分:0)

final static String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
final static String DATABASE_URL = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;DriverID=22;READONLY=false";

public static void main(String[] args)throws Exception {
    File file = new File("C:\\Users\\z671866\\Desktop\\TAS_VAS\\TOS_ERROR_05092014.xlsx");      
    Class.forName(DRIVER_NAME);
    Connection con = DriverManager.getConnection(String.format(
            DATABASE_URL, file.getAbsolutePath()));

    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT SN,IR FROM [Sheet1$]");
    while( rs.next()){
        System.out.println(rs.getString("SN"));
        System.out.println(rs.getString("IR"));
    }

}

答案 1 :(得分:0)

将此代码用于波斯语和中文文本:

String new_Str = new String(excelfield.getBytes(1), "Cp1256"); //....to Persian text

String new_Str = new String(excelfield.getBytes(1), "UTF-8"); //....to Chinese text

OR

String new_Str = new String(your_str.getBytes(), "Cp1256");

String new_Str = new String(your_str.getBytes(), "UTF-8");