简体   繁体   English

读取 Excel 表中特定列的最后一行并附加更多数据 - 在 PowerShell

[英]Reading last row of specific column in Excel sheet and appending more data - in PowerShell

I'm trying to write data to specific column in an Excel spreadsheet with PowerShell.我正在尝试使用 PowerShell 将数据写入 Excel 电子表格中的特定列。 I would like to start below last row with data and continiue downwards.我想从最后一行的数据开始,然后继续向下。 On machine I don't have Excel installed so COM won't work for me.在机器上我没有安装 Excel 所以 COM 对我不起作用。 I'm currently using Import-Excel to read whole sheet and used Open-ExcelPackage to read specific cell values.我目前正在使用Import-Excel来读取整个工作表,并使用Open-ExcelPackage来读取特定的单元格值。

I could do this in CSV file as opposed to .xlsx if it's easier.如果更容易,我可以在CSV文件中执行此操作,而不是.xlsx

Any help would be great!任何帮助都会很棒!

Download PSExcel module from https://github.com/RamblingCookieMonster/PSExcel Import it using Import-Module.https://github.com/RamblingCookieMonster/PSExcel下载 PSExcel 模块 使用 Import-Module 导入。

Then use the following code:然后使用以下代码:

$File = "Path to xlxs file"
$WSName = "SheetName"
$Excel = New-Excel -Path $File
$Worksheet = $Excel | Get-WorkSheet -Name $WSName

$SampleRows = @()
$SampleRows += [PSCustomObject]@{"A" = 1; "B" = 2; "C" = 3; "F" = 4 }
$row_to_insert = $SampleRows.count

$Worksheet.InsertRow($Worksheet.Dimension.Rows,$row_to_insert)
$WorkSheet.Cells["$($Worksheet.Dimension.Start.Address -replace ""\d"")$($Worksheet.Dimension.End.Row):$($Worksheet.Dimension.End.Address)"].Copy($WorkSheet.Cells["$($Worksheet.Dimension.Start.Address -replace ""\d"")$($Worksheet.Dimension.End.Row - $row_to_insert):$($Worksheet.Dimension.End.Address -replace ""\d"")$($Worksheet.Dimension.End.Row - $row_to_insert)"]);
$WorkSheet.Cells["$($Worksheet.Dimension.Start.Address -replace ""\d"")$($Worksheet.Dimension.End.Row):$($Worksheet.Dimension.End.Address)"] | % {$_.Value = ""}
ForEach ($Row in $SampleRows) {
ForEach ($data in $Row.PSObject.Properties.Name) {
$WorkSheet.Cells["$data$($Worksheet.Dimension.Rows)"].Value = $SampleRow.$data
}
}
$Excel | Close-Excel -Save

This code adds 1 row after the last row in the selected worksheet and adds values to this row from $SampleRows .... I think you got the idea.此代码在所选工作表的最后一行之后添加 1 行,并从$SampleRows向该行添加值 .... 我想你明白了。 if you need add more rows to $SampleRows array.如果您需要向$SampleRows数组添加更多行。

暂无
暂无

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

相关问题 Excel中特定行的最后一列 - Last column of a specific row in Excel 读取多张 excel 表并删除每张表的最后一行 - reading multiple excel sheets and dropping the last row of each sheet 从Excel工作表插入数据的特定列 - Inserting Specific Column of Data From Excel Sheet 在不打开Excel表的情况下获取最后使用的行和列 - Get last used Row and Column in an Excel Sheet without opening it 查找特定范围数据的最后一行Excel - Find last row of specific range of data excel 如何使用基于给定列表的python将数据添加到Excel工作表中的特定行/列? - How to add data to specific row/column in an excel sheet using python based on a given list? 当第一行、最后一行、第一列、最后一列仅保存变量时,对 Excel 工作表进行排序 - Sorting an Excel Sheet when First Row, Last Row, First Column, Last Column saved only variables 如何使用Java从Excel工作表中获取特定列名(作为参数传递)的最后一个非空单元格的行索引? - How to get the row index of last non-empty cell for a specific column name(passed as an argument) from an excel sheet using java? 将未打开的excel工作表中的excel数据复制并粘贴到目标excel工作表到最后一行 - Copy and Pasting excel data from unopened excel sheet to destination excel sheet to the last row 电子表格excel阅读器不读取工作表,如果它在行65536上有数据 - spreadsheet excel reader not reading sheet if it has data on row 65536
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM