我尝试将CSV文件加载到Access数据库,使用ANSI文件一切正常,但是使用UTF-8时出现错误。适用于ANSI文件的代码:
function Import-MsAccessCsv
{
param (
[Parameter(Mandatory = $True)] [__ComObject] $Access,
[Parameter(Mandatory = $True)] [string] $Path,
[Parameter(Mandatory = $True)] [string] $TableName,
[Parameter(Mandatory = $False)] [switch] $HasFieldNames,
[Parameter(Mandatory = $False)] [string] $SpecificationName=$null
)
$transferType = 0
$DoCmd = $Access.DoCmd
$DoCmd.TransferText( $transferType, $SpecificationName, $TableName, $Path, [bool]$HasFieldNames )
}
试图将代码页添加到TransferText:
function Import-MsAccessCsv
{
param (
[Parameter(Mandatory = $True)] [__ComObject] $Access,
[Parameter(Mandatory = $True)] [string] $Path,
[Parameter(Mandatory = $True)] [string] $TableName,
[Parameter(Mandatory = $False)] [switch] $HasFieldNames,
[Parameter(Mandatory = $False)] [string] $HTMLTableName=$null,
[Parameter(Mandatory = $False)] [string] $CodePage="65001",
[Parameter(Mandatory = $False)] [string] $SpecificationName=$null
)
$transferType = 0
$DoCmd = $Access.DoCmd
$DoCmd.TransferText( $transferType, $SpecificationName, $TableName, $Path, [bool]$HasFieldNames, $HTMLTableName, $CodePage )
}
错误:
字段''在目标表“ voters”中不存在。在C:\ Users \ Nobody \ Desktop \ ms-access.ps1:24 char:5+ $ DoCmd.TransferText($ transferType,$ SpecificationName,$ TableNam ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~+ CategoryInfo:OperationStopped:(:) [],COMException+ FullyQualifiedErrorId:System.Runtime.InteropServices.COMException
您的代码页标识符是字符串,而应该是整数。
代码页标识符在这里列出:https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers。
UTF-8的适当代码页标识符是65001,为整数。
由于使用的代码页错误,Access无法识别导入规范中指定的字段名称,因此导入失败。