mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-11 05:43:17 +00:00
Updated readme
This commit is contained in:
104
GetExcelTable.ps1
Normal file
104
GetExcelTable.ps1
Normal file
@@ -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
|
||||||
14
README.md
14
README.md
@@ -27,6 +27,20 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi
|
|||||||
```
|
```
|
||||||
|
|
||||||
# What's new
|
# 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
|
#### 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.
|
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.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user