12

MS Excel経由でドキュメントとやり取りしていますPowershell。各 Excel ドキュメントには、約 1000 行のデータがある可能性があります。

現在、このスクリプトはファイルを読み取り、Excel0.6 秒ごとに 1 レコードの速度で画面に値を書き込むようです。一見、非常に遅いように見えます。

Excelでファイルを読み取るのはこれが初めてですがPowershell、これは標準ですか? データを読み取って解析するためのより高速な方法はありExcelますか?

これがスクリプト出力です(読みやすくするためにトリミングされています)

PS P:\Powershell\ExcelInterfaceTest> .\WRIRMPTruckInterface.ps1 test.xlsx
3/20/2013 4:46:01 PM
---------------------------
2   078110
3   078108
4   078107
5   078109
<SNIP>
242   078338
243   078344
244   078347
245   078350
3/20/2013 4:48:33 PM
---------------------------
PS P:\Powershell\ExcelInterfaceTest>

Powershellスクリプトは次のとおりです。

########################################################################################################
# This is a common function I am using which will release excel objects
########################################################################################################
function Release-Ref ($ref) {
    ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

########################################################################################################
# Variables
########################################################################################################

########################################################################################################
# Creating excel object
########################################################################################################
$objExcel = new-object -comobject excel.application 

# Set to false to not open the app on screen.
$objExcel.Visible = $False

########################################################################################################
# Directory location where we have our excel files
########################################################################################################
$ExcelFilesLocation = "C:/ShippingInterface/" + $args[0]

########################################################################################################
# Open our excel file
########################################################################################################
$UserWorkBook = $objExcel.Workbooks.Open($ExcelFilesLocation) 

########################################################################################################
# Here Item(1) refers to sheet 1 of of the workbook. If we want to access sheet 10, we have to modify the code to Item(10)
########################################################################################################
$UserWorksheet = $UserWorkBook.Worksheets.Item(2)

########################################################################################################
# This is counter which will help to iterrate trough the loop. This is simply a row counter
# I am starting row count as 2, because the first row in my case is header. So we dont need to read the header data
########################################################################################################
$intRow = 2

$a = Get-Date
write-host $a
write-host "---------------------------"

Do {

    # Reading the first column of the current row
    $TicketNumber = $UserWorksheet.Cells.Item($intRow, 1).Value()

    write-host $intRow " " $TicketNumber    

    $intRow++

} While ($UserWorksheet.Cells.Item($intRow,1).Value() -ne $null)

$a = Get-Date
write-host $a
write-host "---------------------------"

########################################################################################################
# Exiting the excel object
########################################################################################################
$objExcel.Quit()

########################################################################################################
#Release all the objects used above
########################################################################################################
$a = Release-Ref($UserWorksheet)
$a = Release-Ref($UserWorkBook) 
$a = Release-Ref($objExcel)
4

2 に答える 2

11

Robert M. Toups, Jr. は、自身のブログ エントリPowerShell での Excel ファイルの読み取りを高速化する で、PowerShell への読み込みは高速ですが、実際の Excel セルの読み取りは非常に遅いと説明しています。一方、PowerShell はテキスト ファイルを非常に高速に読み取ることができるため、彼の解決策は、スプレッドシートを PowerShell に読み込み、Excel のネイティブ CSV エクスポート プロセスを使用して CSV ファイルとして保存し、PowerShell の標準Import-Csvコマンドレットを使用してデータを非常に高速に処理することです。 . 彼は、これによりインポート プロセスが最大 20 倍速くなったと報告しています。

Toups のコードを利用して、Import-Excelスプレッドシート データを非常に簡単にインポートできる関数を作成しました。私のコードは、既定のワークシート (つまり、ファイルを保存した時点でアクティブなシート) を使用するだけでなく、Excel ワークブック内の特定のワークシートを選択する機能を追加します。–SheetNameパラメータを省略すると、デフォルトのワークシートが使用されます。

function Import-Excel([string]$FilePath, [string]$SheetName = "")
{
    $csvFile = Join-Path $env:temp ("{0}.csv" -f (Get-Item -path $FilePath).BaseName)
    if (Test-Path -path $csvFile) { Remove-Item -path $csvFile }

    # convert Excel file to CSV file
    $xlCSVType = 6 # SEE: http://msdn.microsoft.com/en-us/library/bb241279.aspx
    $excelObject = New-Object -ComObject Excel.Application  
    $excelObject.Visible = $false 
    $workbookObject = $excelObject.Workbooks.Open($FilePath)
    SetActiveSheet $workbookObject $SheetName | Out-Null
    $workbookObject.SaveAs($csvFile,$xlCSVType) 
    $workbookObject.Saved = $true
    $workbookObject.Close()

     # cleanup 
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbookObject) |
        Out-Null
    $excelObject.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelObject) |
        Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()

    # now import and return the data 
    Import-Csv -path $csvFile
}

これらの補助関数は、Import-Excel で使用されます。

function FindSheet([Object]$workbook, [string]$name)
{
    $sheetNumber = 0
    for ($i=1; $i -le $workbook.Sheets.Count; $i++) {
        if ($name -eq $workbook.Sheets.Item($i).Name) { $sheetNumber = $i; break }
    }
    return $sheetNumber
}

function SetActiveSheet([Object]$workbook, [string]$name)
{
    if (!$name) { return }
    $sheetNumber = FindSheet $workbook $name
    if ($sheetNumber -gt 0) { $workbook.Worksheets.Item($sheetNumber).Activate() }
    return ($sheetNumber -gt 0)
}
于 2013-03-21T14:41:00.537 に答える
9

データが静的な場合 (数式は関係なく、セル内のデータのみ)、スプレッドシートに ODBC データ ソースとしてアクセスし、それに対して SQL (または少なくとも SQL に似た) クエリを実行できます。接続文字列の設定については、このリファレンスを参照してください (ワークブック内の各ワークシートは、この演習では「テーブル」になります)。通常System.Dataデータベースと同じようにクエリを実行します (Don Jones がこのためのラッパー関数を作成しました)。これが役立つ場合があります)。

これ、Excel を起動してセルごとに選択するよりも高速です。

于 2013-03-21T01:05:03.090 に答える