1. Open Excel, Select the 1st worksheet, set AutoFilter and then filter by Specific Column that equals a particular String
$Filename = "c:\temp\mysheet.xlsx"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.DisplayAlerts = $False
$objWrkBk = $objExcel.Workbooks.Open($Filename)
$objExcel.Visible = $True
# one way of selecting a sheet e.g. 3rd worksheet
# $objWrkBk.Sheets(3).Select()
# another way of selecting a sheet e.g. 1st worksheet
# $objWrkBk.Worksheets.item(1).Select()
$objWrkSht1 = $objWrkBk.Worksheets.item(1)
$objWrkSht1.Select()
# filter on 7th column, with string matching 'ABC'
$objWrkSht1.Range('$A$nn:$P$mm').AutoFilter(7,'=ABC')
# $A$nn => range first cell position, $P$mm = range last cell position
2. Read data from particular cell
# Read data at column 7, row 1
$objWrkSht1.Cells.Item(7,1).Value2
Company
3. Read data from a cell range
# Read data from a range
$objWrkSht1.Range("G2:G4").Value2
ABC
ABC
ABC
4. Read data from a cell range into an array
#read into an array
$companies = @($objWrkSht1.Range("G2:G21").Value2)
# iterate on all array items, method 1
for ($i=0;$i -lt $companies.Length; $i++)
{
$companies[$i]
}
# iterate on all array items, method 2
foreach ($coy in $companies) { $coy }
# how to sort on 2 columns H & G with header on, sort by column G ascending, followed by column H descending
$xlSortOnValues = $xlSortNormal = 0
$xlTopToBottom = $xlSummaryBelow = 1
$xlAscending = 1
$xlDescending = 2
$xlNo = 2
$xlYes = 1
$objWrkSht1.sort.setRange($objRange) # define the range to sort
$objWrkSht1.sort.header = $xlYes # range has a header
$objWrkSht1.sort.orientation = $xlTopToBottom
$objWrkSht1.sort.apply()
Before Sort
After Sort
7. Finding the last row and last column of the worksheet that is in use
$xlFilterValues = 7
$xlAnd = 1
$xlOr = 2
# single criteria
$Criteria = "<>ABC01" # Not Equal, use with xlAnd
$Criteria = "=ABC*" # Begins with, use with xlAnd
$Criteria = "=*02" # Ends with, use with xlAnd
$Criteria = "=*BC*" # Contains, use with xlAnd
$Criteria = "<>*BC*" # Does Not Contain, use with xlAnd
$objRange.AutoFilter(7,$Criteria,$xlAnd)
# 2 Criteria with logical And
$Criteria1 = "=A*"
$Criteria2 = "=*1"
$objRange.AutoFilter(7,$Criteria1,$xlAnd,$Criteria2)
# 2 Criteria with logical Or
$objRange.AutoFilter(7,$Criteria1,$xlOr,$Criteria2)
#
# TO CLEAR EXISTING FILTERS
$objRange.AutoFilter()
#
10. Select cells that were autofiltered
# apply autofilter and then select the active cells that are visible from top left to bottom right
$Filename = "c:\temp\mysheet.xlsx"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.DisplayAlerts = $False
$objWrkBk = $objExcel.Workbooks.Open($Filename)
$objExcel.Visible = $True
$objWrkSht1 = $objWrkBk.Worksheets.item(1)
$objWrkSht1.Select()
$objRange = $objWrkSht1.UsedRange
# define constants
$xlCellTypeLastCell = 11
$xlOpenXMLWorkbook = 51
$xlFilterValues = 7
$xlAnd = 1
$xlOr = 2
$xlLastCell = 11
# find last cell
$objLastcell = $objRange.SpecialCells($xlCellTypeLastCell)
$lastrow = $objLastcell.row
$lastcolumn = $objLastcell.column
# Set autofilter
$Criteria = "=DEF02"
$objRange.AutoFilter(7,$Criteria)
# Select filtered cells
$objExcel.ActiveCell.SpecialCells($xlLastCell).Select()
$objExcel.Range($objExcel.Selection,$objExcel.Cells(1)).Select()
# copy and paste to new workbook
$objExcel.Selection.Copy()
$objExcel.Workbooks.Add()
$objExcel.ActiveSheet.Paste()
# set nowrap and column autofit
$objExcel.Selection.WrapText = $False
$objExcel.Selection.Columns.AutoFit()
$objExcel.Range("A2").Select()
# save as new excel workbook
$objExcel.ActiveWorkbook.SaveAs("c:\temp\DEF02-SR-Oct-2018.xlsx",$xlOpenXMLWorkbook)
#
$objExcel.Workbooks.Close()
$objExcel.Quit()
Initial workbook
After autofilter and saving as new workbook
12. Import data from CSV file into a table-like custom object.
The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values.
# This is native to powershell and not really part of Excel $mycontacts = import-csv "c:\temp\contacts.csv" # Creates table-like custom objects from the items in a CSV file # An array of row items, each column accessible by .ColumnName $mycontacts[0].name,$mycontacts[0].address,$mycontacts[0].phone
'------------' foreach ($contact in $mycontacts) { $contact.name,$contact.address,$contact.phone }
source .csv file
output
tom
1 main street
12345678
------------
tom
1 main street
12345678
dick
2 side avenue
23456781
mary
3 highland drive
34567812
xlFileFormat enumeration (Excel)
Name
Value
Description
Extension
xlAddIn
18
Microsoft Excel 97-2003 Add-In
*.xla
xlAddIn8
18
Microsoft Excel 97-2003 Add-In
*.xla
xlCSV
6
CSV
*.csv
xlCSVMac
22
Macintosh CSV
*.csv
xlCSVMSDOS
24
MSDOS CSV
*.csv
xlCSVWindows
23
Windows CSV
*.csv
xlCurrentPlatformText
-4158
Current Platform Text
*.txt
xlDBF2
7
Dbase 2 format
*.dbf
xlDBF3
8
Dbase 3 format
*.dbf
xlDBF4
11
Dbase 4 format
*.dbf
xlDIF
9
Data Interchange format
*.dif
xlExcel12
50
Excel Binary Workbook
*.xlsb
xlExcel2
16
Excel version 2.0 (1987)
*.xls
xlExcel2FarEast
27
Excel version 2.0 far east (1987)
*.xls
xlExcel3
29
Excel version 3.0 (1990)
*.xls
xlExcel4
33
Excel version 4.0 (1992)
*.xls
xlExcel4Workbook
35
Excel version 4.0. Workbook format (1992)
*.xlw
xlExcel5
39
Excel version 5.0 (1994)
*.xls
xlExcel7
39
Excel 95 (version 7.0)
*.xls
xlExcel8
56
Excel 97-2003 Workbook
*.xls
xlExcel9795
43
Excel version 95 and 97
*.xls
xlHtml
44
HTML format
*.htm; *.html
xlIntlAddIn
26
International Add-In
No file extension
xlIntlMacro
25
International Macro
No file extension
xlOpenDocumentSpreadsheet
60
OpenDocument Spreadsheet
*.ods
xlOpenXMLAddIn
55
Open XML Add-In
*.xlam
xlOpenXMLStrictWorkbook
61 (&H3D)
Strict Open XML file
*.xlsx
xlOpenXMLTemplate
54
Open XML Template
*.xltx
xlOpenXMLTemplateMacroEnabled
53
Open XML Template Macro Enabled
*.xltm
xlOpenXMLWorkbook
51
Open XML Workbook
*.xlsx
xlOpenXMLWorkbookMacroEnabled
52
Open XML Workbook Macro Enabled
*.xlsm
xlSYLK
2
Symbolic Link format
*.slk
xlTemplate
17
Excel Template format
*.xlt
xlTemplate8
17
Template 8
*.xlt
xlTextMac
19
Macintosh Text
*.txt
xlTextMSDOS
21
MSDOS Text
*.txt
xlTextPrinter
36
Printer Text
*.prn
xlTextWindows
20
Windows Text
*.txt
xlUnicodeText
42
Unicode Text
No file extension; *.txt
xlWebArchive
45
Web Archive
*.mht; *.mhtml
xlWJ2WD1
14
Japanese 1-2-3
*.wj2
xlWJ3
40
Japanese 1-2-3
*.wj3
xlWJ3FJ3
41
Japanese 1-2-3 format
*.wj3
xlWK1
5
Lotus 1-2-3 format
*.wk1
xlWK1ALL
31
Lotus 1-2-3 format
*.wk1
xlWK1FMT
30
Lotus 1-2-3 format
*.wk1
xlWK3
15
Lotus 1-2-3 format
*.wk3
xlWK3FM3
32
Lotus 1-2-3 format
*.wk3
xlWK4
38
Lotus 1-2-3 format
*.wk4
xlWKS
4
Lotus 1-2-3 format
*.wks
xlWorkbookDefault
51
Workbook default
*.xlsx
xlWorkbookNormal
-4143
Workbook normal
*.xls
xlWorks2FarEast
28
Microsoft Works 2.0 far east format
*.wks
xlWQ1
34
Quattro Pro format
*.wq1
xlXMLSpreadsheet
46
XML Spreadsheet
*.xml
Constants enumeration (Excel)
This enumeration groups together constants used with various Excel methods.