excel 带参数FieldInfo的OpenText

zd287kbt  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(157)

我尝试使用Workbook.OpenText传递参数FieldInfo,但没有成功
我想用fieldinfo参数打开一个最近创建的csv文件(我也试过使用txt扩展名),这样我就可以在一些列上放置文本格式,但它一直给我以下错误:
您无法在Null值运算式上呼叫方法。
该代码
'$array = @(数组(数组(1,1),数组(2,1),数组(3,1),数组(4,1),数组(5,1),数组(6,1),数组(7,1),数组(8,1),数组(9,1),数组(10,2),数组(11,1))

$MyWorkbook = $Excel.workbooks.OpenText($file,2,1,1,1,$True,$True,$True,$False,$False,$False,$False,$array)
    
    $MyWorksheet = $MyWorkbook.WorkSheets.item(1)

`
非常感谢你的帮助

niwlg2el

niwlg2el1#

问题似乎在于,即使您指定逗号作为字段分隔符,Excel也始终会查看设置为系统ListSeparator..的内容。即使您指定“Other”和“OtherChar”参数使用逗号,它仍然会失败,并需要分号。
在我的荷兰机器上,它被设置为分号;,因此即使我的csv文件使用逗号,并且我将下面的参数设置为Comma,Excel也会将其“转换”为[cultureinfo]::CurrentCulture.TextInfo.ListSeparator中设置的任何内容
对于我来说,当我在输入CSV文件中使用分号字符作为分隔符时,这是有效的,所以我需要首先更改csv的格式:

$Delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator
$file = 'D:\Test\comma.csv'  # the original comma delimited csv
(Import-Csv -Path $file) | Export-Csv -Path $file -Delimiter $Delimiter -NoTypeInformation

下一个问题是方法OpenText()似乎没有返回任何内容,这解释了错误消息You cannot call a method on a null-valued expression

# define an array of two-element arrays, in which the first element is the column number (1-based), 
# and the second element is one of the XlColumnDataType constants specifying how the column is parsed.
# see: https://learn.microsoft.com/en-us/office/vba/api/excel.xlcolumndatatype
$FieldInfo = @(1, 1), @(2, 1), @(3, 1), @(4, 1), @(5, 1), @(6, 1), @(7, 1), @(8, 1), @(9, 1), @(10, 2), @(11, 1)

$Excel = New-Object -ComObject Excel.application
$Excel.DisplayAlerts = $false
$Excel.Visible = $true

# there are many parameters to the OpenText() method, but as we cannot use these as Named parameters
# we will have to provide all up to and including the FieldInfo parameter.
# note that for any variant parameter you wish to omit, we use [type]::Missing
$Excel.WorkBooks.OpenText($file,                # The absolute full file path
                          2,                    # Origin: xlWindows
                          1,                    # StartRow (default 1)
                          1,                    # DataType: xlDelimited
                          1,                    # TextQualifier: xlTextQualifierDoubleQuote
                          [type]::Missing,      # ConsecutiveDelimiter
                          [type]::Missing,      # Tab ($true if the file is Tab delimited; default = $false)
                          [type]::Missing,      # Semicolon ($true if the file is Semicolon delimited; default = $false)
                          [type]::Missing,      # Comma  ($true if the file is comma delimited; default = $false)
                          [type]::Missing,      # Space ($true if the file is space delimited; default = $false)
                          $true,                # Other ($true if the file delimited by the OtherChar; default = $false)
                          $Delimiter,           # OtherChar (Required if Other is True; Specifies the delimiter character)
                          $FieldInfo)

# now get the workbook and worksheet in variables
$MyWorkbook  = $Excel.WorkBooks.Item(1)
$MyWorkSheet = $MyWorkBook.WorkSheets.Item(1)

相关问题