【问题标题】:How can I copy a column value from excel to txt file如何将列值从excel复制到txt文件
【发布时间】:2020-05-19 20:26:02
【问题描述】:

我是 Power shell 的新手。我有许多 excel 文件(500+),其中有一列 Animal Count,我想将其保存在一个新的“.txt”文件中。谁能给我一些提示来实现这一点。

【问题讨论】:

  • 您的意思是您需要将“动物计数”列中的值保存为文本还是整个工作表?
  • 我只需要Animal Count列数据,主要目的是检查空白报告。即,Excel 文件中的 Animal Count 为 Null。
  • 请您向我们展示这些文件之一(部分)。您是否需要一份关于该列中所有数字总计为 0 的汇总报告?
  • 我已添加图片以供参考
  • 感谢西奥,您的帮助。我需要再添加一个逻辑。我在文件夹中有 1000 个 excel 文件。但这应该根据修改日期进行扫描。比如我今天生成了报告,这个脚本应该扫描今天使用修改日期创建的excel文件。

标签: powershell powershell-3.0


【解决方案1】:

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

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

试试:

$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

屏幕上的结果将如下所示:

文件动物计数 ---- ----------- 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()
}

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

附注如果动物计数值可能超过 2147483647,则转换为 [int64] 而不是 [int]

【讨论】:

  • 我将所有文件的动物计数设为 0,不知道我在哪里丢失。你能帮帮我吗?
  • @NagarajuKukudala 你能检查是否所有文件都有这个标签Animal count: 它必须是一个完全匹配。顺便说一句,您为什么删除图像?它很好地解释了标签在第一列。请把它放回去。
  • 好的,谢谢,这里的问题是 Animal Count 标签是一个合并的单元格,所以 cells.Find 没有选择它。我们也可以使用 com 对象来实现这一点吗?
  • 该脚本应该在生产服务器中使用,所以 com 对象将不起作用.. 请您在不使用 com 对象的情况下帮助我。感谢您的帮助!
  • @NagarajuKukudala 我添加了新代码以在合并单元格中查找标签。最新评论是您应该在原始问题中提及的信息,而不是在回答后,因为使用OLEDB 会采用完全不同的方法。
【解决方案2】:

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

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

【讨论】:

  • Import-Csv 不能做 Excel 文件。
【解决方案3】:

试试这个,这是为了将 1 个文件保存到同名的 txt。 您可以使用 foreach 选项来获得更多文件

$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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2020-10-19
    相关资源
    最近更新 更多