【问题标题】:Search for multiple items in excel and return the entire feild在excel中搜索多个项目并返回整个字段
【发布时间】:2016-03-18 21:00:09
【问题描述】:

我有一个包含多个工作表的 excel 文件,其中填充了 sku 和数量列。我需要在整个工作簿中搜索多个项目并返回它们的数量。 products.txt 包含 sku ID。

ABC1234
BCDH214
LDJI983

Excel 工作簿,inventory.xlsx 包含以下列

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

我想针对 inventory.xlsx 运行 prodcuts.txt 并返回每个产品的数量。

这可以通过 powershell 完成吗?或任何其他方式来运行这种查询?

【问题讨论】:

  • 所以,products.txt 是您的主文件,您要在 Excel(所有工作表)中查看 .txt 中的每个 SKU,并计算它在 Excel 文件中出现的次数?
  • 嘿!不,我想针对 inventory.xlsx 运行 prodcuts.txt 并返回每个产品的数量。
  • 那么,对于.txt 中的每一行,您想扫描Excel doc. 中的所有工作表,并将每张工作表上可能包含的“数量”列中的数字相加吗?跨度>
  • 是的,但不累加数量。我想看看 products.txt 中每个项目的数量。

标签: excel powershell vba


【解决方案1】:

使用此处显示的代码Get Excel data without Excel 并确保您已安装ACE.OLEDB provider

我创建了一个简单的 xlsx:

SKU    Quantity
one    1
two    4
three  9

然后我调用了 Excel:

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

这给出了以下输出:

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

我认为有了这个,您可以根据需要进行相应更改。

为了完整性,我在此处复制上述 MSDN 博客中的示例代码:

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
}

【讨论】:

  • 谢谢!我会调查并告诉你!
  • 太好了,请标记为答案,以便其他搜索此问题的人看到该问题有解决方案。
  • 嘿,为什么我突然收到“'Get-ExcelData' 一词未被识别为 cmdlet 的名称”。我正在使用 Powershell v5
猜你喜欢
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
相关资源
最近更新 更多