在java中访问.xlsb文件

时间:2015-11-20 14:04:54

标签: java excel jdbc odbc

我有以下的JDBC代码。我正在尝试创建dnsless连接。

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class dummy {
    public static void main(String[] argv) {
        System.out.println("-------- Oracle JDBC Connection Testing ------");
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;
        }
        System.out.println("Oracle JDBC Driver Registered!");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=D:/abc.xls;");
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }
        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

此程序正常工作,输出如下。

-------- Oracle JDBC Connection Testing ------
Oracle JDBC Driver Registered!
You made it, take control your database now!

但我需要访问.xlsb文件,当我更改

connection = DriverManager
                    .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=D:/abc.xls;");

connection = DriverManager
                    .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xlsb)};DBQ=D:/abc.xlsb;");

我得到以下输出,但有例外。

-------- Oracle JDBC Connection Testing ------
Oracle JDBC Driver Registered!
Connection Failed! Check output console
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at dummy.main(dummy.java:19)

请让我知道如何解决此问题,我想使用dnsless connection。因为我有多个Excel表格可供使用。

我的所有应用都是32位。

由于

1 个答案:

答案 0 :(得分:1)

正如其名称所示,旧的Excel ODBC驱动程序

Driver={Microsoft Excel Driver (*.xls)};

仅适用于.xls个文件。

较新的Excel ODBC驱动程序

Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};

也适用于最新版本的Excel(2007及更高版本)使用的Open XML file types。较新的Excel驱动程序包含在Access Database Engine安装中。