[英]Display data on SQL Server of each row from an xls file by using powershell
I'm new to PowerShell. 我是PowerShell的新手。 Please help me on how to achieve the following: display data on SQL Server of each row from an xls file by using PowerShell. 请帮助我实现以下目标:使用PowerShell在xls文件的每行的SQL Server上显示数据。
read xls > powershell > sql server
Below is the code I've created so far but turns out only one row of data is displayed in database. 以下是我到目前为止创建的代码,但结果表明数据库中仅显示一行数据。 How do I loop the output of each row? 如何循环输出每一行?
$filepath = "C:\Source\AandPrates.xls"
#object to open excel workbook
$excel= New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($filepath)
$worksheet = $workbook.Worksheets.Item(1)
$startRow = 4
#create system.datatable
#read xls file line by line and display on powershell
$dt = New-Object "System.Data.DataTable"
[void]$dt.Columns.Add("Currency Name",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("ISO Code",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("Accounting Rates",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("Base Currency",
[System.Type]::GetType("System.String"))
Do {
$ColValues1 = $worksheet.Cells.Item($startRow,1).Value()
$ColValues2 = $worksheet.Cells.Item($startRow,2).Value()
$ColValues3 = $worksheet.Cells.Item($startRow,3).Value()
$ColValues4 = $worksheet.Cells.Item($startRow,4).Value()
$startRow++
$dt.Rows.Add($ColValues1,$ColValues2,$ColValues3,$ColValues4)
}
while ($worksheet.Cells.Item($startRow,1).Value() -ne $null)
$name = $ColValues1
$code = $ColValues2
$rates = $ColValues3
$currency = $ColValues4
$Excel.Quit()
#Connect to the SQL database
$Conn = New-Object System.Data.SqlClient.SqlConnection
$Conn.ConnectionString = "Server=5CG61730W6\SQLEXPRESS;Database=shamsuli;trusted_connection=true;"
$Conn.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Conn
#insert the output from powershell into sql server
$output | foreach {
$Command.CommandText = "INSERT INTO dbo.MyTable (Name,Code,Rates,Currency) VALUES (@Name, @Code, @Rates, @Currency)"
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Name", $name)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Code", $code)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Rates", $rates)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Currency", $currency)));
$Command.ExecuteNonQuery() | out-null
}
$Conn.Close()
I already get the answer for my question. 我已经得到了我问题的答案。 Thanks btw. 谢谢顺便说一句。 :) :)
$filepath = "C:\Source\AandPrates.xls"
#object to open excel workbook
$excel= New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($filepath)
$worksheet = $workbook.Worksheets.Item(1)
$startRow = 2
#create system.datatable
$dt = New-Object "System.Data.DataTable"
[void]$dt.Columns.Add("Currency Name",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("ISO Code",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("Accounting Rates",
[System.Type]::GetType("System.String"))
[void]$dt.Columns.Add("Base Currency",
[System.Type]::GetType("System.String"))
Do {
$name = $ColValues1
$code = $ColValues2
$rates = $ColValues3
$currency = $ColValues4
$ColValues1 = $worksheet.Cells.Item($startRow,1).Value()
$ColValues2 = $worksheet.Cells.Item($startRow,2).Value()
$ColValues3 = $worksheet.Cells.Item($startRow,3).Value()
$ColValues4 = $worksheet.Cells.Item($startRow,4).Value()
$startRow++
$dt.Rows.Add($ColValues1,$ColValues2,$ColValues3,$ColValues4)
#Connect to the SQL database
$Conn = New-Object System.Data.SqlClient.SqlConnection
$Conn.ConnectionString = "Server=5CG61730W6\SQLEXPRESS;Database=shamsuli;trusted_connection=true;"
$Conn.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Conn
foreach ($worksheet in $excel) {
$Command.CommandText = "INSERT INTO dbo.MyTable1000 (Name,Code,Rates,Currency) VALUES (@Name, @Code, @Rates, @Currency)"
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Name", $name)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Code", $code)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Rates", $rates)));
$Command.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Currency", $currency)));
$Command.ExecuteNonQuery();
}
$Conn.Close()
}
while ($worksheet.Cells.Item($startRow,1).Value() -ne $null)
$Excel.Quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.