简体   繁体   English

使用Powershell从sql表将数据写入excel

[英]write data into excel using powershell from sql table

I have a .xlsx file which was made into data table by oledb provider.Now I want to add value to that .xlsx based on the sql table data I have 我有一个.xlsx文件,由oledb provider制成数据表。现在我想根据我拥有的sql表数据向该.xlsx添加值
(which is also converted into a csv file Book1.csv ) (也将其转换为一个csv文件Book1.csv

The sql table consists of name and notes... sql表由名称和注释组成。
Where name column is same in both .xlsx file and sql variable $sql 其中.xlsx文件和sql变量$sql name列相同

I want to add that close notes to f column of .xlsx file if the value of name matches with the value of sql table "A" column One I wrote below is very slow and not effective. 如果name的值与sql table“ A”列的值匹配,我想在.xlsx文件的f列中添加结束注释,我在下面写的一个非常慢且无效。
Any help would be highly appreciated. 任何帮助将不胜感激。

$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)
}
}

Data in .xlsx file .xlsx文件中的数据

Number  Priority  Comment
612721  4 - High

Data in Book1.csv Book1.csv中的数据

Number Clo_notes
612721 Order has been closed

I need to update clo_notes value to comment in .xlsx file if this "number" column in each file matches update the clos_notes to the corresponding column of comment 如果每个文件中的此“数字”列匹配,我需要更新clo_notes值以在.xlsx文件中进行注释,将clos_notes更新为相应的注释列

It looks like you answered my question about where "Nebraska" falls into the data. 您似乎回答了我有关“内布拉斯加州”在数据中的位置的问题。

Make sure to release any COM objects, or you'll have orphaned Excel processes. 确保释放所有COM对象,否则您将具有孤立的Excel进程。

You might try something like this. 您可以尝试这样的事情。 I was able to write the Clo_notes value into column 6 as you were requesting: 我可以按照您的要求将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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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