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

发布时间:2025-07-05 01:43

如何在Illustrator中导入SVG文件 #生活技巧# #数码产品使用技巧# #设计软件使用技巧#

最新推荐文章于 2024-04-30 14:30:32 发布

huang714 于 2019-03-22 11:26:59 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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

===========================================================================

问题:

我有一个文本文件需要导入 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

语法如下:

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 字符集用如下方法:

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];"

html

网址:153.导入文本文件时如何指定字段类型 https://www.yuejiaxmz.com/news/view/1127013

相关内容

电脑文档如何找备份文件 电脑文档备份文件查找指南
如何快速录入文字?苹果6splus参数配置
文件管理小贴士:批量导入助你节省时间与精力
文件夹如何分类管理更高效?快速整理文件夹的3个方法
在MFC中如何选择文件名和文件夹
【Shp文件空间分析详解】:深入空间分析,优化Shp文件分割的应用技巧
怎样才能实现电脑每天定时打开指定文件?6个定时方法帮你实现
大模型时代下的数字安全答案,藏在一本实战指南里
科大讯飞如何导入文件?轻松掌握智能设备的文件管理技巧!
清理云备份文件 高效指南:如何清理和优化您的云备份文件空间

随便看看