简体   繁体   中英

Read contents of an Excel file using PowerShell and store it in a SQL Server database

I am trying to write the contents of an Excel file (on tab 5) to a database but it keeps failing.

Here is my code:

 $dir = "\\server\files\"
    $latest = Get-ChildItem -Path $dir | Where-Object {$_.name -like "*Data Notes*"} | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    Write-Output "The latest file is: $latest"
    Write-SqlTableData -ServerInstance "sql server" -DatabaseName "sql5" -SchemaName dbo -TableName Temptable100 -InputData $latest.FullName -Force

The table gets created fine but its empty. Can anyone please help me??

I have attached the contents of the Excel file here:

在此处输入图片说明

Write-SQLTableData将无法读取Excel文件,因此您将需要使用OLEDB之类的东西首先将Excel表读取到DataTable中,然后将该DataTable传递给Write-SQLTableData。

All I needed was to store the information in a data variable and then use the Write-SQLTableData to store the information in a database. See below:

Write-SqlTableData -ServerInstance "instance" -DatabaseName "db1" -SchemaName dbo -TableName TableName -InputData $DataTable -Force

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM