From df3702a09c7ae0c684d3ba4dd6ce8e7e8b85ee1e Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 23 Nov 2017 12:30:51 -0500 Subject: [PATCH] Updated readme --- GetExcelTable.ps1 | 104 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 14 +++++++ 2 files changed, 118 insertions(+) create mode 100644 GetExcelTable.ps1 diff --git a/GetExcelTable.ps1 b/GetExcelTable.ps1 new file mode 100644 index 0000000..e1aacc0 --- /dev/null +++ b/GetExcelTable.ps1 @@ -0,0 +1,104 @@ +Function Get-ExcelTableName { + Param ( + $Path, + $WorksheetName + ) + + $Path = (Resolve-Path $Path).ProviderPath + $Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite' + + $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream + + if ($WorksheetName) { + $Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName] + } else { + $Worksheet = $Excel.Workbook.Worksheets | Select-Object -First 1 + } + + foreach($TableName in $Worksheet.Tables.Name) { + [PSCustomObject][Ordered]@{ + WorksheetName=$Worksheet.Name + TableName=$TableName + } + } + + $Stream.Close() + $Stream.Dispose() + $Excel.Dispose() + $Excel = $null +} + +Function Get-ExcelTable { + Param ( + $Path, + $TableName, + $WorksheetName + ) + + $Path = (Resolve-Path $Path).ProviderPath + $Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite' + + $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream + + if ($WorksheetName) { + $Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName] + } else { + $Worksheet = $Excel.Workbook.Worksheets | Select-Object -First 1 + } + + if($TableName) { + $Table = $Worksheet.Tables[$TableName] + } else { + $Table = $Worksheet.Tables | Select-Object -First 1 + } + + $rowCount = $Table.Address.Rows + $colCount = $Table.Address.Columns + + $digits = "0123456789".ToCharArray() + + $start, $end=$Table.Address.Address.Split(':') + + $pos=$start.IndexOfAny($digits) + [int]$startCol=ConvertFrom-ExcelColumnName $start.Substring(0,$pos) + [int]$startRow=$start.Substring($pos) + + $propertyNames = for($col=$startCol; $col -lt ($startCol+$colCount); $col+= 1) { + $Worksheet.Cells[$startRow, $col].value + } + + $startRow++ + for($row=$startRow; $row -lt ($startRow+$rowCount); $row += 1) { + $nr=[ordered]@{} + $c=0 + for($col=$startCol; $col -lt ($startCol+$colCount); $col+= 1) { + $nr.($propertyNames[$c]) = $Worksheet.Cells[$row, $col].value + $c++ + } + [pscustomobject]$nr + } + + $Stream.Close() + $Stream.Dispose() + $Excel.Dispose() + $Excel = $null +} + +function ConvertFrom-ExcelColumnName { + param($columnName) + + $sum=0 + $columnName.ToCharArray() | + ForEach { + $sum*=26 + $sum+=[char]$_.tostring().toupper()-[char]'A'+1 + } + $sum +} + +cls + +ipmo .\ImportExcel.psd1 -Force + +#Get-ExcelTableName .\testTable.xlsx | Get-ExcelTable .\testTable.xlsx +Get-ExcelTable .\testTable.xlsx Table3 \ No newline at end of file diff --git a/README.md b/README.md index 03e0c1c..9860f7a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,20 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi ``` # What's new + +#### 11/23/2017 +More great additions and thanks to [James O'Neill](https://twitter.com/jamesoneill) + +* Added `Convert-XlRangeToImage` Gets the specified part of an Excel file and exports it as an image +* Fixed a typo in the message at line 373. +* Now catch an attempt to both clear the sheet and append to it. +* Fixed some issues when appending to sheets where the header isn't in row 1 or the data doesn't start in column 1. +* Added support for more settings when creating a pivot chart. +* Corrected a typo PivotTableName was PivtoTableName in definition of New-PivotTableDefinition +* Add-ConditionalFormat and Set-Format added to the parameters so each has the choice of working more like the other. +* Added Set-Row and Set-Column - fill a formula down or across. +* Added Send-SQLDataToExcel. Insert a rowset and then call Export-Excel for ranges, charts, pivots etc + #### 10/30/2017 Huge thanks to [James O'Neill](https://twitter.com/jamesoneill). PowerShell aficionado. He always brings a flare when working with PowerShell. This is no exception.