OleDb 连接字符串中扩展属性的规范?

发布于 2024-08-30 16:33:49 字数 406 浏览 1 评论 0原文

目前,我正在搜索连接字符串的属性,该属性可用于以只读模式连接到 Excel 文件。搜索 Google 为我提供了很多连接字符串的示例,但我似乎无法在 OleDb 连接字符串的“扩展属性”部分中找到所有可能性的规范。

目前我有这个:

Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Data\Customers.xls; Extended Properties = 'Excel 8.0; Mode=Read; ReadOnly=true; HDR=Yes';

但是......我已经通过示例编写了这个。所以问题: 1. OleDb 连接字符串文档/参考的合适来源是什么? 2. 上述连接字符串是否确实以只读模式连接到Excel文件?

谢谢!

At the moment I'm searching for properties for a connection string, which can be used to connect to an Excel file in readonly mode. Searching Google gets me a lot of examples of connection strings, but I can't seem to find a specification of all possibilities in the 'Extended Properties' section of the OleDb connection string.

At the moment I've this:

Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Data\Customers.xls; Extended Properties = 'Excel 8.0; Mode=Read; ReadOnly=true; HDR=Yes';

However... I've composed this by examples. So questions:
1. What is a decent source for OleDb Connection String documentation/reference?
2. Is the above connection string indeed connecting to the Excel file in readonly mode?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-09-06 16:33:49

我正在使用 UDL 文件。

接下来做:

  1. 创建空文件test.udl
  2. 打开它
  3. 您将看到“数据链接属性”对话框
  4. 在第一个选项卡上将提供程序更改为 Microsoft.Jet.OLEDB.4.0;
  5. 第二个选项卡选择您的 Excel 文件
  6. 第三个选项卡设置权限,例如“读取”
  7. 在最后一个选项卡上设置扩展属性 = 'Excel 8.0; HDR=是'

然后保存并在文本编辑器中打开文件,您将看到连接字符串

您也可以查看 msdn 文章 ADO 提供程序属性和设置

I am using UDL file for that.

Do next:

  1. create empty file test.udl
  2. open it
  3. You will see Data Link Properties dialog
  4. On first tab change provider to Microsoft.Jet.OLEDB.4.0;
  5. Second tab select you Excel file
  6. Third tab set permission like Read
  7. On last tab set Extended Properties = 'Excel 8.0; HDR=Yes'

Than save, and open file in text editor and you will see connection string

As well you can check msdn article ADO Provider Properties and Settings

尘世孤行 2024-09-06 16:33:49

对 Excel 规范的基于 COM 的数据访问可能隐藏在几乎无法访问的 Microsoft 存档文档中。 (通常作为一个巨大的 PDF)

我将规范添加到此处的另一个答案: https://stackoverflow.com/a/68912543/6237912 :

为了完整性,复制到这个答案

连接字符串有一些部分:

Provider:它是用于打开Excel的主要oledb提供程序
床单。对于 Excel 97 及以上版本,这将是 Microsoft.Jet.OLEDB.4.0
文件格式和 Microsoft.ACE.OLEDB.12.0(适用于 Excel 2007 或更高版本)
Excel 文件格式(扩展名为 xlsx)

数据源:是Excel工作簿的整个路径。您需要提及与 Excel 文件相对应的 dospath。这样,就会
看起来像:数据源=C:\testApp.xls"。

扩展属性(可选):扩展属性可应用于 Excel 工作簿,这可能会更改 Excel 的整体活动
您的程序中的工作簿。最常见的有以下几种:

HDR:代表Excel表格中字段的表头。默认为“是”。如果您的标题中没有字段名称
工作表中,您可以指定 HDR=NO ,这将采用
它找到的表为 f1、f2 等。

ReadOnly:您还可以通过指定ReadOnly=true以只读模式打开Excel工作簿;默认情况下,Readonly 属性为 false,因此
您可以修改工作簿中的数据。

FirstRowHasNames:与 HDR 相同,始终设置为 1(表示 true),如果您没有,可以将其指定为 false
标题行。如果 HDR 为“是”,则提供商会忽略此属性。你可以
通过更改来更改环境的默认行为
注册值
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\FirstRowHasNames] 到 00
(这是错误的)

MaxScanRows:Excel 不提供其找到的表的详细架构定义。它需要先扫描行
决定字段的数据类型。 MaxScanRows 指定
在决定数据类型之前要扫描的单元格数量
柱子。默认情况下,该值为 8。您可以指定任何值
从 1 - 16 表示 1 到 16 行。您还可以将该值设置为 0,以便
它在决定数据类型之前搜索所有现有行。你可以
通过更改值来更改此属性的默认行为
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows] 这是
默认为 8。目前,MaxScanRows 被忽略,因此您只需
取决于 TypeGuessRows 注册表值。希望微软修复这个问题
问题到其更高版本。

IMEX:(警告)如上所述,Excel 必须猜测一个数字或行来选择最合适的数据类型
列中,如果您在一个列中混合数据,则可能会出现严重问题
柱子。假设您在单列上有整数和文本数据,
在这种情况下,Excel 将根据大多数数据选择其数据类型
数据。因此,它选择大多数数据类型的数据,即
选择,并为少数数据类型返回 NULL。如果两个
类型在列中均匀混合,提供者选择数字
超过文本。

例如,在八 (8) 个扫描行中,如果该列包含五 (5) 个数值和三 (3) 个文本值,则
提供程序返回五 (5) 个数字和三 (3) 个空值。

要解决此数据问题,请在连接字符串的扩展属性部分中设置“IMEX=1”。这强制执行
ImportMixedTypes=Text 注册表设置。您可以更改
通过改变强制类型
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\ImportMixedTypes] 到
也是数字。

因此,如果您查看所有这些简单的连接字符串,它将如下所示:

 Provider=Microsoft.Jet.OLEDB.4.0;数据源=c:\\testexcel.xls;
    扩展属性=\"Excel 8.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\""

    或者:
    复制代码

    提供商=Microsoft.ACE.OLEDB.12.0;数据源=C:\\testexcel.xlsx;
    扩展属性=\"Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\""

我们需要将扩展​​属性放入 Quotes(") 中,因为有多个值。

COM-based data access to Excel specifications are likely buried in nearly inaccessible Microsoft archive documentation. (Usually served as one enormous PDF)

I added the spec to another answer here: https://stackoverflow.com/a/68912543/6237912

Copied to this answer for completeness:

The connectionstring has some parts:

Provider: It is the main oledb provider that is used to open the Excel
sheet. This will be Microsoft.Jet.OLEDB.4.0 for Excel 97 onwards Excel
file format and Microsoft.ACE.OLEDB.12.0 for Excel 2007 or higher
Excel file format (One with xlsx extension)

Data Source: It is the entire path of the Excel workbook. You need to mention a dospath that corresponds to an Excel file. Thus, it will
look like: Data Source=C:\testApp.xls".

Extended Properties (Optional): Extended properties can be applied to Excel workbooks which may change the overall activity of the Excel
workbook from your program. The most common ones are the following:

HDR: It represents Header of the fields in the Excel table. Default is YES. If you don't have fieldnames in the header of your
worksheet, you can specify HDR=NO which will take the columns of the
tables that it finds as f1,f2 etc.

ReadOnly: You can also open Excel workbook in readonly mode by specifying ReadOnly=true; By default, Readonly attribute is false, so
you can modify data within your workbook.

FirstRowHasNames: It is the same as HDR, it is always set to 1 ( which means true) you can specify it as false if you don't have your
header row. If HDR is YES, provider disregards this property. You can
change the default behaviour of your environment by changing the
Registry Value
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\FirstRowHasNames] to 00
(which is false)

MaxScanRows: Excel does not provide the detailed schema defination of the tables it finds. It need to scan the rows before
deciding the data types of the fields. MaxScanRows specifies the
number of cells to be scanned before deciding the data type of the
column. By default, the value of this is 8. You can specify any value
from 1 - 16 for 1 to 16 rows. You can also make the value to 0 so that
it searches all existing rows before deciding the data type. You can
change the default behaviour of this property by changing the value of
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows] which is
8 by default. Currently, MaxScanRows is ignored, so you need only to
depend on TypeGuessRows Registry value. Hope Microsoft fixes this
issue to its later versions.

IMEX: (A Caution) As mentioned above, Excel will have to guess a number or rows to select the most appropriate data type of the
column, a serious problem may occur if you have mixed data in one
column. Say you have data of both integer and text on a single column,
in that case, Excel will choose its data type based on majority of the
data. Thus it selects the data for the majority data type that is
selected, and returns NULL for the minority data type. If the two
types are equally mixed in the column, the provider chooses numeric
over text.

For example, in your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the
provider returns five (5) numbers and three (3) null values.

To work around this problem for data, set "IMEX=1" in the Extended Properties section of the connection string. This enforces
the ImportMixedTypes=Text registry setting. You can change the
enforcement of type by changing
[HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\ImportMixedTypes] to
numeric as well.

Thus if you look into the simple connectionstring with all of them, it will look like:

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\testexcel.xls;
    Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\""

    or:
    Copy Code

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\testexcel.xlsx;
    Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\""

We need to place extended properties into Quotes(") as there are multiple number of values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文