Powershell / Excel - 删除部分单元格内容 [英] Powershell/Excel - Deleting Partial Cell Contents

查看:373
本文介绍了Powershell / Excel - 删除部分单元格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用Powershell删除excel中单元格的部分内容。



一个单元格为: 



* 1    CISCO1941 / K9          FCZ1645C1QB

并且需要:b
$
FCZ1645C1QB

$
和第二个单元格是:



Leon-ver-live



并且需要:



Leon



到目前为止,我已将所需部分转移到同一工作表中的新单元格,并且我现在正试图将结果转移到新的工作簿(请原谅错误的代码):

Hi,

I'm trying to delete part of the content of a cell in excel using Powershell.

One cell is: 

*1    CISCO1941/K9          FCZ1645C1QB
and needs to be:

FCZ1645C1QB

and the second cell is:

Leon-ver-live

and needs to be:

Leon

So far I've been transferring the required part to a new cell in the same worksheet, and am now trying to transfer the results to a new workbook(Excuse the bad code):

#从CSV获取信息并输入csv

Select-String - 路径c:\ * -Live CISCO1941 / K9 -AllMatches | Export-CSV C:\ xxxx.csv

$ Excel = New-Object -ComObject Excel.Application

$ Excel.visible = $ true

$ Excel.DisplayAlerts = $ false

#build电子表格和导入数据

$ Excel.Workbooks.Open(" C:\ xxx.csv")

$ Excel.Worksheets.Item(1).name =" InventoryRAW"

$ Excel.Worksheets.Item(" InventoryRAW")。activate()





#delete列

[void] $ Excel.Cells.Item(1,1).EntireColumn。删除()

[void] $ Excel.Cells.Item(1,1).EntireColumn.Delete()

[void] $ Excel.Cells.Item(1 ,6).EntireColumn.Delete()

[void] $ Excel.Cells.Item(1,5).EntireColumn.Delete()

[void] $ Excel .Cells.Item(1,3).EntireColumn.Delete()

[void] $ Excel.Cells.Item(2,1).EntireRow.Delete()




$
#Autofit

$ Excel.Cells.Item(" 1,5")。 EntireColumn.AutoFit()

$ Excel.Cells.Item(" 1,4")。EntireColumn.AutoFit ()

$ Excel.Cells.Item(" 1,3")。EntireColumn.AutoFit()

$ Excel.WorkSheets.item(" InventoryRAW") .UsedRange.Columns.Autofit()| Out-Null

[void] $ Excel.Cells.Item(2,1).EntireRow.Delete()



#Excel公式

$ Excel.Cells.Item(2,5).Value2 ='= RIGHT(A2,LEN(A2)-FIND(" K",A2)-1)'

$ Excel.Cells.Item(2,4).Value2 ='= LEFT(B2,LEN(B2)-FIND(" v",B2)-3)'



#输入新的csv文件

$ xl = new-object -comobject excel.application

$ xl.Visible = $ true < br $>
$ xl.DisplayAlerts = $ False

$ wb = $ xl.Workbooks.Add()

$ ws = $ wb.Worksheets.Item (1)
$
[void] $ xl.Worksheets.Item(3).Delete()

[void] $ xl.Worksheets.Item(2).Delete( )
$
$ ws.name ="库存"



在尝试这样做的时候,我偶然发现了.split功能尝试解决是否可以使用它来更新原始文件,而不是通过将数据从单元格传输到单元格到单元格的严格程度。  到目前为止我有这个:

# Get info from CSV and put into csv
Select-String -Path c:\*-Live CISCO1941/K9 -AllMatches | Export-CSV C:\xxxx.csv
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Excel.DisplayAlerts = $false
#build spread sheet and import data
$Excel.Workbooks.Open("C:\xxxx.csv")
$Excel.Worksheets.Item(1).name="InventoryRAW"
$Excel.Worksheets.Item("InventoryRAW").activate()


#delete columns
[void]$Excel.Cells.Item(1,1).EntireColumn.Delete()
[void]$Excel.Cells.Item(1,1).EntireColumn.Delete()
[void]$Excel.Cells.Item(1,6).EntireColumn.Delete()
[void]$Excel.Cells.Item(1,5).EntireColumn.Delete()
[void]$Excel.Cells.Item(1,3).EntireColumn.Delete()
[void]$Excel.Cells.Item(2,1).EntireRow.Delete()



#Autofit
$Excel.Cells.Item("1,5").EntireColumn.AutoFit()
$Excel.Cells.Item("1,4").EntireColumn.AutoFit()
$Excel.Cells.Item("1,3").EntireColumn.AutoFit()
$Excel.WorkSheets.item("InventoryRAW").UsedRange.Columns.Autofit() | Out-Null
[void]$Excel.Cells.Item(2,1).EntireRow.Delete()

#Excel Formula
$Excel.Cells.Item(2,5).Value2 = '=RIGHT(A2,LEN(A2)-FIND("K",A2)-1)'
$Excel.Cells.Item(2,4).Value2 = '=LEFT(B2,LEN(B2)-FIND("v",B2)-3)'

#Put result in new csv file
$xl = new-object -comobject excel.application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
[void]$xl.Worksheets.Item(3).Delete()
[void]$xl.Worksheets.Item(2).Delete()
$ws.name = "Inventory"

In trying to do this I've stumbled upon the .split function and am trying to work out if it's possible to use it to just update the original file instead of going through the rigmarole of transferring data from cell to cell to cell.  So far I have this:

#从CSV获取信息并输入csv

Select-String -Path c:\ * -Live CISCO1941 / K9 -AllMatches | Export-CSV C:\Users\robertph \ Files \ RepeatableDesign\LeonPowershellScript\cs5-export.csv

$ Excel = New-Object -ComObject Excel.Application

$ Excel.visible = $ true

$ Excel.DisplayAlerts = $ false

#build电子表格和进口数据

$ Excel.Workbooks.Open(" C:\Users\robertph\Documents\RepeatableDesign\LeonPowershellScript\csv5-export.csv")

$ Excel.Worksheets.Item(1 ).name =" InventoryRAW"

$ Excel.Worksheets.Item(" InventoryRAW")。activate()



$ HostName = $ Excel.Cells.Item(" 3,4")

$ Object = @()

Foreach($ HostName $ $){

  &NBSP; $ one =($ Entry.Split( - )[0])。修剪( - )

  &NBSP; $ 2 =($ Entry.Split( - )[1])。修剪( - )

  &NBSP; $ 3 =($ Entry.Split( - )[2])。修剪( - )



  &NBSP;删除项目$ 2 $
  &NBSP; Remove-Item $ 3

} b

# Get info from CSV and put into csv
Select-String -Path c:\*-Live CISCO1941/K9 -AllMatches | Export-CSV C:\Users\robertph\Documents\RepeatableDesign\LeonPowershellScript\cs5-export.csv
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Excel.DisplayAlerts = $false
#build spread sheet and import data
$Excel.Workbooks.Open("C:\Users\robertph\Documents\RepeatableDesign\LeonPowershellScript\csv5-export.csv")
$Excel.Worksheets.Item(1).name="InventoryRAW"
$Excel.Worksheets.Item("InventoryRAW").activate()

$HostName = $Excel.Cells.Item("3,4")
$Object = @()
Foreach ($Entry in $HostName) {
    $one = ($Entry.Split(-)[0]).Trim(-)
    $two = ($Entry.Split(-)[1]).Trim(-)
    $three = ($Entry.Split(-)[2]).Trim(-)

    Remove-Item $two
    Remove-Item $three
}

非常感谢任何建议!

Rob

推荐答案

几乎不可能破译你想要问的内容。

It is almost impossible to decipher what you are trying to ask.

为什么不只是在Excel中执行此操作? 为什么你认为你需要PowerShell?

Why not just do this in Excel?  Why do you think you need PowerShell?

在PowerShell中这是如何使用拆分

In PowerShell this is how to use split

 

 

Excel.Cells.Item(" 3,4")。Value =
Excel.Cells.Item("3,4").Value =


Excel.Cells.Item(" 3,4")。 Value2.Split('',[System.StringSplitOptions] :: RemoveEmptyEntries)[2]
Excel.Cells.Item("3,4").Value2.Split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)[2]

这将获取数组的第三个元素并将其分配回单元格。

This takes the third element of the array and assigns it back to the cell.

除了短划线外,此处相同。

Same here except on the dash.


这篇关于Powershell / Excel - 删除部分单元格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆