简体   繁体   English

Powershell与Excel

[英]Powershell with Excel

What I need to do is to extract the data in the excel row and output them into different rows on Excel. 我需要做的是提取excel行中的数据并将其输出到Excel的不同行中。 After that, I will need to use the extracted data and perform certain conditions on the extracted data. 之后,我将需要使用提取的数据并对提取的数据执行某些条件。

This is my current script: 这是我当前的脚本:

  1. To open excel and apply the formulas 打开Excel并应用公式
 $excel = New-Object -ComObject excel.application $filepath = 'D:\\testexcel.xlsx' $workbook = $excel.workbooks.open("$filepath") $worksheet = $workbook.worksheets.item(1) $excel.Visible = $true $rows = $worksheet.range("A1").currentregion.rows.count $worksheet.range("S1:S$rows").formula = $worksheet.range("S1").formula 
  1. Function to find the row, apply the formula and output it 查找行,应用公式并将其输出的函数
 function test123(){ param([string]$test123) $sourcefile = "D:\\testexcel.xlsx" $sheetname = "abc" $excel = new-object -comobject excel.application $excel.Visible = $true $excelworkbook = $excel.Workbooks.open($sourcefile, 2, $true) $excelworksheet = $excelworkbook.worksheets.item($sheetname) $row = 1 $column = 1 $found = $false while(($excelworksheet.cells.item($row, $column).value() -ne $null) -and($found -eq $false)){ if(($excelworksheet.cells.item($row, $column).value()).toupper() -eq $test123.ToUpper()){ write-host $excelworksheet.cells.item($row, $column).value() $excelworksheet.cells.item($row, $column+1).value(), $excelworksheet.cells.item($row, $column +2).value() $found = $true } $row += 1 } #close workbook $excelworkbook.close() $excel.quit() } test123 -test123 "Test123" 

Please guide me and tell me if this is the right way to do it... Thanks 请指导我,告诉我这是否是正确的方法...谢谢

Please have a look into the ImportExcel module by Douge Finke. 请查看Douge Finke的ImportExcel模块。 This module has the capability to do what you need. 该模块具有执行所需功能的能力。

Get it from PowerShell gallery: Install-Module -Name ImportExcel 从PowerShell库中获取它: Install-Module -Name ImportExcel

Github link: https://github.com/dfinke/ImportExcel Github链接: https : //github.com/dfinke/ImportExcel

you can then do Get-Help Import-Excel -Examples which has pretty good examples. 然后,您可以执行Get-Help Import-Excel -Examples ,其中包含非常好的示例。

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

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