Java 使用 jdbc 连接到 Excel 表而不指定 DSN 到 Excel 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18181119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:04:34  来源:igfitidea点击:

Connecting to excel sheet using jdbc without specifying DSN to Excel sheet

javaexceljdbc

提问by Abhishek Singh

I want to connect to an excel sheet using jdbc or some other method but i do not want to specify DSN for the same using administrative tool. Is their someway to do it using code? If yes how ?

我想使用 jdbc 或其他一些方法连接到 Excel 工作表,但我不想使用管理工具为其指定 DSN。他们是否有办法使用代码来做到这一点?如果是怎么办?

Thanks in advance

提前致谢

采纳答案by Pandiyan Cool

It is also possible to connect to a spreadsheet without using DSN, which provides a more flexible way within code to point JDBC at an Excel file of interest without the accesses to a client registry to define the required DSN. Without DSN, the db connection is created as following, please not the difference of constructed JDBC URL:

还可以在不使用 DSN 的情况下连接到电子表格,这在代码中提供了一种更灵活的方式来将 JDBC 指向感兴趣的 Excel 文件,而无需访问客户端注册表来定义所需的 DSN。没有DSN,db连接创建如下,请注意构造JDBC URL的区别:

java.sql.DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Excel Driver
(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls");

Here DBQ defines the path to the target spreadsheet file (qa.xls). Both backslash and forward slash work well.

此处 DBQ 定义了目标电子表格文件 (qa.xls) 的路径。反斜杠和正斜杠都可以很好地工作。

Source: Available source

来源:可用来源

回答by Tarik

What you are eluding to is a DSN less connection string. See http://support.microsoft.com/kb/165866for details. Yet, I would opt for Apache POI as mentioned by Jayan 14.

您要避免的是一个无 DSN 的连接字符串。有关 详细信息,请参阅http://support.microsoft.com/kb/165866。然而,我会选择 Jayan 14 提到的 Apache POI。

回答by Kirti

try changing the driver name from Microsoft Excel Driver(*.xls)to Driver do Microsoft Excel(*.xls)

尝试将驱动程序名称从Microsoft Excel Driver(*.xls)Driver更改为Microsoft Excel(*.xls)

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls");

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls");

and if you want to update the excel file use the following connection string:

如果要更新 excel 文件,请使用以下连接字符串:

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls;ReadOnly=0");

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls;ReadOnly=0");