mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-12 06:13:26 +00:00
Compare commits
4 Commits
Fix-Import
...
add-header
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6166f0e87c | ||
|
|
bb8297c528 | ||
|
|
b61aef888f | ||
|
|
4c002358fe |
29
Examples/CopyRange/CopyFromOneSheetInSameWorkbook.ps1
Normal file
29
Examples/CopyRange/CopyFromOneSheetInSameWorkbook.ps1
Normal file
@@ -0,0 +1,29 @@
|
||||
<#
|
||||
Copy a range from WorksheetA to WorksheetB
|
||||
#>
|
||||
|
||||
$data = ConvertFrom-Csv @"
|
||||
Region,State,Units,Price
|
||||
West,Texas,927,923.71
|
||||
North,Tennessee,466,770.67
|
||||
East,Florida,520,458.68
|
||||
East,Maine,828,661.24
|
||||
West,Virginia,465,053.58
|
||||
North,Missouri,436,235.67
|
||||
South,Kansas,214,992.47
|
||||
North,North Dakota,789,640.72
|
||||
South,Delaware,712,508.55
|
||||
"@
|
||||
|
||||
$xlfile = "./test.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$data | Export-Excel $xlfile -WorksheetName WorksheetA
|
||||
$data | Export-Excel $xlfile -WorksheetName WorksheetB
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
# Copy a range from WorksheetA to WorksheetB
|
||||
$excel.WorksheetA.Cells["A3:B5"].Copy($excel.WorksheetB.Cells["G3"])
|
||||
|
||||
Close-ExcelPackage $excel -Show
|
||||
24
Examples/HeaderName/ConvertDictionaryOfArraysToExcel.ps1
Normal file
24
Examples/HeaderName/ConvertDictionaryOfArraysToExcel.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
function ConvertTo-Excel {
|
||||
param(
|
||||
$Path,
|
||||
[System.Collections.IDictionary]$targetData
|
||||
)
|
||||
|
||||
$column = 1
|
||||
foreach ($key in $targetData.Keys) {
|
||||
$cityData[$key] | Export-Excel $xlfile -StartColumn ($column++) -HeaderName $key -AutoSize
|
||||
}
|
||||
}
|
||||
|
||||
$cityData = [Ordered]@{}
|
||||
|
||||
$cityData.City = "New York City", "Paris", "Barcelona", "Rome"
|
||||
$cityData.Country = "United States", "France", "Spain", "Italy"
|
||||
$cityData.Population = 8600000, 2141000, 5515000, 2873000
|
||||
|
||||
$xlfile = "$PSScriptRoot/test.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
ConvertTo-Excel $xlfile $cityData
|
||||
|
||||
. $xlfile
|
||||
24
Examples/HeaderName/HeaderName.ps1
Normal file
24
Examples/HeaderName/HeaderName.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return }
|
||||
|
||||
## This exports only the numbers
|
||||
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -Show
|
||||
|
||||
## This exports the numbers and in A1 the text "MyNum"
|
||||
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -HeaderName MyNum -Show
|
||||
|
||||
$xlfile = "$PSScriptRoot/testMultipleColumns.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$Regions = 'West', 'North', 'East ', 'East ', 'West ', 'North', 'South', 'North', 'South'
|
||||
$States = 'Texas', 'Tennessee', 'Florida', 'Maine', 'Virginia', 'Missouri', 'Kansas', 'North Dakota', 'Delaware'
|
||||
$Units = 927, 466, 520, 828, 465, 436, 214, 789, 712
|
||||
$Prices = 923.71, 770.67, 458.68, 661.24, 53.58, 235.67, 992.47, 640.72, 508.55
|
||||
|
||||
# Export each list (array) as a separate column to the same worksheet and workbook
|
||||
$Regions | Export-Excel -Path $xlfile -HeaderName Region -StartColumn 1 -AutoSize
|
||||
$States | Export-Excel -Path $xlfile -HeaderName State -StartColumn 2 -AutoSize
|
||||
$Units | Export-Excel -Path $xlfile -HeaderName Units -StartColumn 3 -AutoSize
|
||||
$Prices | Export-Excel -Path $xlfile -HeaderName Prices -StartColumn 4 -AutoSize
|
||||
|
||||
# Show the results in Excel
|
||||
. $xlfile
|
||||
@@ -49,7 +49,7 @@ function Add-Worksheet {
|
||||
}
|
||||
else {$ExcelWorkbook.Worksheets.MoveBefore($WorksheetName, $MoveBefore)}
|
||||
}
|
||||
else {Write-Warning "Can't find worksheet '$MoveBefore'; worksheet '$WorksheetName' will not be moved."}
|
||||
else {Write-Warning "Can't find worksheet '$MoveBefore'; worsheet '$WorksheetName' will not be moved."}
|
||||
}
|
||||
elseif ($MoveAfter ) {
|
||||
if ($MoveAfter -eq "*") {
|
||||
@@ -68,7 +68,7 @@ function Add-Worksheet {
|
||||
$ExcelWorkbook.Worksheets.MoveAfter($WorksheetName, $MoveAfter)
|
||||
}
|
||||
}
|
||||
else {Write-Warning "Can't find worksheet '$MoveAfter'; worksheet '$WorksheetName' will not be moved."}
|
||||
else {Write-Warning "Can't find worksheet '$MoveAfter'; worsheet '$WorksheetName' will not be moved."}
|
||||
}
|
||||
#endregion
|
||||
if ($Activate) {Select-Worksheet -ExcelWorksheet $ws }
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
[Switch]$Calculate,
|
||||
[Switch]$Show,
|
||||
[String]$WorksheetName = 'Sheet1',
|
||||
[String]$HeaderName,
|
||||
[Alias("PW")]
|
||||
[String]$Password,
|
||||
[switch]$ClearSheet,
|
||||
@@ -56,7 +57,7 @@
|
||||
[Alias('Table')]
|
||||
$TableName,
|
||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
|
||||
[Switch]$BarChart,
|
||||
[Switch]$Barchart,
|
||||
[Switch]$PieChart,
|
||||
[Switch]$LineChart ,
|
||||
[Switch]$ColumnChart ,
|
||||
@@ -258,6 +259,11 @@
|
||||
try {
|
||||
if ($null -eq $InputObject) { $row += 1 }
|
||||
foreach ($TargetData in $InputObject) {
|
||||
|
||||
if ($HeaderName -and $TargetData.psobject.TypeNames[0] -match 'System.String|System.Int32|System.Double|System.Char') {
|
||||
$TargetData = [PSCustomObject]@{ $HeaderName = $TargetData }
|
||||
}
|
||||
|
||||
if ($firstTimeThru) {
|
||||
$firstTimeThru = $false
|
||||
$isDataTypeValueType = ($null -eq $TargetData) -or ($TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort|URI|ExcelHyperLink')
|
||||
|
||||
@@ -16,7 +16,6 @@ function Get-ExcelFileSummary {
|
||||
[PSCustomObject][Ordered]@{
|
||||
ExcelFile = Split-Path -Leaf $Path
|
||||
WorksheetName = $workSheet.Name
|
||||
Visible = $workSheet.Hidden -eq 'Visible'
|
||||
Rows = $workSheet.Dimension.Rows
|
||||
Columns = $workSheet.Dimension.Columns
|
||||
Address = $workSheet.Dimension.Address
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
[Parameter(ParameterSetName = 'PackageC', Mandatory)]
|
||||
[Switch]$NoHeader ,
|
||||
[Alias('HeaderRow', 'TopRow')]
|
||||
[ValidateRange(1, 1048576)]
|
||||
[ValidateRange(1, 9999)]
|
||||
[Int]$StartRow = 1,
|
||||
[Alias('StopRow', 'BottomRow')]
|
||||
[Int]$EndRow ,
|
||||
@@ -235,14 +235,8 @@
|
||||
# Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$($p.Value)' and value '$($Worksheet.Cells[$R, $P.Column].Value)'."
|
||||
}
|
||||
}
|
||||
|
||||
if ($WorksheetName -eq '*') {
|
||||
$xlBook["$targetSheetname"] += [PSCustomObject]$NewRow
|
||||
}
|
||||
else {
|
||||
[PSCustomObject]$NewRow
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -253,8 +247,6 @@
|
||||
# $EndColumn = 0
|
||||
if ($Path) { $stream.close(); $ExcelPackage.Dispose() }
|
||||
|
||||
if ($WorksheetName -eq '*') {
|
||||
|
||||
if ($Raw) {
|
||||
foreach ($entry in $xlbook.GetEnumerator()) {
|
||||
$entry.Value
|
||||
@@ -269,5 +261,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
elseif ($Worksheet -and ($Range -is [string] -or $Range -is [OfficeOpenXml.ExcelAddress])) {
|
||||
$Range = $Worksheet.Cells[$Range]
|
||||
}
|
||||
elseif ($Range -is [string]) {Write-Warning -Message "The range parameter you have specified also needs a worksheet parameter." ;return}
|
||||
elseif ($Range -is [string]) {Write-Warning -Message "The range pararameter you have specified also needs a worksheet parameter." ;return}
|
||||
#else we assume $Range is a range.
|
||||
if ($ClearAll) {
|
||||
$Range.Clear()
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification = 'Only executes on versions without the automatic variable')]
|
||||
param()
|
||||
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
|
||||
Describe 'All tests for Get-ExcelFileSummary' -Tag "Get-ExcelFileSummary" {
|
||||
Context "Test Get-ExcelFileSummary" {
|
||||
@@ -12,7 +14,6 @@ Describe 'All tests for Get-ExcelFileSummary' -Tag "Get-ExcelFileSummary" {
|
||||
|
||||
$actual.ExcelFile | Should -BeExactly 'TestData1.xlsx'
|
||||
$actual.WorksheetName | Should -BeExactly 'Sheet1'
|
||||
$actual.Visible | Should -BeTrue
|
||||
$actual.Rows | Should -Be 3
|
||||
$actual.Columns | Should -Be 2
|
||||
$actual.Address | Should -BeExactly 'A1:B3'
|
||||
@@ -25,7 +26,6 @@ Describe 'All tests for Get-ExcelFileSummary' -Tag "Get-ExcelFileSummary" {
|
||||
|
||||
$actual[0].ExcelFile | Should -BeExactly 'MultipleSheets.xlsx'
|
||||
$actual[0].WorksheetName | Should -BeExactly 'Sheet1'
|
||||
$actual[0].Visible | Should -BeTrue
|
||||
$actual[0].Rows | Should -Be 1
|
||||
$actual[0].Columns | Should -Be 4
|
||||
$actual[0].Address | Should -BeExactly 'A1:D1'
|
||||
@@ -33,20 +33,11 @@ Describe 'All tests for Get-ExcelFileSummary' -Tag "Get-ExcelFileSummary" {
|
||||
|
||||
$actual[1].ExcelFile | Should -BeExactly 'MultipleSheets.xlsx'
|
||||
$actual[1].WorksheetName | Should -BeExactly 'Sheet2'
|
||||
$actual[1].Visible | Should -BeTrue
|
||||
$actual[1].Rows | Should -Be 2
|
||||
$actual[1].Columns | Should -Be 2
|
||||
$actual[1].Address | Should -BeExactly 'A1:B2'
|
||||
$actual[1].Path | Should -Not -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Tests if sheet is hidden or not" {
|
||||
$actual = Get-ExcelFileSummary "$PSScriptRoot\ImportExcelTests\SheetVisibleTesting.xlsx"
|
||||
|
||||
$actual[0].Visible | Should -BeTrue
|
||||
$actual[1].Visible | Should -BeFalse
|
||||
$actual[2].Visible | Should -BeTrue
|
||||
$actual[3].Visible | Should -BeFalse
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
88
__tests__/HeaderName.tests.ps1
Normal file
88
__tests__/HeaderName.tests.ps1
Normal file
@@ -0,0 +1,88 @@
|
||||
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
|
||||
Describe "Test HeaderName parameter" -Tag HeaderName {
|
||||
It "Should add data as usual" {
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
1..10 | Export-Excel -Path $xlfile
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
1..10 | ForEach-Object {
|
||||
$excel.Sheet1.Cells[$_, 1].Text | Should -BeExactly $_
|
||||
}
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should add data and the first cell should have the header name" {
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
1..10 | Export-Excel -Path $xlfile -HeaderName MyNum
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
$excel.Sheet1.Cells[1, 1].Text | Should -BeExactly "MyNum"
|
||||
|
||||
1..10 | ForEach-Object {
|
||||
$excel.Sheet1.Cells[($_ + 1), 1].Text | Should -BeExactly $_
|
||||
}
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should ignore the header name" {
|
||||
$data = ConvertFrom-Csv @"
|
||||
Region,State,Units,Price
|
||||
West,Texas,927,923.71
|
||||
North,Tennessee,466,770.67
|
||||
East,Florida,520,458.68
|
||||
East,Maine,828,661.24
|
||||
West,Virginia,465,053.58
|
||||
North,Missouri,436,235.67
|
||||
South,Kansas,214,992.47
|
||||
North,North Dakota,789,640.72
|
||||
South,Delaware,712,508.55
|
||||
"@
|
||||
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$data | Export-Excel -Path $xlfile -HeaderName MyNum
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
$excel.Sheet1.Cells[1, 1].Text | Should -BeExactly "Region"
|
||||
$excel.Sheet1.Cells[1, 2].Text | Should -BeExactly "State"
|
||||
$excel.Sheet1.Cells[1, 3].Text | Should -BeExactly "Units"
|
||||
$excel.Sheet1.Cells[1, 4].Text | Should -BeExactly "Price"
|
||||
|
||||
$excel.Sheet1.Cells[2, 1].Text | Should -BeExactly "West"
|
||||
$excel.Sheet1.Cells[2, 2].Text | Should -BeExactly "Texas"
|
||||
$excel.Sheet1.Cells[2, 3].Text | Should -Be 927
|
||||
$excel.Sheet1.Cells[2, 4].Text | Should -Be 923.71
|
||||
|
||||
$excel.Sheet1.Cells[10, 1].Text | Should -BeExactly "South"
|
||||
$excel.Sheet1.Cells[10, 2].Text | Should -BeExactly "Delaware"
|
||||
$excel.Sheet1.Cells[10, 3].Text | Should -Be 712
|
||||
$excel.Sheet1.Cells[10, 4].Text | Should -Be 508.55
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
@@ -32,12 +32,6 @@ Describe 'Test' -Tag ImportExcelEndRowAndCols {
|
||||
$colNames[0] | Should -Be 'Units'
|
||||
$colNames[1] | Should -Be 'Price'
|
||||
}
|
||||
|
||||
It 'Should read any row up to maximum allowed row' {
|
||||
$xlMaxRows = "$PSScriptRoot\MaxRows.xlsx"
|
||||
$actual = Import-Excel $xlMaxRows -StartRow 1048576 -EndRow 1048576 -NoHeader
|
||||
$actual.P1 | Should -Be 1048576
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Test reading multiple sheets with data in differnt rows and columns' {
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
|
||||
|
||||
Describe "Tests Import-Excel Timings" -Tag Timing {
|
||||
It "Should read the 20k xlsx in -le 2100 milliseconds" {
|
||||
$timer = Measure-Command {
|
||||
$data = Import-Excel $PSScriptRoot\TimingRows20k.xlsx
|
||||
}
|
||||
|
||||
$timer.TotalMilliseconds | Should -BeLessOrEqual 2100
|
||||
$data.Count | Should -Be 19999
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
15
changelog.md
15
changelog.md
@@ -1,10 +1,15 @@
|
||||
# 7.8.2
|
||||
|
||||
- Fix docs [#1254](https://github.com/dfinke/ImportExcel/pull/1251)`Add-Worksheet` warning. Thank you [Wilson Stewart](https://github.com/WilsonStewart)
|
||||
- Fix docs [#1251](https://github.com/dfinke/ImportExcel/pull/1251)`Add-Worksheet` warning. Thank you [Jeremiah Adams](https://github.com/JeremiahTheFirst)
|
||||
- Fix docs [#1253](https://github.com/dfinke/ImportExcel/pull/1253) `convertfrom-exceltosqlinsert`. Thank you [Wes Stahler](https://github.com/stahler)
|
||||
- Set Validate Range for rows to max rows available [#1273](https://github.com/dfinke/ImportExcel/pull/1273). Thank you [Stephen Brown](https://github.com/steve-daedilus)
|
||||
- Extended Get-ExcelFileSummary to include more Visible -eq $true|$false
|
||||
- Added `HeaderName` parameter to `Export-Excel`. Allows you to output an object with a property name. Otherwise the data is just the array of values.
|
||||
|
||||
```powershell
|
||||
1..10 | Export-Excel -Path .\test.xlsx -HeaderName MyNum
|
||||
```
|
||||
|
||||
- Added example `CopyFromOneSheetInSameWorkbook`. Shows how to copy a range of data from WorksheetA to WorksheetB
|
||||
- Added example `HeaderName`. Shows how to use the new `-HeaderName` parameter
|
||||
- Added example `HeaderName`. Shows how to use the new `-HeaderName` parameter
|
||||
- Added example `ConvertDictionaryOfArraysToExcel` example. Takes a dictionary of arrays and converts it Rows and Columns of data in Excel.
|
||||
|
||||
# 7.8.1
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ Accept wildcard characters: False
|
||||
|
||||
### -Header
|
||||
|
||||
Specifies custom property names to use, instead of the values defined in the column headers of the TopRow. If you provide fewer header names than there is data in the worksheet, then only the data with a corresponding header name will be imported and the data without header name will be disregarded. If you provide more header names than there is data in the worksheet, then all data will be imported and all objects will have all the property names you defined in the header names. As such, the last properties will be blank as there is no data for them.
|
||||
Specifies custom property names to use, instead of the values defined in the column headers of the TopRow. If you provide fewr header names than there is data in the worksheet, then only the data with a corresponding header name will be imported and the data without header name will be disregarded. If you provide more header names than there is data in the worksheet, then all data will be imported and all objects will have all the property names you defined in the header names. As such, the last properties will be blank as there is no data for them.
|
||||
|
||||
```yaml
|
||||
Type: String[]
|
||||
|
||||
Reference in New Issue
Block a user