如何将列值从 excel 复制到 txt 文件

How can I copy a column value from excel to txt file

我是 Power 的新手 shell。我有许多 excel 文件(500+),其中有一列动物计数,我想将其保存在新的“.txt”文件中。任何人都可以给我一些技巧来实现这一点。

您可以使用 Import-Csv 将 excel 文件转换为 PS 对象,并且列将是新对象的属性。

$excel = Import-Csv $excelPath
$excel.Animals | out-file $txtPath

试试这个,这是为 1 个文件保存到同名的 txt。 您可以使用 foreach options

完成更多文件吗
$FileName = "C:\temp\test.xlsx"
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.DisplayAlerts = $false
$WorkBook = $Excel.Workbooks.Open($FileName)
$NewFilePath = [System.IO.Path]::ChangeExtension($FileName,".txt")
$Workbook.SaveAs($NewFilepath, 42)   # xlUnicodeText
  # cleanup
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

查看您提供的图片,计数值不在名为 'Animal count' 的列中,而是在带有该文本的 标签 旁边的列中。

至于输出类型,我建议不要使用 .txt 文件,而是将找到的信息输出为 CSV 文件,以便以结构化的方式保存文件名和动物计数值。

尝试:

$Source = 'D:\Test'  # the path to where the Excel files are

# create an Excel COM object
$excel  = New-Object -ComObject Excel.Application 

# find Excel files in the Source path and loop through.
# you may want to add the -Recurse switch here if the code should also look inside subfolders
$result = Get-ChildItem -Path $Source -Filter '*.xlsx' -File | ForEach-Object {
    $workBook  = $excel.Workbooks.Open($_.FullName)
    $workSheet = $Workbook.Sheets.Item(1)
    $count     = 0
    $label     = $WorkSheet.Cells.Find('*Animal count*')
    if ($label) {
        # get the numeric value for the cell next to the label
        # empty cells will translate to 0
        $count = [int]$workSheet.Cells.Item($label.Row, $label.Column + 1).Value()
    }
    # output a PSObject with the full filename and the animal count value
    [PsCustomObject] @{
        'File'        = $_.FullName
        'AnimalCount' = $count
    }
    $workBook.Close()
}

# quit Excel and clean up the used COM objects
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workSheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workBook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()


# output on screen
$result | Format-Table -AutoSize

#output to CSV file
$result | Export-Csv -Path 'D:\Test\AnimalCount.csv' -UseCulture -NoTypeInformation

屏幕上的结果如下所示:

File               AnimalCount
----               -----------
D:\Test\File1.xlsx         165
D:\Test\File2.xlsx           0
D:\Test\File3.xlsx       87596


编辑

由于您评论过标签位于 合并单元格 中,因此您需要使用它来查找 Animal count:

的值
$label = $workSheet.Range('$A:$B').Find('*Animal count*')

if ($label) {
    # get the numeric value for the cell next to the label
    # empty cells will translate to 0
    $count = [int]$workSheet.Cells.Item($label.Row, $label.Column + 2).Value()
}

假设有两个单元格合并为一个单元格。

P.S。如果动物计数值可以超过 2147483647,则转换为 [int64] 而不是 [int]