我是PowerShell的新手。请帮我说明如何实现以下功能:使用PowerShell在xls文件中显示每行SQL Server上的数据。
read xls > powershell > sql server
下面是我到目前为止创建的代码,但事实证明只有一行数据显示在数据库中。如何循环每行的输出?
$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()
答案 0 :(得分:0)
我已经得到了我的问题的答案。谢谢顺便说一下。 :)
$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()