【问题标题】:Insert CSV data into next available corresponding cell in Excel using Powershell使用 Powershell 将 CSV 数据插入 Excel 中下一个可用的相应单元格
【发布时间】:2016-07-01 02:30:30
【问题描述】:

我有一个每周导出到 CSV 的查询,其中包含与我的 excel 数据表相同的标题和列顺序。 powershell 脚本将在事件计时器上,我希望它获取 CSV 并将数据导入下一个可用单元格中的相应标题。我不希望它覆盖现有数据,本质上它是一个不断增长的表。

环境:Powershell V3 w/ISE Steroids & Excel 2013

这是我当前不起作用的代码:

Add-Type -AssemblyName Microsoft.Office.Interop.Excel

$Excel = New-Object -ComObject Excel.Application
$xlLastCell = [Microsoft.Office.Interop.Excel.Constants]::xlLastCell
$xlFile = 'C:\Users\me\Desktop\Test\Agents Stats 2016.xlsx'
$csvFile = 'C:\Users\me\Desktop\Test\Data\AgentStats.csv'

$Excel.Visible = $true

$ExcelWordBook = $Excel.Workbooks.Open($xlFile)
$ExcelWorkSheet = $Excel.WorkSheets.item('rawdata')
$ExcelWorkSheet.activate()

$objRange = $ExcelWorkSheet.UsedRange
$lastRow = $objRange.SpecialCells($xlLastCell).Row
$ExcelWorkSheet.cells($lastRow,1).Select()

$AgentStats = Import-Csv -Path C:\Users\me\Desktop\Test\Data\AgentStats.csv
$AgentStats.foreach{
$lastRow += 1
$ExcelWorkSheet.cells($lastRow,1).value = $psitem.COUNT
$ExcelWorkSheet.cells($lastRow,2).value = $psitem.STATUS
$ExcelWorkSheet.cells($lastRow,3).value = $psitem.OPERATOR
$ExcelWorkSheet.cells($lastRow,4).value = $psitem.PRODUCT
$ExcelWorkSheet.cells($lastRow,5).value = $psitem.WEEK
}

$ExcelWordBook.Save()
$ExcelWordBook.Close()
$Excel.Quit()

我收到以下错误,但我不知道为什么:

在第 19 行 char:20 + $AgentStats.foreach{ + ~ 表达式或语句中出现意外标记“{”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

[16,1] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

[21,3] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

[22,3] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

[23,3] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

[24,3] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

[25,3] 方法调用失败,因为 [System.__ComObject] 不包含名为“cells”的方法。

在过去的一天里,我用谷歌搜索了这些错误,但我用“Foreach”胡闹,但没有用。但是,“cells com 对象”是合法的并且确实存在,我无法终生弄清楚为什么会发生该错误。

提前感谢您的帮助,我是 Powershell 的新手。

【问题讨论】:

    标签: excel csv powershell


    【解决方案1】:

    1) 你用错了括号:

    $AgentStats.foreach( #Not the squiggelies
    $lastRow += 1
    $ExcelWorkSheet.cells($lastRow,1).value = $psitem.COUNT
    $ExcelWorkSheet.cells($lastRow,2).value = $psitem.STATUS
    $ExcelWorkSheet.cells($lastRow,3).value = $psitem.OPERATOR
    $ExcelWorkSheet.cells($lastRow,4).value = $psitem.PRODUCT
    $ExcelWorkSheet.cells($lastRow,5).value = $psitem.WEEK
    )
    

    2) 我认为您的对象声明不正确:

    $ExcelWordBook = $Excel.Workbooks.Open($xlFile)
    $ExcelWorkSheet = $ExcelWordbooks.item('rawdata') #here, use your excelwordbooks-object
    $ExcelWorkSheet.activate() #Not necessary
    

    【讨论】:

    • 选择一个工作表:$worksheet = $workbook.worksheets.item(1) 引用单元格:$worksheet.cells.item(1,1).Text,这纯粹是获取,我没有尝试设置。这些以前对我有用,希望对您有所帮助
    • 如果它不起作用:只是从我的记忆中回答:尝试使用属性value2,就像我在 excel 中使用它一样。
    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多