使用powershell从sql表将数据写入excel
我有一个.xlsx文件,它是由oledb提供程序制作成数据表的。现在我想根据我拥有的sql表数据为该.xlsx添加值 它还转换为csv文件Book1.csv使用powershell从sql表将数据写入excel,sql,powershell,Sql,Powershell,我有一个.xlsx文件,它是由oledb提供程序制作成数据表的。现在我想根据我拥有的sql表数据为该.xlsx添加值 它还转换为csv文件Book1.csv Number Clo_notes 612721 Order has been closed sql表由名称和注释组成。。。 其中,name列在.xlsx文件和sql变量$sql中都是相同的 我想在.xlsx文件的f列中添加closenotes,如果name的值与sql表A的值匹配,那么我在下面编写的列A的速度非常慢,而且没有效果。 任何帮
Number Clo_notes
612721 Order has been closed
sql表由名称和注释组成。。。
其中,name列在.xlsx文件和sql变量$sql中都是相同的
我想在.xlsx文件的f列中添加closenotes,如果name的值与sql表A的值匹配,那么我在下面编写的列A的速度非常慢,而且没有效果。
任何帮助都将不胜感激
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open('C:\Users\VIKRAM\Documents\Sample - Superstore.xlsx')
$workSheet = $Workbook.Sheets.Item(1)
$WorkSheet.Name
$Found = $WorkSheet.Cells.Find('$Data.number')
$Found.row
$Found.text
$Excel1 = New-Object -ComObject Excel.Application
$file = $Excel1.Workbooks.Open('C:\Users\VIKRAM\Documents\Book1.xlsx')
$ff=$file.Sheets.Item(1)
$ff.Name
$ff1=$ff.Range("A1").entirecolumn
$ff1.Value2
foreach ($line in $ff1.value2){
if( $found.text -eq $line)
{
Write-Host "success"
$fff=$ff1.Row
$WorkSheet.Cells.item($fff,20) =$ff.cells.item($fff,2)
}
}
.xlsx文件中的数据
Number Priority Comment
612721 4 - High
Book1.csv中的数据
Number Clo_notes
612721 Order has been closed
我需要在.xlsx文件中更新clo_注释值以注释,如果每个文件中的此数字列匹配,请将clo_注释更新到相应的注释列看起来您回答了我关于内布拉斯加州在数据中的位置的问题 确保释放任何COM对象,否则您将有孤立的Excel进程 你可以试试这样的。我能够按照您的要求将Clo_notes值写入第6列:
## function to close all com objects
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
## open Excel data
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open('C:\Users\51290\Documents\_temp\StackOverflowAnswers\Excel.xlsx')
$workSheet = $Workbook.Sheets.Item(1)
$WorkSheet.Name
## open SQL data
$Excel1 = New-Object -ComObject Excel.Application
$file = $Excel1.Workbooks.Open('C:\Users\51290\Documents\_temp\StackOverflowAnswers\SQL.xlsx')
$sheetSQL = $file.Sheets.Item(1)
$dataSQL = $sheetSQL.Range("A1").currentregion
$foundNumber = 0
$row_idx = 1
foreach ($row in $WorkSheet.Rows) {
"row_idx = " + $row_idx
if ($row_idx -gt 1) {
$foundNumber = $row.Cells.Item(1,1).Value2
"foundNumber = " + $foundNumber
if ($foundNumber -eq "" -or $foundNumber -eq $null) {
Break
}
foreach ($cell in $dataSQL.Cells) {
if ($cell.Row -gt 1) {
if ($cell.Column -eq 1 -and $cell.Value2 -eq $foundNumber) {
$clo_notes = $sheetSQL.Cells.Item($cell.Row, 2).Value2
Write-Host "success"
$WorkSheet.Cells.item($row_idx, 6).Value2 = $clo_notes
}
}
}
}
$row_idx++
}
$Excel.Quit()
$Excel1.Quit()
## close all object references
Release-Ref($WorkSheet)
Release-Ref($WorkBook)
Release-Ref($Excel)
Release-Ref($Excel1)
你看过ImportExcel模块了吗?安装模块导入cel:@jrider挑战是由于安全限制,我无法在我的环境中安装任何模块。只是出于好奇,内布拉斯加州与您的数据结构有什么关系?@LotPings抱歉的输入错误,编辑了它。这提出了一个问题:在您使用$data.Numberis时,它是如何定义的?是的,这一逻辑有帮助!!。。。如果有10行呢?如何增加它是的,这个逻辑有帮助!!。。。如果有10行怎么办?如何增加itI修改了逻辑以循环遍历Excel数据并在SQL数据中查找数字列。我不是Excel中Range对象的专家,因此可能有更好/更快的方法来实现这一点。