【问题标题】:Powershell import CSV file with TransferTextPowershell 使用 TransferText 导入 CSV 文件
【发布时间】:2020-03-23 13:13:33
【问题描述】:

我尝试将 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)] [int] $CodePage=65001,
        [Parameter(Mandatory = $False)] [string] $SpecificationName=$null
    )

    $transferType = 0 
    $DoCmd = $Access.DoCmd
    $DoCmd.TransferText( $transferType, $SpecificationName, $TableName, $Path, [bool]$HasFieldNames, $HTMLTableName, $CodePage )
}

错误:

目标表“选民”中不存在字段“”。在 C:\Users\Nobody\Desktop\ms-access.ps1:24 char:5 + $DoCmd.TransferText($transferType, $SpecificationName, $TableNam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

【问题讨论】:

  • 您的 CSV 文件是用 UTF-8 with BOM 编码,还是 UTF-8 不带 BOM 编码?这有什么不同吗? (错误表明该文件在内容之前有一个 ByteOrderMark,而 Access 对此感到窒息)
  • 是的,带有 BOM 的 UTF-8
  • 无论如何,找到了将过程https://www.eileenslounge.com/viewtopic.php?f=29&t=25643保存在Access db模板中的方法

标签: powershell ms-access


【解决方案1】:

在所有者删除它的评论的帮助下找到了答案,所以很遗憾我不能给予信任。 HTMLTableName 必须是 [Type]::Missing 而不是 null

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,
        [Parameter(Mandatory = $False)] [int] $CodePage=65001,
        [Parameter(Mandatory = $False)] [string] $SpecificationName=$null
    )

    $transferType = 0 
    $DoCmd = $Access.DoCmd
    $DoCmd.TransferText( $transferType, $SpecificationName, $TableName, $Path, [bool]$HasFieldNames, [Type]::Missing, $CodePage )
}

$dbPath = "C:\Users\Nobody\Desktop\Mexico_1.accdb"
$access = New-Object -ComObject Access.Application
$access.OpenCurrentDatabase($dbPath)
$access.Visible = $true

# import csv file into a new table
Import-MsAccessCsv -Access $access -Path "D:\Data\Mexico-1.csv" -TableName 'voters' -HasFieldNames

# close access
$access.Quit()

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2015-10-15
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多