Paste into Excel from Powershell - excel

I have tried lots of options to paste information copied from other Excel workbook into my new workbook but not success do that (the range is huge - more them 3000 lines).
Please see sample of my script:
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.displayAlerts = $false
$Src = [Environment]::GetFolderPath('Desktop')+'\New.xlsx'
$Files = [Environment]::GetFolderPath('Desktop')+'\Org.xlsx'
$wb1 = $objExcel.workbooks.open($Files)
$Worksheetwb1 = $wb1.WorkSheets.item('Org')
$Worksheetwb1.activate()
$Range = $Worksheetwb1.Range('A1:I1').EntireColumn
$Range.Copy() | Out-Null
$wb3 = $objExcel.workbooks.open($Src)
$Worksheetwb3 = $wb3.WorkSheets.item('Dest')
$Worksheetwb3.activate()
$Worksheetwb3.Columns.item('A:I').clear()
$Range3 = $Worksheetwb3.Range('A1:I1').EntireColumn
$Worksheetwb3.Paste($Range.Value2)
$wb3.close($true)
$wb1.close($true)
$objExcel.Quit()

You're pasting into a wrong range. Worksheet.Paste() has parameters of destination and link, your code uses destination only, which should be a Range belonging to that worksheet. Therefore, the proper line should be this:
$Worksheetwb3.Paste($Range3)

Alternatively to Vesper's solution:
$Worksheetwb3.Range("A1").Paste() | Out-Null
# Paste special (as values)
$Worksheetwb3.Range("A1").PasteSpecial(-4163) | Out-Null

I have found the answer by changing the order of commands the Copy and then immediate after it the paste solved it for me.

Related

Load variable data into a workbook using PowerShell

I have an API request that set a variable named $data with CSV style data.
I want to open a Workbook with that data without writing on disk.
How can I do that?
The best I can do is
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add()
$workbook.worksheets(1).Cells(1,1) = $csv
Unfortunately, it fills my CSV into 1 cell, adding delimiter, instead of simply "copying" data into the workbook.
This is a fairly simple task if you want to use the ComObject, as you can use ConvertTo-Csv with a tab delimiter, copy to the clipboard, and paste into Excel. Here's a snippet of what I've used in a script myself to do just that:
$tasks|ConvertTo-Csv -del "`t" -NoTypeInformation|clip
$XL = New-Object -ComObject Excel.Application
$XL.Visible = $true
$WB = $XL.Workbooks.Add()
$XL.ActiveCell.PasteSpecial() | Out-Null

How to use PowerShell to write a files content to different rows in Excel

I am trying to automate the following manual task, and am struggling with part of it:
1) Open a text file that contains multiple lines containing data.
2) Copy the contents of this file to the clipboard.
3) Open and Excel spreadsheet.
4) Rename the spreadsheet to Test.
5) Paste the contents of the clipboard.
When this is done manually the content is pasted and each line in the text file is inserted as a new row in column A.
Originally the customer wanted all of the file content to be injected into cell A1. I was able to achieve this with the below PowerShell code.
However they have since changed this back to wanting each line of text to go into a separate row in column A.
I cannot figure out how to do this gracefully via the Get-Content method of copying out the text data. I have seen workarounds to this issue whereby Excel opens the text file and copies the text into an intermediate workbook and then into the final workbook.
Could someone please let me know if it's possible to amend my already working code below so that it adds the text to rows in column A rather than to cell A1?
# Clear the screen of any previous text.
cls
$ExcelFile="C:\Users\User\Desktop\Test\Test.xlsx"
$TextFile="C:\Users\User\Desktop\Test\TestText.txt"
$Content = Get-Content $TextFile -Raw
# Perform operations in Excel based on content of the downloaded file.
$Excel = New-Object -ComObject Excel.Application
# For troubleshooting enable the below to view Excel as file is manipulated:
#$Excel.Visible=$true
# Disable Excel alerts. Hash this line out for troubleshooting.
$Excel.DisplayAlerts = $false
# Set up workbook...
$Workbook = $Excel.Workbooks.Add()
$Data = $Workbook.Worksheets.Item(1)
$Data.Name = 'Test'
# Insert Data
$Data.Cells.Item(1,1) = "$Content"
# Format, save and quit excel
$UsedRange = $Data.UsedRange
$UsedRange.EntireColumn.AutoFit() | Out-Null
$Workbook.SaveAs("$ExcelFile")
$Excel.Quit()
I know that the part I would need to change is as follows, but I'm not sure what to change it to:
# Insert Data
$Data.Cells.Item(1,1) = "$Content"
Many thanks in advance.
To do this, you need to find the last used row in the sheet and write each line from there:
$ExcelFile = "C:\Users\User\Desktop\Test\Test.xlsx"
$TextFile = "C:\Users\User\Desktop\Test\TestText.txt"
# Perform operations in Excel based on content of the downloaded file.
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $false
# open the file and select the first worksheet
$WorkBook = $Excel.Workbooks.Open($ExcelFile)
$WorkSheet = $Workbook.Worksheets.Item(1)
# get the first unused row
$row = ($WorkSheet.UsedRange.Rows).Count + 1
# fill in the data
Get-Content -Path $TextFile | ForEach-Object {
$WorkSheet.Cells.Item($row++, 1) = $_
}
# format column A and save the file
$UsedRange = $WorkSheet.UsedRange
$UsedRange.EntireColumn.AutoFit() | Out-Null
$WorkBook.Save()
# quit excel and clean up the used COM objects
$Excel.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkSheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
I think the solution would be to read each line in content by for or foreach loop
in loop, write the line's content into the last row of column A in the excel file.
It's will be something like this
foreach($line in $Content){
$Data.Cells.Item($LastRow,1) = $line
}

Copy CSV Rows to an Existing Excel Sheet

This should be pretty simple. I'm looking to take the information from my previous question(s), which is a CSV, and place them in an existing Excel document.
Here's the existing data (in a CSV):
SO | Status | ElapsedHrs
PMTT12345678 Hit on Debra 2.5
PMTS23456789 Get rejected 4.25
PMTT87654321 Send some faxes 1.0
So I have an existing Excel sheet, where all of the SO category needs to go to column K starting at cell 6, Status goes to L starting at 6 and ElapsedHrs goes to column O starting at cell 6.
Using this and a few others as examples, but I can't figure out the syntax. Any help is appreciated, again.
Edit
So far I have this:
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$WorkBook = $objExcel.Workbooks.Open($ExportCsv)
$WorkBook2 = $excel.Workbooks.open($Template)
$Worksheet = $Workbook.WorkSheets.item(“$ExpCsvShort”)
$ExportCsv is the name of the CSV with the full path. $ExpCsvShort' is just the filename (the name changes based on the hour and date). $Template` is the template .xslx file to which the data would be written.
$range = $WorkSheet.Range(“A2”).EntireColumn
$range.Copy() | out-null
Not sure what this should be, as I want column A (minus the header) to go to K on $Template starting at 6. Then I want C to start at L6 and D to start at O6, but I don't know the syntax.
Once I have that:
$Worksheet2 = $Workbook2.Worksheets.item(“Worklog”)
$worksheet2.activate()
$range2 = $Worksheet2.Range(“K6:K6”)
$Worksheet2.Paste($range2)
$workbook2.SaveAs($WorkLogSave)
$workbook.close($false)
$Excel.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
But again, I don't know the syntax for the range here, either.
Edit 2
Here's what I ended up doing.
$Excel = New-Object -ComObject excel.application
$Excel.visible = $true
$WorkBook = $excel.Workbooks.Open($ExportCsv)
$WorkBook2 = $excel.Workbooks.open($Template)
$Worksheet = $Workbook.WorkSheets.item($ExpCsvShort)
$Worksheet.activate()
#A Range Copy
$rangeAc = $WorkSheet.Range(“A2:A26”)
$rangeAc.Copy() | out-null
#Select sheet 2
$Worksheet2 = $Workbook2.Worksheets.item(“Worklog”)
$worksheet2.activate()
#A Range Paste
$rangeAp = $Worksheet2.Range(“K6:K30”)
$Worksheet2.Paste($rangeAp)
#C Range Copy
$rangeCc = $WorkSheet.Range(“C2:C26”)
$rangeCc.Copy() | out-null
#C Range Paste
$rangeCp = $Worksheet2.Range(“O6:O30”)
$Worksheet2.Paste($rangeCp)
#D Range Copy
$rangeDc = $WorkSheet.Range(“D2:D26”)
$rangeDc.Copy() | out-null
#D Range Paste
$rangeDp = $Worksheet2.Range(“L6:L30”)
$Worksheet2.Paste($rangeDp)
$workbook2.SaveAs($WorkLogSave)
$workbook2.close($true)
$workbook.close($true)
$Excel.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Probably a pretty crappy way to do it, but it works. Also, I still have EXCEL.EXE running after everything is closed. I've read about 5 ways to kill the process, but I'm worried if I do I'll mess someone up who has another document open, so maybe I can -passthru and capture the .id and kill that instead, but I'll worry about that later I guess.
Thanks for helping, all.
And I'd love to use that gravity, but I'm not sure I understand how. Thanks!

How do I export just one worksheet in Excel to a single htm file?

I'm having trouble saving one worksheet from Excel workbook as a single .htm file.
I know if I open up Excel, open my workbook, select the worksheet I want, and do a "Save As" file type .htm it will work. Every time I code it I get "Hitlist.htm" plus a folder named "Hitlist_files" with all the style sheets, etc. Unfortunately, in powershell it does the "save as" with all the other data and supplemental files (extra folders, styling specifications, etc.)
Help. Code below.
#Create and get my Excel Obj
$excel = New-Object -comobject Excel.Application
$excel.visible=$false
$excel.DisplayAlerts=$false
$UserWorkBook = $excel.Workbooks.Open("e:\hitlist\hitlist.xlsx")
#Select first Sheet
$UserWorksheet = $UserWorkBook.Worksheets.Item(1)
#HitList File name and type
$hitlist = "E:\HitList\Hitlist.htm"
$xlHtml = 44
#Save, close, and clean up
#I tried this too...no go - $UserWorkBook.SaveAs($hitlist,$xlHtml)
$UserWorksheet.SaveAs($hitlist,$xlHtml)
$UserWorkBook.close()
$excel.quit()
$excel = $null
I've adjusted your code to your expectations again, after we clarified some more. In particular, look at any line denoted with #changed-grav for my modifications to your existing code, or the very end (#added-grav) for some additional steps I added to fit the exact specs:
(This was tested fully, and appears to be working exactly as you requested - but I did some modifications for my testing, so let me know if I didn't change a value back that you needed)
#Create and get my Excel Obj
$excel = New-Object -comobject Excel.Application
$excel.visible=$false
$excel.DisplayAlerts=$false
$UserWorkBook = $excel.Workbooks.Open("e:\hitlist\hitlist.xlsx")
#Select first Sheet
$UserWorksheet = $UserWorkBook.Worksheets.Item(1)
#HitList File name and type
$hitlistCSV = "e:\hitlist\hitlist.csv" #changed-grav
$hitlistHTML = "e:\hitlist\hitlist.htm" #changed-grav
$xlCSV = 6 #changed-grav
#Save, close, and clean up
$UserWorksheet.SaveAs($hitlistCSV,$xlCSV) #changed-grav
$UserWorkBook.close()
$excel.quit()
$excel = $null
#new functionality, to import the CSV and then export as HTM
#added-grav START
$htmlData = Get-Content $hitlistCSV | ConvertFrom-CSV | ConvertTo-HTML
Set-Content $hitlistHTML $htmlData
Remove-Item $hitlistCSV
#added-grav END

Add a column in excel sheet using powershell

I want to add a column after a particular column number in excel sheet using Powershell.
I am able to add it at starting of sheet, but couldn't insert after a specific column.
#This will insert a column at column R
$Excel = New-Object -ComObject excel.application
$ExcelWorkSheet = $ExcelWordBook.Worksheets.Add()
$ExcelWorkSheet.Name = "TestThis"
#do other things
$ColumnSelect = $ExcelWorkSheet.Columns("R:R")
$ColumnSelect.Insert()
Alas, I agree, I have not found neither documentation or examples :-/ .
Nevertheless here is below how to insert a column 7th and give it a name:
(Get-ChildItem "*.xlsb")|
foreach-object {
$xl=New-Object -ComObject Excel.Application
$wb=$xl.workbooks.open($_)
$ws = $wb.worksheets.Item(1)
$ws.Columns.ListObject.ListColumns.Add(7)
$ws.Cells.Item(1,7) ='Comment'
$wb.Save()
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$xl)){'released'| Out-Null}
}
Best regards

Resources