导出 Excel Powershell 模块无法传输所有列 - 格式

问题描述 投票:0回答:1

我正在执行 MySql DB 查询管道并使用结果将数据传输到存储帐户。 对于数据库查询并将数据传输到文件 .txt,我使用以下命令:

sqlcmd -S $(Server_prod) -i "G:\DB_Automation\xxx\Night_Batch_Report_4.sql" -o "G:\DB_Automation\SQL_Queries\xxx\4th_init.txt"

后来我使用该脚本将 .txt 文件中的所有内容传输为 .xlsx 格式。

- task: PowerShell@2
                  displayName: Tranfer 4st night job --> Excel
                  inputs:
                    targetType: 'inline'
                    script: |
                      $rawData = Get-Content -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\1st_init.txt' | Where-Object {$_ -match '\S'}
                      $delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator 
                      ($rawData -replace '\s+' , $delimiter) | Set-Content -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\theNewFile.csv'
                      Import-Csv -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\theNewFile.csv' | Export-Excel -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\batch_audit_report_$(Build.BuildNumber)_prod_Night.xlsx' -Autosize -WorkSheetname '04-Check all task completed'

这种方法的问题是,我可以看到 .txt 文件中要传输的内容比稍后传输到 .xlsx 中的内容多得多。 请参考截图:

部分栏目未转移。

请告诉我 PowerShell 脚本/格式是否存在问题?

谢谢

regex powershell module
1个回答
0
投票

问题

问题是您尝试从第一个屏幕截图中的原始数据中提取 *.csv 文件无法正常工作。

如果你从这个开始:

$lines = @"
Changed database context to 'GPF_PROD_DB'

(0 rows affected)
entry_time batch_run_num batch_name batch_id task_name task_id primary_key chunk_id
aaa bbb ccc ddd eee fff ggg hhh

(0 rows affected)
"@ -split "`n"

和你的代码:

$rawData = $lines | Where-Object {$_ -match '\S'}
$delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator 
($rawData -replace '\s+' , $delimiter)

你得到:

Changed,database,context,to,'GPF_PROD_DB'
(0,rows,affected)
entry_time,batch_run_num,batch_name,batch_id,task_name,task_id,primary_key,chunk_id
aaa,bbb,ccc,ddd,eee,fff,ggg,hhh
(0,rows,affected)

当您使用

Import-Csv
读回该内容时,它会从第一行获取列标题 - 只有 5 列,因此它会丢弃任何其他列中的数据(即
task_id
以后)。

将此数据导出到 Excel(或者甚至使用

Export-Csv
返回到 CSV)只会写入前 5 列中的数据,因为这就是导入的全部数据。

解决方法

您需要从源文本文件中提取表格数据行。

我不知道这种方法有多脆弱,但您可以拆分

(x rows affected)
并从文本文件中获取第二个结果 - 例如:

# use "Get-Content -Raw" to read the file as a single string in your code
$text = @"
Changed database context to 'GPF_PROD_DB'

(0 rows affected)
entry_time batch_run_num batch_name batch_id task_name task_id primary_key chunk_id
aaa bbb ccc ddd eee fff ggg hhh

(0 rows affected)
"@

$rawData = ($text -split "\(\d* rows affected\)")[1].Trim() -split "\n"

它的作用是:

  • ($text -split "\(\d* rows affected\)")
    - 根据格式“(受影响的 x 行)”的分隔符将整个文本文件拆分为块
  • [1]
    - 获取包含表格数据的第二个块(数组是零索引的,因此 1 是数组中的第二项)
  • .Trim()
    - 删除前导和尾随空格,包括换行符
  • -split "`n"
    - 将块分割成行

现在您可以像以前一样处理它:

$delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator

# no need to write to a file and re-import - just use ConvertFrom-Csv
$data = ($rawData -replace '\s+' , $delimiter) | ConvertFrom-Csv

$data | Export-Excel -Path ...

如果您的源文件格式发生更改,这可能会中断,但它有望为您指明正确的方向...

© www.soinside.com 2019 - 2024. All rights reserved.