Copying multiple excel columns one after another with powershell - excel

I'm using powershell to manipulate a group of excel files. I'm copying one column from each file into another file that will contain all the data. The problem is I need to copy the columns one after another.
I have managed to write this to copy a column and paste it to another excel file.
ForEach ($item in $files) {
$FullName = [System.IO.Path]::GetFileName("$item")
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Workbook = $Excel.workbooks.open($Path + [System.IO.Path]::GetFileName("$item"))
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(1)
$range = $WorkSheet.Range($range1).EntireColumn
$range.Copy() | out-null
$Worksheet = $Workbook.Worksheets.item($merge)
$Range = $Worksheet.Range($range2)
$Worksheet.Paste($range)
$WorkBook.Save()
$WorkBook.Close()
$Excel.quit()
}

Related

Powershell - Filter an Excel File

I would like to add a filter for an Excel file within a PowerShell script.
So "if you find in column D the entry "Listener", make the whole row invisible, so filter it out, so that only rows are shown where no "Listener" occurs.
Can I implement this with PowerShell somehow? I tried but it didn't work.
i tried it with this :
$column = 4 # column D
$filename = "C:\Users\xxxxx\Desktop\Test.XLS"
$criteria = "Listener"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$workbook = $Excel.Workbooks.Open($filename)
$worksheet = $workbook.Worksheets.Item(1)
$usedrange = $worksheet.UsedRange
$usedrange.EntireColumn.AutoFilter()
$usedrange.AutoFilter($column, $criteria)
$worksheet.UsedRange.offset($column,4).EntireLine.Delete()
Thanks!
This should work for you:
$column = 4 # column D
$filename = "C:\Users\xxxxx\Desktop\Test.XLS"
$criteria = "Listener"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$workbook = $Excel.Workbooks.Open($filename)
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Activate()
# find the number of used rows in the table
$rowMax = $worksheet.UsedRange.Rows.Count
# hide rows that match the criteria
# go from bottom to top
# if your file does not have column headers, use $row -gt 0
for ($row = $rowMax; $row -gt 1; $row--) {
if ($worksheet.Cells.Item($row, $column).Value() -eq $criteria) {
$worksheet.Rows($row).Hidden = $true
}
}
# save and close the workbook
$workbook.Close($true)
# quit Excel and clean up the COM objects from memory
$Excel.Quit()
# IMPORTANT: clean-up used Com objects
$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()
Edit
I may have misinterpreted your question and instead of hiding the rows that match the criteria like in the code above, all you need is to turn on an Autofilter on column 'D':
$column = 4 # column D
$filename = "C:\Users\xxxxx\Desktop\Test.XLS"
$criteria = "Listener"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$workbook = $Excel.Workbooks.Open($filename)
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Activate()
# find the number of used rows in the table
$rowMax = $worksheet.UsedRange.Rows.Count
# add the autofilter to to column D
[void]$worksheet.Range("D$(1):D$($rowMax)").AutoFilter(1,"<>$criteria")
# save and close the workbook
$workbook.Close($true)
# quit Excel and clean up the COM objects from memory
$Excel.Quit()
# IMPORTANT: clean-up used Com objects
$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()

Split excel sheets without cell formula (links) using PowerShell

I have excel with multiple sheets and I'm trying to split sheets into separate workbooks. Excel sheets are linked between, so when I split them cells will have linked values. Do you have idea how to split excel sheets but paste only values, without formulas?
$Excel = New-Object -ComObject "Excel.Application"
$Excel.Visible = $false
$Excel.DisplayAlerts = $false
$filepath ="C:\Users\XX\Documents\2020\XXX_test.xlsx"
$Workbook = $Excel.Workbooks.open($filepath)
$WorkbookName = "test.xlsx"
$output_type = "xlsx"
if ($Workbook.Worksheets.Count -gt 0) {
write-Output "Now processing: $WorkbookName"
$FileFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
$WorkbookName = $filepath -replace ".xlsx", ""
foreach($Worksheet in $Workbook.Worksheets) {
$Worksheet.Copy()
$ExtractedFileName = $WorkbookName + "~~" + $Worksheet.Name + "." + $output_type
$Excel.ActiveWorkbook.SaveAs($ExtractedFileName, $FileFormat)
$Excel.ActiveWorkbook.Close
write-Output "Created file: $ExtractedFileName"
}
}
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Stop-Process -Name EXCEL
Remove-Variable Excel
The ImportExcel module makes this easier.
Import-Module ImportExcel
$filepath ="C:\Users\XX\Documents\2020\XXX_test.xlsx"
$output_type = "xlsx"
$sheets = Get-ExcelSheetInfo -Path $filepath
foreach ($sheet in $sheets) {
$ExtractedFileName = "{0}~~{1}.{2}" -f ($filepath -replace '\.xlsx$'),$sheet.Name,$output_type
Import-Excel -Path $sheet.Path -WorkSheetName $sheet.Name |
Export-Excel -Path $ExtractedFileName
}

Compare two EXCEL files to delete duplicate data using Powershell

I have a excel sheet that has number of columns and rows i need to delete all columns except one and delete all rows except predefined one(have an other excel sheet with one column and data(in rows) that are need to be deleted.
I have done the first that is to delete all the columns except, but i was only able to do that with Column number and i want to do this with Header name i.e. "Product Name" as Column number may change with other sheets.
Also want to do the same with Rows so i can compare row data from my reference execl.xlsx file and delete the one that are same.
$file = "C:\TE.xlsx" # here goes the path and name of the excel file.
$ColumnsToKeep = 4 # Specify the column numbers to delete.
$excel = New-Object -comobject Excel.Application # Creating object of excel in powershell.
$excel.DisplayAlerts = $False
$excel.visible = $False
$workbook = $excel.Workbooks.Open($file)
$sheet = $workbook.Sheets.Item(1) # Referring to first sheet.
$maxColumns = $sheet.UsedRange.Columns.Count
$ColumnsToRemove = Compare-Object $ColumnsToKeep (1..$maxColumns) | Where-Object{$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
0..($ColumnsToRemove.Count - 1) | %{$ColumnsToRemove[$_] = $ColumnsToRemove[$_] - $_}
$ColumnsToRemove | ForEach-Object{
[void]$sheet.Cells.Item(1,$_).EntireColumn.Delete()
}
$workbook.SaveAs("C:\data1.XLSX")
$workbook.Close($true)
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
Provided all your columns have headers in the first row, you can use this:
$file = "C:\TE.xlsx"
$columnToKeep = 'product'
$excel = New-Object -ComObject Excel.Application
$excel.DisplayAlerts = $false
$excel.Visible = $false
$workbook = $excel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item(1)
$maxColumns = $sheet.UsedRange.Columns.Count
# remove all columns except the one with header named $columnToKeep
for ($col = 1; $col -le $maxColumns; $col++) {
if ($sheet.Cells.Item(1, $col).Value() -ne $columnToKeep) {
[void]$sheet.Columns($col).EntireColumn.Delete()
}
}
$workbook.SaveAs("C:\data1.XLSX")
$workbook.Close($true)
$excel.Quit()
## clean-up used Com objects
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

Powershell with Excel

What I need to do is to extract the data in the excel row and output them into different rows on Excel. After that, I will need to use the extracted data and perform certain conditions on the extracted data.
This is my current script:
To open excel and apply the formulas
$excel = New-Object -ComObject excel.application
$filepath = 'D:\testexcel.xlsx'
$workbook = $excel.workbooks.open("$filepath")
$worksheet = $workbook.worksheets.item(1)
$excel.Visible = $true
$rows = $worksheet.range("A1").currentregion.rows.count
$worksheet.range("S1:S$rows").formula = $worksheet.range("S1").formula
Function to find the row, apply the formula and output it
function test123(){
param([string]$test123)
$sourcefile = "D:\testexcel.xlsx"
$sheetname = "abc"
$excel = new-object -comobject excel.application
$excel.Visible = $true
$excelworkbook = $excel.Workbooks.open($sourcefile, 2, $true)
$excelworksheet = $excelworkbook.worksheets.item($sheetname)
$row = 1
$column = 1
$found = $false
while(($excelworksheet.cells.item($row, $column).value() -ne $null) -and($found -eq $false)){
if(($excelworksheet.cells.item($row, $column).value()).toupper() -eq $test123.ToUpper()){
write-host $excelworksheet.cells.item($row, $column).value() $excelworksheet.cells.item($row, $column+1).value(),
$excelworksheet.cells.item($row, $column +2).value() $found = $true
}
$row += 1
}
#close workbook
$excelworkbook.close()
$excel.quit()
}
test123 -test123 "Test123"
Please guide me and tell me if this is the right way to do it... Thanks
Please have a look into the ImportExcel module by Douge Finke. This module has the capability to do what you need.
Get it from PowerShell gallery: Install-Module -Name ImportExcel
Github link: https://github.com/dfinke/ImportExcel
you can then do Get-Help Import-Excel -Examples which has pretty good examples.

Can PowerShell generate a plain Excel file with multiple sheets?

The Export-Csv cmdlet can export data as a CSV file, which can then be imported in Excel. However, what I'd like to do is to merge different CSV files (with different column definition) into one Excel file having multiple sheets (where each sheet is different).
Can PowerShell call the Excel DLL directly and construct sheets from CSV files?
You can use Excel.ComObject:
## Excel must be installed for this function to work...
Function Merge-CSVFiles
{
Param(
$CSVPath = "C:\CSV", ## Soruce CSV Folder
$XLOutput="c:\temp.xlsx" ## Output file name
)
$csvFiles = Get-ChildItem ("$CSVPath\*") -Include *.csv
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.sheetsInNewWorkbook = $csvFiles.Count
$workbooks = $excel.Workbooks.Add()
$CSVSheet = 1
Foreach ($CSV in $Csvfiles)
{
$worksheets = $workbooks.worksheets
$CSVFullPath = $CSV.FullName
$SheetName = ($CSV.name -split "\.")[0]
$worksheet = $worksheets.Item($CSVSheet)
$worksheet.Name = $SheetName
$TxtConnector = ("TEXT;" + $CSVFullPath)
$CellRef = $worksheet.Range("A1")
$Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
$worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
$worksheet.QueryTables.item($Connector.name).TextFileParseType = 1
$worksheet.QueryTables.item($Connector.name).Refresh()
$worksheet.QueryTables.item($Connector.name).delete()
$worksheet.UsedRange.EntireColumn.AutoFit()
$CSVSheet++
}
$workbooks.SaveAs($XLOutput,51)
$workbooks.Saved = $true
$workbooks.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbooks) | Out-Null
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
Update the Path for the CSV files, it will create a sheet for each CSV in the folder,
Run it like this:
Merge-CSVFiles -CSVPath C:\CsvFolder -XLOutput C:\ExcelFile.xlsx
Using the Export-excel method will create multiple sheets
$a=[pscustomobject]#{ColumnOne=1;ColumnTwo=2;}
$b=[pscustomobject]#{ColumnOne=1;ColumnTwo=2;}
$a|Export-excel $fileName -Autosize -TableName A -WorksheetName A
$b|Export-excel $fileName -Autosize -TableName B -WorksheetName B
The resulting spreadsheet will look as follows...

Resources