用 Schema.ini 文件也可以达到要求。 作用:Schema.ini用于提供文本文件中记录的构架信息。每个 Schema.ini 项都用于指定表的五个特征之一: 1、文本文件名 2、文件格式 3、字段名、字段长度、字段类型 4、字符集 5、特别数据类型转换 指定文件名 文件名要用方括号括起来,例如如果要对 Sample.txt 使用数据构架信息文件,那么它的对应的项应该是
指定文件格式
格式说明 | 表格式 | Schema.ini 格式描述 |
---|
Tab 制表符分隔 | 文件中的字段用制表符分隔 | Format=TabDelimited | CSV 分隔 | 文件中的字段用逗号来分隔 | Format=CSVDelimited | 自定义分隔 | 文件中的字段可以用任何字符来分隔,所有的字符都可以用来分隔,包括空格,但是双引号 ( " ) 除外 | Format=Delimited(自定义分隔符) - 或者没有分隔符 - Format=Delimited( ) | 固定宽度 | 文件中的字段为固定长度 | | 指定字段 你可以有两种方法在一个字符分隔的文本文件中指定字段名 1、在文本文件中的第一行包含字段名,并且设置 ColNameHeader 为 True 。 2、用数字编号指定每一列并且指定每一列的名字以及数据类型 你必须用数字编号指定每一列并且指定每一列的名字、数据类型以及长度(在固定长度分隔的文本文件中需要指定长度) 注意,设定了 ColNameHeader 选项,在 Schema.ini 中 Windows 注册时会忽略 FirstRowHasNames 选项。 你也可以指定字段的数据类型,使用 MaxScanRows 选项用来指定在确定列的数据类型时要扫描多少行数据。设置 MaxScanRows 为 0 将扫描整个文件。 如果文本文件第一行包含字段名,并且要扫描整个文件,改项目就要定义如下:
ColNameHeader=True MaxScanRows=0 |
接下来的项目用来指定表中的字段,使用列编号(Coln)选项来指定列。字段长度在“固定分隔文本文件中”是必填项目,在“字符分隔文本文件”中是可选项目。 示例:定义 2 个字段,CustomerNumber 是长度为 10 的文本字段、CustomerName 是长度为 30 的文本字段。
Col1=CustomerNumber Text Width 10 Col2=CustomerName Text Width 30 |
语法如下: Coln=ColumnName type [Width #] 参数解释如下:
参数 | 说明 |
---|
ColumnName | 文本,标识字段名,如果包含空格要用双引号括起来 | type | 数据类型包括: Microsoft Jet 数据类型:Bit Byte Short Long Currency Single Double DateTime Text Memo ODBC 数据类型: Char (same as Text) Float (same as Double) Integer (same as Short) LongChar (same as Memo) Date date format 其中date format 是日期的格式字符串例如:Date YYYY-MM-DD | Width | 字符串的长度,后面的数字用来指定字段的长度(“固定分隔文本文件”为必填,“文字分隔文本文件”为可选) | # | 整形数字,标识字段长度 | 指定字符集 CharacterSet 项有两个选择:ANSI | OEM 选择 ANSI 字符集用如下方法:
特别数据类型转换 特别数据类型转换主要是定义比如日期、货币型数据如何转换或者如何显示的,你可以参考下面这张表:
选项 | 说明 |
---|
DateTimeFormat
| Can be set to a format string indicating dates and times. You should specify this entry if all date/time fields in the import/export are handled with the same format. All Microsoft Jet formats except A.M. and P.M. are supported. In the absence of a format string, the Windows Control Panel short date picture and time options are used.
| DecimalSymbol
| Can be set to any single character that is used to separate the integer from the fractional part of a number.
| NumberDigits
| Indicates the number of decimal digits in the fractional portion of a number.
| NumberLeadingZeros
| Specifies whether a decimal value less than 1 and greater than –1 should contain leading zeros; this value can either be False (no leading zeros) or True. | CurrencySymbol
| Indicates the currency symbol to be used for currency values in the text file. Examples include the dollar sign ($) and Dm.
| CurrencyPosFormat
| Can be set to any of the following values: · Currency symbol prefix with no separation ($1) · Currency symbol suffix with no separation (1$) · Currency symbol prefix with one character separation ($ 1) · Currency symbol suffix with one character separation (1 $)
| CurrencyDigits
| Specifies the number of digits used for the fractional part of a currency amount.
| CurrencyNegFormat
| Can be one of the following values: · ($1) · –$1 · $–1 · $1– · (1$) · –1$ · 1–$ · 1$– · –1 $ · –$ 1 · 1 $– · $ 1– · $ –1 · 1– $ · ($ 1) · (1 $) This example shows the dollar sign, but you should replace it with the appropriate CurrencySymbol value in the actual program.
| CurrencyThousandSymbol
| Indicates the single-character symbol to be used for separating currency values in the text file by thousands.
| CurrencyDecimalSymbol
| Can be set to any single character that is used to separate the whole from the fractional part of a currency amount.
|
下面给出一个简单的例子,假设有一个表Contacts.txt类似下面:
姓名 单位 联系日期 王海 上海有机化学研究所 2002-1-1 罗炙 数字化机床研究院 2004-1-1 |
导入 Access 应该类似下面表格:
姓名 | 单位 | 联系日期 |
---|
王海 | 上海有机化学研究所 | 2002-1-1 | 罗炙 | 数字化机床研究院 | 2004-1-1 | 那么 Schema.ini 则是类似下面的INI文件:
[Contacts.txt] ColNameHeader=True format=Delimited(" ") MaxScanRows=0 CharacterSet=ANSI Col1="姓名" Char Width 10 Col2="单位" Char Width 9 Col3="联系日期" Date Width 8 |
注释如下:
[Contacts.txt] ///文本文件名 ColNameHeader=True ///带有表头 format=Delimited( ) ///空格作为分隔符,如果是分号,请用format=Delimited(;) 来解决 MaxScanRows=0 ///扫描整个文件 CharacterSet=ANSI ///ANSI 字符集 Col1="姓名" Char Width 10 ///字段1 Col2="单位" Char Width 9 ///字段2 Col3="联系日期" Date Width 8 ///字段3 ///如果有更多字段可 Col4 .... ColN |
注意,Schema.ini 必须和需要导入的文本文件在同一目录。 此后,我们就可以利用下面的语句来导入数据了:
CurrentProject.Connection.Execute "SELECT * INTO NewContact FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=C:/;].[Contacts#txt];" |
注意,到 2000 格式的 MDB 为止,以下语句都会导致导入失败,应该是 Access 本身的问题: 到 Access XP / access 2003 出现不知道是否已经解决该问题,大家可以在“评论”中告诉我测试结果。
DoCmd.TransferText acImportFixed, , "Contacts", "C:/contacts.txt" |
或者
DoCmd.TransferText acImportFixed, "C:/.ini", "Contacts", "C:/Documents.txt" |
错误消息为: 运行时错误 '3625': 文本文件规范 'c:schema.ini' 不存在。不能使用规范进行导入、导出或者链接。 或者 运行时错误 '2511': 这个操作或方法需要一个 Specification Name 参数。 另外,请参考: http://support.microsoft.com/default.aspx?scid=kb;en-us;241477
|