gpt4 book ai didi

excel - 如何在excel中读取单元格数据并输出到命令提示符

转载 作者:行者123 更新时间:2023-12-03 01:24:06 24 4
gpt4 key购买 nike

我是一名系统管理员,我正在尝试学习如何使用 powershell...我以前从未做过任何类型的脚本或编码,我一直通过从 technet 脚本中心和在线论坛学习在线自学。

我想要完成的是打开一个 excel 电子表格从中获取信息(用户名和密码),然后将其输出到 powershell 中的命令提示符中。每当我尝试这样做时,我都会收到一个调用“InvokeMember”的异常,这是我到目前为止的代码:


function Invoke([object]$m, [string]$method, $parameters)
{
$m.PSBase.GetType().InvokeMember(
$method, [Reflection.BindingFlags]::InvokeMethod, $null, $m, $parameters,$ciUS )
}



$ciUS = [System.Globalization.CultureInfo]'en-US'



$objExcel = New-Object -comobject Excel.Application $objExcel.Visible = $False $objExcel.DisplayAlerts = $False



$objWorkbook = Invoke $objExcel.Workbooks.Open "C:\PS\User Data.xls" Write-Host "Numer of worksheets: " $objWorkbook.Sheets.Count



$objWorksheet = $objWorkbook.Worksheets.Item(1) Write-Host "Worksheet: " $objWorksheet.Name



$Forename = $objWorksheet.Cells.Item(2,1).Text $Surname = $objWorksheet.Cells.Item(2,2).Text



Write-Host "Forename: " $Forename Write-Host "Surname: " $Surname



$objExcel.Quit() If (ps excel) { kill -name excel}


我在论坛上阅读了许多不同的帖子和有关如何尝试解决美国问题的文章,但我似乎无法解决它,希望这里的人可以提供帮助!

这是我提到的 Exeption 问题:

Exception calling "InvokeMember" with "6" argument(s): "Method 'System.Management.Automation.PSMethod.C:\PS\User Data.x
ls' not found."
At C:\PS\excel.ps1:3 char:33
+ $m.PSBase.GetType().InvokeMember <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException



Numer of worksheets: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:18 char:45 + $objWorksheet = $objWorkbook.Worksheets.Item <<<< (1) + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull



Worksheet: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:21 char:37 + $Forename = $objWorksheet.Cells.Item <<<< (2,1).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull



You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:22 char:36 + $Surname = $objWorksheet.Cells.Item <<<< (2,2).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull



Forename:
Surname:


这是我问过的第一个问题,努力做个好人! :))

非常感谢

最大限度

最佳答案

我发现 OLE DB 和 Office 2007 驱动程序更易于使用。如果您没有Office 2007,可以下载drivers from MS .

$filepath = 'C:\Users\u00\documents\backupset.xlsx'
$connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$filepath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
#Connection String for Excel 2003:
#$connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=`"$filepath`";Extended Properties=`"Excel 8.0;HDR=Yes;IMEX=1`";"
$qry = 'select * from [sheet1$]'

$conn = New-Object System.Data.OleDb.OleDbConnection
$conn.ConnectionString = $connString
$conn.open()
$cmd = new-object System.Data.OleDb.OleDbCommand($qry,$conn)
$da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
$dt = new-object System.Data.dataTable
[void]$da.fill($dt)
$conn.close()
$dt

关于excel - 如何在excel中读取单元格数据并输出到命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1378883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com