2016-03-18 15 views
1

Birden çok sayfalı sku ve miktar sütunuyla doldurulmuş bir excel dosyasına sahibim. Tüm çalışma kitabını birden çok öğe için aramalı ve miktarlarını iade etmeliyim. products.txt, sku kimliklerini içerir. Excel'de birden çok öğeyi arayın ve tüm ortamı geri döndürün

ABC1234 
BCDH214 
LDJI983 

Ve Excel çalışma kitabı

, inventory.xlsx ben inventory.xlsx karşı prodcuts.txt koşmak ve her ürünün miktarı dönmek istiyorum aşağıdaki sütunları

**sku**     ***Quantity*** 
ABC1234      2 
BCDH214      0  
LDJI983      1 

içerir.

Bu, powershell yoluyla yapılabilir mi? veya bu tür bir sorguyu çalıştırmanın başka bir yolu var mı?

+0

Yani, 'products.txt' ana dosyasıdır ve Excel'de bakmak istiyorum: tamlığı için

, ben burada yukarıda belirtilen MSDN blogundan örnek kod kopyaladık Tüm sayfalar için .txt 'deki her bir SKU için ve excel dosyasında görüntülenme sayısını saymak ister misiniz? – BruceWayne

+0

Hey! Hayır, envanter.xlsx'e karşı prodcuts.txt dosyasını çalıştırmak ve her bir ürünün miktarını iade etmek istiyorum. –

+0

Yani, '.txt''deki her satır için, Excel belgesindeki tüm sayfaları taramak ve her sayfadaki" Miktar "sütununa sayıları eklemek ister misiniz? – BruceWayne

cevap

1

Get Excel data without Excel numaralı belgede gösterilen kodu kullanın ve ACE.OLEDB provider'un yüklü olduğundan emin olun.

SKU Quantity 
one 1 
two 4 
three 9 

Sonra Excel'e seslendi::

Ben basit bir xlsx yarattı

$path = 'd:\test\exceldb.xlsx' 
$results = Get-ExcelData -Path $path -Query 'SELECT * FROM [Sheet1$]' 

$stuffFromProductsTxtFile = 'one', 'two', 'three' 

foreach ($sku in $stuffFromProductsTxtFile) 
{ 
    $results.Rows | Where-Object {$_.SKU -eq $sku} | % {Write-Output "$($_.SKU) has quantity $($_.Quantity)"} 
} 

Bu aşağıdaki çıktıyı verir: Ben bu konuda düşünmek

one has quantity 1 
two has quantity 4 
three has quantity 9 

, sen Gereksinim duyduğunuz her şeye göre değişebilir. (

function Get-ExcelData { 
    [CmdletBinding(DefaultParameterSetName='Worksheet')] 
    Param(
     [Parameter(Mandatory=$true, Position=0)] 
     [String] $Path, 

     [Parameter(Position=1, ParameterSetName='Worksheet')] 
     [String] $WorksheetName = 'Sheet1', 

     [Parameter(Position=1, ParameterSetName='Query')] 
     [String] $Query = 'SELECT * FROM [Sheet1$]' 
    ) 

    switch ($pscmdlet.ParameterSetName) { 
     'Worksheet' { 
      $Query = 'SELECT * FROM [{0}$]' -f $WorksheetName 
      break 
     } 
     'Query' { 
      # Make sure the query is in the correct syntax (e.g. 'SELECT * FROM [SheetName$]') 
      $Pattern = '.*from\b\s*(?<Table>\w+).*' 
      if($Query -match $Pattern) { 
       $Query = $Query -replace $Matches.Table, ('[{0}$]' -f $Matches.Table) 
      } 
     } 
    } 

    # Create the scriptblock to run in a job 
    $JobCode = { 
     Param($Path, $Query) 

     # Check if the file is XLS or XLSX 
     if ((Get-Item -Path $Path).Extension -eq 'xls') { 
      $Provider = 'Microsoft.Jet.OLEDB.4.0' 
      $ExtendedProperties = 'Excel 8.0;HDR=YES;IMEX=1' 
     } else { 
      $Provider = 'Microsoft.ACE.OLEDB.12.0' 
      $ExtendedProperties = 'Excel 12.0;HDR=YES' 
     } 

     # Build the connection string and connection object 
     $ConnectionString = 'Provider={0};Data Source={1};Extended Properties="{2}"' -f $Provider, $Path, $ExtendedProperties 
     $Connection = New-Object System.Data.OleDb.OleDbConnection $ConnectionString 

     try { 
      # Open the connection to the file, and fill the datatable 
      $Connection.Open() 
      $Adapter = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $Query, $Connection 
      $DataTable = New-Object System.Data.DataTable 
      $Adapter.Fill($DataTable) | Out-Null 
     } 
     catch { 
      # something went wrong :-(
      Write-Error $_.Exception.Message 
     } 
     finally { 
      # Close the connection 
      if ($Connection.State -eq 'Open') { 
       $Connection.Close() 
      } 
     } 

     # Return the results as an array 
     return ,$DataTable 
    } 

    # Run the code in a 32bit job, since the provider is 32bit only 
    $job = Start-Job $JobCode -RunAs32 -ArgumentList $Path, $Query 
    $job | Wait-Job | Receive-Job 
    Remove-Job $job 
} 
+0

Teşekkürler! Ona bakıp haber vereceğim! –

+0

Yukarıdaki çalışmaları zaten biliyoruz. Başka neler sunabileceğime emin değilim ... –

+0

Bu sorununuzu çözdünüz mü? –

İlgili konular