【问题标题】:How to use Aspose Cells dll in Powershell Script如何在 Powershell 脚本中使用 Aspose Cells dll
【发布时间】:2020-07-01 09:43:55
【问题描述】:

我是 powershell 新手,我想在 excel 上执行一些操作。我可以通过使用 Com Object 来做到这一点。但我不想使用 Com Object ,而不是尝试使用 aspose cells dll。

能否请您帮助我如何在我的 powershell 脚本中导入或添加这个 dll。

下面是使用 Com Object 的代码,我想用 aspose dll 重写它。

$Source = 'D:\Test'  # the path to where the Excel files are

# create an Excel COM object
$excel  = New-Object -ComObject Excel.Application 

# find Excel files in the Source path and loop through.
# you may want to add the -Recurse switch here if the code should also look inside subfolders
$result = Get-ChildItem -Path $Source -Filter '*.xlsx' -File | ForEach-Object {
    $workBook  = $excel.Workbooks.Open($_.FullName)
    $workSheet = $Workbook.Sheets.Item(1)
    $count     = 0
    $label = $workSheet.Range('$A:$B').Find('*Animal count:*')
    if ($label) {
        # get the numeric value for the cell next to the label
        # empty cells will translate to 0
       $count = [int]$workSheet.Cells.Item($label.Row, $label.Column +2 ).Value()
    }
    # output a PSObject with the full filename and the animal count value
    [PsCustomObject] @{
        'File'        = $_.FullName
        'AnimalCount' = $count
    }
    $workBook.Close()
}

# quit Excel and clean up the used COM objects
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workSheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workBook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()


# output on screen
$result | Format-Table -AutoSize

#output to CSV file
$result | Export-Csv -Path 'D:\Test\AnimalCount.csv' -UseCulture -NoTypeInformation
e

【问题讨论】:

    标签: powershell aspose aspose-cells


    【解决方案1】:

    通常,您可以在 PowerShell 中使用 .NET 库。 Aspose.Cells for .NET 是用托管 C#.NET 编写的,因此它是一个 .NET 程序集,也可以使用标准方法在 PowerShell 环境中使用(与使用自定义 .NET 库的方式类似)。例如,请访问此link,了解如何在 PowerShell 中使用 .NET 程序集。请注意,您必须自己适应/转换为 PowerShell 脚本。

    关于您的 COM 互操作。示例代码段,请访问以下使用 Aspose.Cells API 的文档链接,供您执行不同任务时参考:

    例如 (如何打开不同的 MS Excel 文件格式。) https://docs.aspose.com/display/cellsnet/Opening+Different+Microsoft+Excel+Versions+Files

    (如何在工作表中查找或搜索数据) https://docs.aspose.com/display/cellsnet/Find+or+Search+Data

    (如何转换为 CSV 文件格式) https://docs.aspose.com/display/cellsnet/Different+Ways+to+Save+Files#DifferentWaystoSaveFiles-SavingTextFileswithCustomSeparator

    希望,这有点帮助。

    PS。我在 Aspose 担任支持开发人员/布道者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多