无法通过 PowerShell 从 Excel 导入日期列

时间:2021-06-09 10:44:17

标签: sql-server excel powershell

我正在尝试通过下面的 PowerShell 脚本提取 Excel 工作表,稍后我需要将其写入 SQL Server。 “报告日期”列无法导入任何数据。 某个地方的 sytax 有问题吗?我附上了我的 excel 表的样子。

Excel Sheet

$My_File_Date = $My_FileIN.BaseName.Replace("filename_","").Replace("_","")
$My_Formatted_Date = [datetime]::ParseExact($My_File_Date,"yyyyMMdd",$null).ToString("dd/MM/yyyy")
$My_Fields_File = "$Headers_DIR\My_Fields.txt"
$My_Fields = @{}
$My_Fields = (Get-content "$My_Fields_File" -Delim ",").Replace(",","")
Convert-xlsToxlsx $My_FileIN $My_Converted_File
$My_Data = Import-XLSX -Path $My_Converted_File -Sheet 1 -WarningAction SilentlyContinue
$My_Count = $My_Data.Count
$My_Data | Add-Member -MemberType NoteProperty -Name Distributor -Value "Name Co."
$My_Data | Add-Member -MemberType NoteProperty -Name "File Date" -Value $My_Formatted_Date
Write-Host "File imported - $My_Count records found ... " -NoNewline
Write-Host "Converting to Data Table"
$My_DataTable = 

 ($My_Data |
    Select-Object -Property Distributor,'File Date',@{l='Report Date';e={If ($_.'Report Date' -as [datetime]){$_.'[datetime]::ParseExact("Report Date","dd.MM.yyyy",$null).ToString("dd/MM/yyyy")'} else {[DBNULL]::Value}}},Country
  )





Distributor File Date  Report Date Country
----------- ---------  ----------- -------
Name Co.    09/06/2021             US     
Name Co.    09/06/2021             UK     
Name Co.    09/06/2021             GE

0 个答案:

没有答案
相关问题