导入文本文件时如何指定字段类型?(二)

导入文本文件时如何指定字段类型?

问题:

| 我有一个文本文件需要导入 __ Access __ ,但是文本文件中有一列数据原本是文本,但是导入数据库后自动变成了“双精度”类型,我该如何让各个字段按我需要的数据类型生成哪?

如何让 __ ACCESS __ 按照我规定的构架、规格从文本文件、XLS文档中导入数据?

方法二:

| 用 __ Schema.ini __ 文件也可以达到要求。

作用:Schema.ini用于提供文本文件中记录的构架信息。每个 __ Schema.ini __ 项都用于指定表的五个特征之一:
1、文本文件名
2、文件格式
3、字段名、字段长度、字段类型
4、字符集
5、特别数据类型转换

指定文件名
文件名要用方括号括起来,例如如果要对 __ Sample.txt __ 使用数据构架信息文件,那么它的对应的项应该是
| [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

语法如下:
Col n = 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 __ 字符集用如下方法:
CharacterSet=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


http://www.access911.net 站长收藏

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus