Compare commits

...

79 Commits

Author SHA1 Message Date
Doug Finke
2a8cbbba7c Merge pull request #1197 from dfinke/fix-1194-EndRow-and-EndColumn-Parameters-Ignored
Fix 1194 end row and end column parameters ignored
2022-06-12 11:43:33 -04:00
dfinke
b789db9ba3 bump version 2022-06-12 11:32:12 -04:00
dfinke
89a4cfde0d fix test 2022-06-12 10:49:05 -04:00
dfinke
ab56ae4409 Add additional tests 2022-06-12 10:42:16 -04:00
dfinke
ef35c4fca8 Fix for EndRow and EndColumn Parameters Ignored in Import-Excel #1194 2022-06-12 09:53:52 -04:00
dfinke
8bb3d50052 Add sample - How to convert abbreviate or shorten long numbers in Excel 2022-05-29 09:54:02 -04:00
dfinke
d5b807d3cb Don't publish images folder and more - Suggestion: Remove unnecessary files when publishing to the gallery #1187 2022-05-28 06:32:01 -04:00
Doug Finke
e2053e4998 fix casing 2022-05-07 17:19:15 -04:00
Doug Finke
8c6c7eeaf6 update 2022-05-07 17:15:24 -04:00
Doug Finke
31abd215dc Merge pull request #1181 from dfinke/update-read-me
Update read me
2022-05-07 09:50:06 -04:00
dfinke
cafaafd53d add link to original readme 2022-05-07 09:45:16 -04:00
dfinke
ee3c8e7680 use / 2022-05-07 09:40:18 -04:00
dfinke
0b207548e0 fix link 2022-05-07 09:39:52 -04:00
dfinke
6628b55ce5 update link 2022-05-07 09:38:28 -04:00
dfinke
f6bfabd96a remove encoding 2022-05-07 09:34:02 -04:00
dfinke
980631df8b update xlsx link 2022-05-07 09:33:08 -04:00
dfinke
4f9b83f0e6 update logo 2022-05-07 08:17:19 -04:00
dfinke
59cf89b451 Additional updates 2022-05-07 08:08:49 -04:00
dfinke
5777d907c4 added Azure pipeline badge 2022-05-06 13:11:56 -04:00
dfinke
d7c516d35e Merge branch 'update-read-me' of https://github.com/dfinke/ImportExcel into update-read-me 2022-05-06 13:01:21 -04:00
dfinke
27d69e7531 Update pointing to examples 2022-05-06 13:01:12 -04:00
Doug Finke
8ae92fba54 spelling 2022-05-05 21:19:24 -04:00
dfinke
aa0f0cf4e2 layout 2022-05-05 18:36:19 -04:00
dfinke
6e38d97498 fix header 2022-05-05 18:35:59 -04:00
dfinke
ef7c8331f5 add links for versions, donate etc 2022-05-05 18:34:56 -04:00
dfinke
267d15f99a Add videos and articles 2022-05-05 18:21:27 -04:00
dfinke
390aca0496 update 2022-05-05 16:58:15 -04:00
dfinke
4fa34ae257 Rename switch NotAsDictionary to Raw 2022-05-04 18:33:19 -04:00
dfinke
0b3b382c4e update 2022-05-04 18:32:27 -04:00
dfinke
415be5bca3 bump version 2022-05-04 18:32:19 -04:00
dfinke
79e5bdf8ba update 2022-05-03 17:54:21 -04:00
dfinke
b03d9b048a bump version 2022-05-03 17:54:16 -04:00
dfinke
aea90aa8d6 Fix reading multiple sheets in the xlsx where row/col count is diff 2022-05-03 17:53:58 -04:00
dfinke
d4ebc9e95d Use explicit parameter names 2022-04-30 13:17:20 -04:00
Doug Finke
6ece4483e2 Merge pull request #1171 from dfinke/add-helper-functions
Add helper functions `Enable-ExcelAutoFilter`,  `Enable-ExcelAutofit`, `Get-ExcelSheetDimensionAddress`
2022-04-30 08:35:08 -04:00
dfinke
ba14511254 update changelog 2022-04-30 08:21:24 -04:00
dfinke
964cf9e9c6 Merge branch 'master' of https://github.com/dfinke/ImportExcel into add-helper-functions 2022-04-30 08:21:18 -04:00
Doug Finke
8d56a351ff Merge pull request #1161 from dfinke/adding-to-import-excel
Update and improve Import-Excel for reading nultiple sheets
2022-04-30 08:04:39 -04:00
dfinke
b2119f08f5 add example for importing an xlsx and multiple sheets 2022-04-30 07:59:22 -04:00
dfinke
634aaacc55 update change log 2022-04-30 07:58:55 -04:00
dfinke
61b44f826d Merge branch 'master' of https://github.com/dfinke/ImportExcel into adding-to-import-excel 2022-04-30 07:25:58 -04:00
Doug Finke
9273261b34 Merge pull request #1173 from dfinke/fix-bug-#1172
Fix bug #1172
2022-04-30 07:10:57 -04:00
dfinke
e55dfdd10e Update changes 2022-04-30 07:05:10 -04:00
dfinke
59e40d62d0 bump version 2022-04-30 07:05:04 -04:00
dfinke
f478e8a134 Add Enable-ExcelAutoFilter and Enable-ExcelAutofit example 2022-04-29 22:11:24 -04:00
dfinke
b634bf9d93 Fix fir #1172 2022-04-29 20:39:51 -04:00
dfinke
a10ade898a Remove xl file after use in test 2022-04-29 20:39:15 -04:00
dfinke
eb10364722 Add helpers 2022-04-28 19:44:49 -04:00
dfinke
b0024cf2c4 bump version 2022-04-28 19:44:40 -04:00
dfinke
91b0e8b0ed update https://github.com/dfinke/ImportExcel/pull/1145 2022-04-19 09:26:20 -04:00
dfinke
b997e90e78 bump version 2022-04-19 09:23:36 -04:00
Doug Finke
4d8710d017 Merge pull request #1145 from jamesmmueller/master
Allow for escaping of single quote in data.
2022-04-19 09:12:20 -04:00
dfinke
7f36b44fa8 renamed to NotAsDictionary 2022-04-12 18:08:11 -04:00
dfinke
483f761016 Add switch to not return data as a dictionary 2022-04-12 18:03:34 -04:00
dfinke
d55f0e64d4 WIP - Add sheets and contents to a hashtable 2022-04-11 18:48:17 -04:00
dfinke
f83f654c4a Update how Import-Excel reads sheets, and remain backward compatible 2022-04-11 16:58:55 -04:00
dfinke
6a956dbd7e delete xlsx after tests run 2022-04-11 16:58:13 -04:00
Doug Finke
0929a442a5 Merge pull request #1149 from caosborn/CO_Examples-AddWorksheetVBA
Adding VBA examples for worksheets
2022-03-27 10:21:15 -04:00
Doug Finke
895a5c0cb2 Merge pull request #1150 from dfinke/isolate-build-issues
Fixes `Get-Service` fail on Windows 2022
2022-03-27 09:44:52 -04:00
dfinke
d7901af8f3 try always importing the clixml 2022-03-27 09:37:15 -04:00
dfinke
34f55a3659 suspect Get-Service 2022-03-25 19:32:53 -04:00
dfinke
606988bcf6 rigger build 2022-03-25 18:45:06 -04:00
Craig Osborn
441f2ada22 fix: Typo in filename 2022-03-25 08:43:48 +00:00
Craig Osborn
29ea7012d7 examples: Add VBA module and call in all worksheets 2022-03-25 08:36:02 +00:00
Craig Osborn
67ac63ddcf examples: Add VBA to single worksheet 2022-03-25 08:35:13 +00:00
James Mueller
c939c6ecb0 Merge pull request #1 from jamesmmueller/jamesmmueller-patch-1
ConvertFrom-ExcelToSQLInsert fix single quotes
2022-03-17 10:55:37 -05:00
James Mueller
96493e059b ConvertFrom-ExcelToSQLInsert fix single quotes
If data in the Excel file contains single quotes, then the insert will bomb as it will be malformed. Quotes would need escaped. Not all languages will support the same escape method, so I recommend it being set by an optional parameter.

Example of what will fail:
  Name
  -----------
  Fry's

INSERT INTO [Name] Values ( 'Fry's' ) 

Corrected for quotes (such as needing to escape a single quote with an additional single quote)

INSERT INTO [Name] Values ('Fry''s') 


Example code:
$ConvertToSqlParams = @{TableName = ‘dbo.Names’
Path = ‘C:\temp\data.xlsx’
ConvertEmptyStringsToNull = $true
UseMSSQLSyntax = $true
SingleQuoteStyle = "''"
}

$SQLInsert = ConvertFrom-ExcelToSqlInsert @ConvertToSqlParams
2022-03-11 16:09:38 -06:00
Doug Finke
cb7f2a06f4 Remove vscode badge 2022-03-06 14:52:37 -05:00
Doug Finke
99beda7a10 Move to mixed case folders 2022-03-06 14:51:30 -05:00
dfinke
0c8bb2300a update links 2022-02-04 17:31:39 -05:00
dfinke
c09083d350 updated 2022-02-04 17:30:40 -05:00
dfinke
556f0ac51e Updated changelog 2022-02-04 17:28:30 -05:00
Doug Finke
1e4fc59a25 Merge pull request #1133 from joshooaj/master
Add Add-ExcelImage example
2022-02-04 15:48:05 -05:00
dfinke
5ab6a9116d @joshooaj added throw if not on Windows, pls review 2022-02-03 18:43:15 -05:00
Josh Hendricks
f33215382a docs: rename to AddImage for consistency 2022-02-02 08:31:35 -08:00
Josh Hendricks
cf5d3f83d6 docs: Update readme 2022-02-02 08:16:22 -08:00
Josh Hendricks
91da711635 docs: add Add-ExcelImage example 2022-02-01 21:37:55 -08:00
Doug Finke
eec5e343d4 Merge pull request #1118 from dfinke/add-freeze-pane-example
Add FreezePane Example
2021-12-30 14:46:52 -05:00
dfinke
d69b640edc Add FreezePane Example 2021-12-30 14:39:33 -05:00
80 changed files with 2266 additions and 1398 deletions

View File

@@ -0,0 +1,119 @@
function Add-ExcelImage {
<#
.SYNOPSIS
Adds an image to a worksheet in an Excel package.
.DESCRIPTION
Adds an image to a worksheet in an Excel package using the
`WorkSheet.Drawings.AddPicture(name, image)` method, and places the
image at the location specified by the Row and Column parameters.
Additional position adjustment can be made by providing RowOffset and
ColumnOffset values in pixels.
.EXAMPLE
$image = [System.Drawing.Image]::FromFile($octocat)
$xlpkg = $data | Export-Excel -Path $path -PassThru
$xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell
Where $octocat is a path to an image file, and $data is a collection of
data to be exported, and $path is the output path for the Excel document,
Add-Excel places the image at row 4 and column 6, resizing the column
and row as needed to fit the image.
.INPUTS
[OfficeOpenXml.ExcelWorksheet]
.OUTPUTS
None
#>
[CmdletBinding()]
param(
# Specifies the worksheet to add the image to.
[Parameter(Mandatory, ValueFromPipeline)]
[OfficeOpenXml.ExcelWorksheet]
$WorkSheet,
# Specifies the Image to be added to the worksheet.
[Parameter(Mandatory)]
[System.Drawing.Image]
$Image,
# Specifies the row where the image will be placed. Rows are counted from 1.
[Parameter(Mandatory)]
[ValidateRange(1, [int]::MaxValue)]
[int]
$Row,
# Specifies the column where the image will be placed. Columns are counted from 1.
[Parameter(Mandatory)]
[ValidateRange(1, [int]::MaxValue)]
[int]
$Column,
# Specifies the name to associate with the image. Names must be unique per sheet.
# Omit the name and a GUID will be used instead.
[Parameter()]
[string]
$Name,
# Specifies the number of pixels to offset the image on the Y-axis. A
# positive number moves the image down by the specified number of pixels
# from the top border of the cell.
[Parameter()]
[int]
$RowOffset = 1,
# Specifies the number of pixels to offset the image on the X-axis. A
# positive number moves the image to the right by the specified number
# of pixels from the left border of the cell.
[Parameter()]
[int]
$ColumnOffset = 1,
# Increase the column width and row height to fit the image if the current
# dimensions are smaller than the image provided.
[Parameter()]
[switch]
$ResizeCell
)
begin {
if ($IsWindows -eq $false) {
throw "This only works on Windows and won't run on $([environment]::OSVersion)"
}
<#
These ratios work on my machine but it feels fragile. Need to better
understand how row and column sizing works in Excel and what the
width and height units represent.
#>
$widthFactor = 1 / 7
$heightFactor = 3 / 4
}
process {
if ([string]::IsNullOrWhiteSpace($Name)) {
$Name = (New-Guid).ToString()
}
if ($null -ne $WorkSheet.Drawings[$Name]) {
Write-Error "A picture with the name `"$Name`" already exists in worksheet $($WorkSheet.Name)."
return
}
<#
The row and column offsets of 1 ensures that the image lands just
inside the gray cell borders at the top left.
#>
$picture = $WorkSheet.Drawings.AddPicture($Name, $Image)
$picture.SetPosition($Row - 1, $RowOffset, $Column - 1, $ColumnOffset)
if ($ResizeCell) {
<#
Adding 1 to the image height and width ensures that when the
row and column are resized, the bottom right of the image lands
just inside the gray cell borders at the bottom right.
#>
$width = $widthFactor * ($Image.Width + 1)
$height = $heightFactor * ($Image.Height + 1)
$WorkSheet.Column($Column).Width = [Math]::Max($width, $WorkSheet.Column($Column).Width)
$WorkSheet.Row($Row).Height = [Math]::Max($height, $WorkSheet.Row($Row).Height)
}
}
}

View File

@@ -0,0 +1,39 @@
if ($IsWindows -eq $false) {
throw "This only works on Windows and won't run on $([environment]::OSVersion)"
}
Add-Type -AssemblyName System.Drawing
. $PSScriptRoot\Add-ExcelImage.ps1
$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
"@
$path = "$PSScriptRoot/Add-Picture-test.xlsx"
Remove-Item $path -ErrorAction SilentlyContinue
try {
$octocat = "$PSScriptRoot/Octocat.jpg"
$image = [System.Drawing.Image]::FromFile($octocat)
$xlpkg = $data | Export-Excel -Path $path -PassThru
$xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell
}
finally {
if ($image) {
$image.Dispose()
}
if ($xlpkg) {
Close-ExcelPackage -ExcelPackage $xlpkg -Show
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -0,0 +1,33 @@
# Add-ExcelImage Example
Adding pictures to an Excel worksheet is possible by calling the `AddPicture(name, image)`
method on the `Drawings` property of an `ExcelWorksheet` object.
The `Add-ExcelImage` example here demonstrates how to add a picture at a given
cell location, and optionally resize the row and column to fit the image.
## Running the example
To try this example, run the script `AddImage.ps1`. The `Add-ExcelImage`
function will be dot-sourced, and an Excel document will be created in the same
folder with a sample data set. The Octocat image will then be embedded into
Sheet1.
The creation of the Excel document and the `System.Drawing.Image` object
representing Octocat are properly disposed within a `finally` block to ensure
that the resources are released, even if an error occurs in the `try` block.
## Note about column and row sizing
Care has been taken in this example to get the image placement to be just inside
the cell border, and if the `-ResizeCell` switch is present, the height and width
of the row and column will be increased, if needed, so that the bottom right of
the image also lands just inside the cell border.
The Excel row and column sizes are measured in "point" units rather than pixels,
and a fixed multiplication factor is used to convert the size of the image in
pixels, to the corresponding height and width values in Excel.
It's possible that different DPI or text scaling options could result in
imperfect column and row sizing and if a better strategy is found for converting
the image dimensions to column and row sizes, this example will be updated.

View File

@@ -0,0 +1,23 @@
# How to convert abbreviate or shorten long numbers in Excel
Remove-Item .\custom.xlsx -ErrorAction SilentlyContinue
$data = $(
12000
1000
2000
3000
2400
3600
6000
13000
40000
400000
1000000
)
$excel = $data | Export-Excel .\custom.xlsx -PassThru
Set-ExcelRange -Worksheet $excel.Sheet1 -Range "A:A" -NumberFormat '[>999999]#,,"M";#,"K"'
Close-ExcelPackage $excel -Show

View File

@@ -0,0 +1,54 @@
# Freeze the columns/rows to left and above the cell
$data = ConvertFrom-Csv @"
Region,State,Units,Price,Name,NA,EU,JP,Other
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
West,Texas,927,923.71,Wii Sports,41.49,29.02,3.77,8.46
"@
$xlfilename = "test.xlsx"
Remove-Item $xlfilename -ErrorAction SilentlyContinue
<#
Freezes the top two rows and the two leftmost column
#>
$data | Export-Excel $xlfilename -Show -Title 'Sales Data' -FreezePane 3, 3

View File

@@ -0,0 +1,7 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
$xlfile = "$PSScriptRoot\yearlySales.xlsx"
$result = Import-Excel -Path $xlfile -WorksheetName * -Raw
$result | Measure-Object

View File

@@ -0,0 +1,9 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
$xlfile = "$PSScriptRoot\yearlySales.xlsx"
$result = Import-Excel -Path $xlfile -WorksheetName *
foreach ($sheet in $result.Values) {
$sheet
}

Binary file not shown.

View File

@@ -0,0 +1,28 @@
# How to use Enable-ExcelAutoFilter and Enable-ExcelAutofit
try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return }
$data = ConvertFrom-Csv @"
RegionInfo,StateInfo,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 = "$PSScriptRoot\enableFeatures.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$data | Export-Excel $xlfile
$excel = Open-ExcelPackage $xlfile
Enable-ExcelAutoFilter -Worksheet $excel.Sheet1
Enable-ExcelAutofit -Worksheet $excel.Sheet1
Close-ExcelPackage $excel -Show

View File

@@ -0,0 +1,64 @@
$xlfile = "$env:temp\test.xlsm"
Remove-Item $xlfile -ErrorAction SilentlyContinue
ConvertFrom-Csv @"
Region,Item,TotalSold
West,screwdriver,98
West,kiwi,19
North,kiwi,47
West,screws,48
West,avocado,52
East,avocado,40
South,drill,61
North,orange,92
South,drill,29
South,saw,36
"@ | Export-Excel $xlfile -TableName 'Sales' -WorksheetName 'Sales' -AutoSize
$Excel = ConvertFrom-Csv @"
Supplier,Item,TotalBought
Hardware,screwdriver,98
Groceries,kiwi,19
Hardware,screws,48
Groceries,avocado,52
Hardware,drill,61
Groceries,orange,92
Hardware,drill,29
HArdware,saw,36
"@ | Export-Excel $xlfile -TableName 'Purchases' -WorksheetName 'Purchases' -PassThru -AutoSize
$wb = $Excel.Workbook
$wb.CreateVBAProject()
# Create a module with a sub to highlight the selected row & column of the active table.
# https://docs.microsoft.com/en-gb/office/vba/excel/Concepts/Cells-and-Ranges/highlight-the-active-cell-row-or-column
$codeModule = @"
Public Sub HighlightSelection(ByVal Target As Range)
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
' Highlight the row and column that contain the active cell, within the current region
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 38
Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 24
End With
Application.ScreenUpdating = True
End Sub
"@
$module = $wb.VbaProject.Modules.AddModule("PSExcelModule")
$module.Code = $codeModule
# Add a call to the row & column highlight sub on each worksheet.
$codeSheet = @"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
HighlightSelection Target
End Sub
"@
foreach ($sheet in $wb.Worksheets) {
$sheet.CodeModule.Code = $codeSheet
}
Close-ExcelPackage $Excel -Show

View File

@@ -0,0 +1,41 @@
$xlfile = "$env:temp\test.xlsm"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$Excel = ConvertFrom-Csv @"
Region,Item,TotalSold
West,screwdriver,98
West,kiwi,19
North,kiwi,47
West,screws,48
West,avocado,52
East,avocado,40
South,drill,61
North,orange,92
South,drill,29
South,saw,36
"@ | Export-Excel $xlfile -TableName 'Sales' -WorksheetName 'Sales' -AutoSize -PassThru
$wb = $Excel.Workbook
$sheet = $wb.Worksheets["Sales"]
$wb.CreateVBAProject()
# Add a sub to the 'Worksheet_SelectionChange' event of the worksheet to highlight the selected row & column of the active table.
# https://docs.microsoft.com/en-gb/office/vba/excel/Concepts/Cells-and-Ranges/highlight-the-active-cell-row-or-column
$code = @"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
' Highlight the row and column that contain the active cell, within the current region
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 38
Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 24
End With
Application.ScreenUpdating = True
End Sub
"@
$sheet.CodeModule.Code = $code
Close-ExcelPackage $Excel -Show

View File

@@ -6,7 +6,7 @@
RootModule = 'ImportExcel.psm1'
# Version number of this module.
ModuleVersion = '7.4.1'
ModuleVersion = '7.6.0'
# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
@@ -48,11 +48,14 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
'ConvertTo-ExcelXlsx',
'Copy-ExcelWorksheet',
'DoChart',
'Enable-ExcelAutoFilter',
'Enable-ExcelAutofit',
'Expand-NumberFormat',
'Export-Excel',
'Export-ExcelSheet',
'Get-ExcelColumnName',
'Get-ExcelFileSummary',
'Get-ExcelFileSummary',
'Get-ExcelSheetDimensionAddress',
'Get-ExcelSheetInfo',
'Get-ExcelWorkbookInfo',
'Get-HtmlTable',
@@ -63,8 +66,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
'Import-UPS',
'Import-USPS',
'Invoke-AllTests',
'Invoke-Sum',
'Invoke-ExcelQuery',
'Invoke-Sum',
'Join-Worksheet',
'LineChart',
'Merge-MultipleSheets',
@@ -80,8 +83,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
'PieChart',
'Pivot',
'Read-Clipboard',
'ReadClipboardImpl',
'Read-OleDbData',
'ReadClipboardImpl',
'Remove-Worksheet',
'Select-Worksheet',
'Send-SQLDataToExcel',
@@ -128,9 +131,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
'.\Charting\Charting.ps1',
'.\InferData\InferData.ps1',
'.\Pivot\Pivot.ps1',
'.\spikes\ConvertFrom-ExcelColumnName.ps1',
'.\Examples', '.\images', '.\Testimonials'
'.\Examples',
'.\Testimonials'
)
# Private data to pass to the module specified in RootModule/ModuleToProcess

View File

@@ -7,5 +7,5 @@ if (-not $fullPath) {
$fullPath = Join-Path $fullPath -ChildPath "ImportExcel"
}
Push-location $PSScriptRoot
Robocopy . $fullPath /mir /XD .vscode .git CI __tests__ data mdHelp /XF appveyor.yml azure-pipelines.yml .gitattributes .gitignore filelist.txt install.ps1 InstallModule.ps1
Robocopy . $fullPath /mir /XD .vscode images .git .github CI __tests__ data mdHelp spikes /XF README.md README.original.md appveyor.yml azure-pipelines.yml .gitattributes .gitignore filelist.txt install.ps1 InstallModule.ps1 PublishToGallery.ps1
Pop-Location

View File

@@ -16,12 +16,15 @@ function ConvertFrom-ExcelToSQLInsert {
[switch]$NoHeader,
[switch]$DataOnly,
[switch]$ConvertEmptyStringsToNull,
[switch]$UseMsSqlSyntax
[switch]$UseMsSqlSyntax,
[Parameter(Mandatory = $false)]
$SingleQuoteStyle
)
$null = $PSBoundParameters.Remove('TableName')
$null = $PSBoundParameters.Remove('ConvertEmptyStringsToNull')
$null = $PSBoundParameters.Remove('UseMsSqlSyntax')
$null = $PSBoundParameters.Remove('SingleQuoteStyle')
$params = @{} + $PSBoundParameters
@@ -38,11 +41,16 @@ function ConvertFrom-ExcelToSQLInsert {
'NULL'
}
else {
"'" + $record.$propertyName + "'"
if ( $SingleQuoteStyle ) {
"'" + $record.$propertyName.ToString().Replace("'",${SingleQuoteStyle}) + "'"
}
else {
"'" + $record.$propertyName + "'"
}
}
}
$targetValues = ($values -join ", ")
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
}
}
}

View File

@@ -0,0 +1,16 @@
function Enable-ExcelAutoFilter {
<#
.SYNOPSIS
Enable the Excel AutoFilter
.EXAMPLE
Enable-ExcelAutoFilter $targetSheet
#>
param(
[Parameter(Mandatory)]
[OfficeOpenXml.ExcelWorksheet]$Worksheet
)
$range = Get-ExcelSheetDimensionAddress $Worksheet
$Worksheet.Cells[$range].AutoFilter = $true
}

View File

@@ -0,0 +1,16 @@
function Enable-ExcelAutofit {
<#
.SYNOPSIS
Make all text fit the cells
.EXAMPLE
Enable-ExcelAutofit $excel.Sheet1
#>
param(
[Parameter(Mandatory)]
[OfficeOpenXml.ExcelWorksheet]$Worksheet
)
$range = Get-ExcelSheetDimensionAddress $Worksheet
$Worksheet.Cells[$range].AutoFitColumns()
}

View File

@@ -0,0 +1,15 @@
function Get-ExcelSheetDimensionAddress {
<#
.SYNOPSIS
Get the Excel address of the dimension of a sheet
.EXAMPLE
Get-ExcelSheetDimensionAddress $excel.Sheet1
#>
param(
[Parameter(Mandatory)]
[OfficeOpenXml.ExcelWorksheet]$Worksheet
)
$Worksheet.Dimension.Address
}

View File

@@ -15,7 +15,7 @@
[Alias('Sheet')]
[Parameter(Position = 1)]
[ValidateNotNullOrEmpty()]
[String]$WorksheetName,
[String[]]$WorksheetName,
[Parameter(ParameterSetName = 'PathB' , Mandatory)]
[Parameter(ParameterSetName = 'PackageB', Mandatory)]
[String[]]$HeaderName ,
@@ -36,7 +36,8 @@
[string[]]$AsDate,
[ValidateNotNullOrEmpty()]
[String]$Password,
[Int[]]$ImportColumns
[Int[]]$ImportColumns,
[Switch]$Raw
)
end {
$sw = [System.Diagnostics.Stopwatch]::StartNew()
@@ -64,7 +65,7 @@
try {
if ($ImportColumns) {
$end = $Worksheet.Dimension.End.Column
$end = $sheet.Dimension.End.Column
# Check $ImportColumns
if ($ImportColumns[0] -le 0) { throw "The first entry in ImportColumns must be equal or greater 1" ; return }
# Check $StartColumn and $EndColumn
@@ -95,7 +96,7 @@
foreach ($C in $Columns) {
#allow "False" or "0" to be column headings
$Worksheet.Cells[$StartRow, $C] | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Select-Object @{N = 'Column'; E = { $C } }, Value
$sheet.Cells[$StartRow, $C] | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Select-Object @{N = 'Column'; E = { $C } }, Value
}
}
}
@@ -125,108 +126,138 @@
}
try {
#Select worksheet
if (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] }
if ($WorksheetName -eq '*') { $Worksheet = $ExcelPackage.Workbook.Worksheets }
elseif (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] }
elseif (-not ($Worksheet = $ExcelPackage.Workbook.Worksheets[$WorksheetName])) {
throw "Worksheet '$WorksheetName' not found, the workbook only contains the worksheets '$($ExcelPackage.Workbook.Worksheets)'. If you only wish to select the first worksheet, please remove the '-WorksheetName' parameter." ; return
}
#region Get rows and columns
#If we are doing dataonly it is quicker to work out which rows to ignore before processing the cells.
if (-not $EndRow ) { $EndRow = $Worksheet.Dimension.End.Row }
if (-not $EndColumn) { $EndColumn = $Worksheet.Dimension.End.Column }
$endAddress = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$EndRow]C[$EndColumn]", 0, 0)
if ($DataOnly) {
#If we are using headers startrow will be the header-row so examine data from startRow + 1,
if ($NoHeader) { $range = "A" + ($StartRow ) + ":" + $endAddress }
else { $range = "A" + ($StartRow + 1 ) + ":" + $endAddress }
#We're going to look at every cell and build 2 hash tables holding rows & columns which contain data.
#Want to Avoid 'select unique' operations & large Sorts, becuse time time taken increases with square
#of number of items (PS uses heapsort at large size). Instead keep a list of what we have seen,
#using Hash tables: "we've seen it" is all we need, no need to worry about "seen it before" / "Seen it many times".
$colHash = @{ }
$rowHash = @{ }
foreach ($cell in $Worksheet.Cells[$range]) {
if ($null -ne $cell.Value ) { $colHash[$cell.Start.Column] = 1; $rowHash[$cell.Start.row] = 1 }
}
$rows = ( $StartRow..$EndRow ).Where( { $rowHash[$_] })
$columns = ($StartColumn..$EndColumn).Where( { $colHash[$_] })
}
else {
$Columns = $StartColumn .. $EndColumn ; if ($StartColumn -gt $EndColumn) { Write-Warning -Message "Selecting columns $StartColumn to $EndColumn might give odd results." }
if ($NoHeader) { $rows = $StartRow..$EndRow ; if ($StartRow -gt $EndRow) { Write-Warning -Message "Selecting rows $StartRow to $EndRow might give odd results." } }
elseif ($HeaderName) { $rows = $StartRow..$EndRow }
else {
$rows = (1 + $StartRow)..$EndRow
if ($StartRow -eq 1 -and $EndRow -eq 1) {
$rows = 0
}
$xlBook = [Ordered]@{}
foreach ($sheet in $Worksheet) {
if ($Worksheet.Count -gt 1 -or $Paths.Count -gt 1) {
<#
Needed under these conditions to handle sheets of different number of Row/Col
- When reading more than one xlsx file
- When reading more than one worksheet in the same file
#>
$EndRow = $null
$EndColumn = $null
}
# ; if ($StartRow -ge $EndRow) { Write-Warning -Message "Selecting $StartRow as the header with data in $(1+$StartRow) to $EndRow might give odd results." } }
}
#endregion
#region Create property names
if ((-not $Columns) -or (-not ($PropertyNames = Get-PropertyNames -Columns $Columns -StartRow $StartRow))) {
throw "No column headers found on top row '$StartRow'. If column headers in the worksheet are not a requirement then please use the '-NoHeader' or '-HeaderName' parameter."; return
}
if ($Duplicates = $PropertyNames | Group-Object Value | Where-Object Count -GE 2) {
throw "Duplicate column headers found on row '$StartRow' in columns '$($Duplicates.Group.Column)'. Column headers must be unique, if this is not a requirement please use the '-NoHeader' or '-HeaderName' parameter."; return
}
#endregion
if (-not $rows) {
Write-Warning "Worksheet '$WorksheetName' in workbook '$Path' contains no data in the rows after top row '$StartRow'"
}
else {
#region Create one object per row
if ($AsText -or $AsDate) {
<#join items in AsText together with ~~~ . Escape any regex special characters...
$targetSheetname = $sheet.Name
$xlBook["$targetSheetname"] = @()
#region Get rows and columns
#If we are doing dataonly it is quicker to work out which rows to ignore before processing the cells.
if (-not $EndRow ) { $EndRow = $sheet.Dimension.End.Row }
if (-not $EndColumn) { $EndColumn = $sheet.Dimension.End.Column }
$endAddress = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$EndRow]C[$EndColumn]", 0, 0)
if ($DataOnly) {
#If we are using headers startrow will be the header-row so examine data from startRow + 1,
if ($NoHeader) { $range = "A" + ($StartRow ) + ":" + $endAddress }
else { $range = "A" + ($StartRow + 1 ) + ":" + $endAddress }
#We're going to look at every cell and build 2 hash tables holding rows & columns which contain data.
#Want to Avoid 'select unique' operations & large Sorts, becuse time time taken increases with square
#of number of items (PS uses heapsort at large size). Instead keep a list of what we have seen,
#using Hash tables: "we've seen it" is all we need, no need to worry about "seen it before" / "Seen it many times".
$colHash = @{ }
$rowHash = @{ }
foreach ($cell in $sheet.Cells[$range]) {
if ($null -ne $cell.Value ) { $colHash[$cell.Start.Column] = 1; $rowHash[$cell.Start.row] = 1 }
}
$rows = ( $StartRow..$EndRow ).Where( { $rowHash[$_] })
$columns = ($StartColumn..$EndColumn).Where( { $colHash[$_] })
}
else {
$Columns = $StartColumn .. $EndColumn ; if ($StartColumn -gt $EndColumn) { Write-Warning -Message "Selecting columns $StartColumn to $EndColumn might give odd results." }
if ($NoHeader) { $rows = $StartRow..$EndRow ; if ($StartRow -gt $EndRow) { Write-Warning -Message "Selecting rows $StartRow to $EndRow might give odd results." } }
elseif ($HeaderName) { $rows = $StartRow..$EndRow }
else {
$rows = (1 + $StartRow)..$EndRow
if ($StartRow -eq 1 -and $EndRow -eq 1) {
$rows = 0
}
}
# ; if ($StartRow -ge $EndRow) { Write-Warning -Message "Selecting $StartRow as the header with data in $(1+$StartRow) to $EndRow might give odd results." } }
}
#endregion
#region Create property names
if ((-not $Columns) -or (-not ($PropertyNames = Get-PropertyNames -Columns $Columns -StartRow $StartRow))) {
throw "No column headers found on top row '$StartRow'. If column headers in the worksheet are not a requirement then please use the '-NoHeader' or '-HeaderName' parameter."; return
}
if ($Duplicates = $PropertyNames | Group-Object Value | Where-Object Count -GE 2) {
throw "Duplicate column headers found on row '$StartRow' in columns '$($Duplicates.Group.Column)'. Column headers must be unique, if this is not a requirement please use the '-NoHeader' or '-HeaderName' parameter."; return
}
#endregion
if (-not $rows) {
Write-Warning "Worksheet '$WorksheetName' in workbook '$Path' contains no data in the rows after top row '$StartRow'"
}
else {
#region Create one object per row
if ($AsText -or $AsDate) {
<#join items in AsText together with ~~~ . Escape any regex special characters...
# which turns "*" into "\*" make it ".*". Convert ~~~ to $|^ and top and tail with ^%;
So if we get "Week", "[Time]" and "*date*" ; make the expression ^week$|^\[Time\]$|^.*Date.*$
$make a regex for this which is case insensitive (option 1) and compiled (option 8)
#>
$TextColExpression = ''
if ($AsText) {
$TextColExpression += '(?<astext>^' + [regex]::Escape($AsText -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)'
}
if ($AsText -and $AsDate) {
$TextColExpression += "|"
}
if ($AsDate) {
$TextColExpression += '(?<asDate>^' + [regex]::Escape($AsDate -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)'
}
$TextColRegEx = New-Object -TypeName regex -ArgumentList $TextColExpression , 9
}
else { $TextColRegEx = $null }
foreach ($R in $rows) {
#Disabled write-verbose for speed
# Write-Verbose "Import row '$R'"
$NewRow = [Ordered]@{ }
if ($TextColRegEx) {
foreach ($P in $PropertyNames) {
$MatchTest = $TextColRegEx.Match($P.value)
if ($MatchTest.groups.name -eq "astext") {
$NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Text
}
elseif ($MatchTest.groups.name -eq "asdate" -and $Worksheet.Cells[$R, $P.Column].Value -is [System.ValueType]) {
$NewRow[$P.Value] = [datetime]::FromOADate(($Worksheet.Cells[$R, $P.Column].Value))
}
else { $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value }
$TextColExpression = ''
if ($AsText) {
$TextColExpression += '(?<astext>^' + [regex]::Escape($AsText -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)'
}
}
else {
foreach ($P in $PropertyNames) {
$NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value
# Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$($p.Value)' and value '$($Worksheet.Cells[$R, $P.Column].Value)'."
if ($AsText -and $AsDate) {
$TextColExpression += "|"
}
if ($AsDate) {
$TextColExpression += '(?<asDate>^' + [regex]::Escape($AsDate -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)'
}
$TextColRegEx = New-Object -TypeName regex -ArgumentList $TextColExpression , 9
}
[PSCustomObject]$NewRow
else { $TextColRegEx = $null }
foreach ($R in $rows) {
#Disabled write-verbose for speed
# Write-Verbose "Import row '$R'"
$NewRow = [Ordered]@{ }
if ($TextColRegEx) {
foreach ($P in $PropertyNames) {
$MatchTest = $TextColRegEx.Match($P.value)
if ($MatchTest.groups.name -eq "astext") {
$NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Text
}
elseif ($MatchTest.groups.name -eq "asdate" -and $sheet.Cells[$R, $P.Column].Value -is [System.ValueType]) {
$NewRow[$P.Value] = [datetime]::FromOADate(($sheet.Cells[$R, $P.Column].Value))
}
else { $NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Value }
}
}
else {
foreach ($P in $PropertyNames) {
$NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Value
# Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$($p.Value)' and value '$($Worksheet.Cells[$R, $P.Column].Value)'."
}
}
$xlBook["$targetSheetname"] += [PSCustomObject]$NewRow
}
#endregion
}
#endregion
}
}
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return }
finally {
# $EndRow = 0
# $EndColumn = 0
if ($Path) { $stream.close(); $ExcelPackage.Dispose() }
if ($Raw) {
foreach ($entry in $xlbook.GetEnumerator()) {
$entry.Value
}
}
elseif ($Worksheet.Count -eq 1) {
$xlBook["$targetSheetname"]
}
else {
$xlBook
}
}
}
}

1357
README.md

File diff suppressed because it is too large Load Diff

1248
README.original.md Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -63,6 +63,10 @@ Describe "Import-Excel on a sheet with no headings" {
}
}
AfterAll {
Remove-Item $PSScriptRoot\testImportExcelSparse.xlsx -ErrorAction SilentlyContinue
}
It "Import-Excel should have this shape" {
$actual = @(Import-Excel $xlfile)
@@ -230,6 +234,7 @@ Describe "Import-Excel on a sheet with no headings" {
$actual.Count | Should -Be 1
Remove-Item $xlfile
# Looks like -DataOnly does not handle empty columns
# $actual[0].FirstName | Should -BeExactly 'Jean-Claude'
# $actual[0].SecondName | Should -BeExactly 'Vandamme'

Binary file not shown.

View File

@@ -0,0 +1,51 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe 'Test' -Tag ImportExcelEndRowAndCols {
BeforeAll {
$script:xlFilename = "$PSScriptRoot\DataInDiffRowCol.xlsx"
}
Context 'Test reading a partial sheet' {
It 'Should read 2 rows and first 3 columns' {
$actual = Import-Excel $xlFilename -StartRow 5 -EndRow 7 -StartColumn 3 -EndColumn 5
# $actual | out-host
$actual.Count | Should -Be 2
$colNames = $actual[0].psobject.properties.Name
$colNames.Count | Should -Be 3
$colNames[0] | Should -Be 'Region'
$colNames[1] | Should -Be 'State'
$colNames[2] | Should -Be 'Units'
}
It 'Should read second 2 rows and last 2 columns' {
$actual = Import-Excel $xlFilename -StartRow 8 -EndRow 9 -StartColumn 5 -EndColumn 6 -HeaderName 'Units', 'Price'
# $actual | out-host
$actual.Count | Should -Be 2
$colNames = $actual[0].psobject.properties.Name
$colNames.Count | Should -Be 2
$colNames[0] | Should -Be 'Units'
$colNames[1] | Should -Be 'Price'
}
}
Context 'Test reading multiple sheets with data in differnt rows and columns' {
It 'Should read 2 sheets same StartRow different dimensions' {
$xlFilename = "$PSScriptRoot\DataInDiffRowColMultipleSheets.xlsx"
$actual = Import-Excel $xlFilename -StartRow 5 -WorksheetName *
$actual.Keys.Count | Should -Be 2
$actual.Contains('Sheet1') | Should -BeTrue
$actual.Contains('Sheet2') | Should -BeTrue
$actual['Sheet1'].Count | Should -Be 9
$actual['Sheet2'].Count | Should -Be 12
}
}
}

View File

@@ -0,0 +1,82 @@
#Requires -Modules Pester
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
param()
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets {
BeforeAll {
$xlFilename = "$PSScriptRoot\yearlySales.xlsx"
}
Context 'Test reading sheets' {
It 'Should read one sheet' {
$actual = Import-Excel $xlFilename
$actual.Count | Should -Be 100
$actual[0].Month | Should -BeExactly "April"
$actual[99].Month | Should -BeExactly "April"
}
It 'Should read two sheets' {
$actual = Import-Excel $xlFilename march, june
$actual.keys.Count | Should -Be 2
$actual["March"].Count | Should -Be 100
$actual["June"].Count | Should -Be 100
}
It 'Should read all the sheets' {
$actual = Import-Excel $xlFilename *
$actual.keys.Count | Should -Be 12
$actual["January"].Count | Should -Be 100
$actual["February"].Count | Should -Be 100
$actual["March"].Count | Should -Be 100
$actual["April"].Count | Should -Be 100
$actual["May"].Count | Should -Be 100
$actual["June"].Count | Should -Be 100
$actual["July"].Count | Should -Be 100
$actual["August"].Count | Should -Be 100
$actual["September"].Count | Should -Be 100
$actual["October"].Count | Should -Be 100
$actual["November"].Count | Should -Be 100
$actual["December"].Count | Should -Be 100
}
It 'Should throw if it cannot find the sheet' {
{ Import-Excel $xlFilename april, june, notthere } | Should -Throw
}
It 'Should return an array not a dictionary' {
$actual = Import-Excel $xlFilename april, june -Raw
$actual.Count | Should -Be 200
$group = $actual | Group-Object month -NoElement
$group.Count | Should -Be 2
$group[0].Name | Should -BeExactly 'April'
$group[1].Name | Should -BeExactly 'June'
}
It "Should read multiple sheets with diff number of rows correctly" {
$xlFilename = "$PSScriptRoot\construction.xlsx"
$actual = Import-Excel $xlFilename 2015, 2016
$actual.keys.Count | Should -Be 2
$actual["2015"].Count | Should -Be 12
$actual["2016"].Count | Should -Be 1
}
It "Should read multiple sheets with diff number of rows correctly and flatten it" {
$xlFilename = "$PSScriptRoot\construction.xlsx"
$actual = Import-Excel $xlFilename 2015, 2016 -Raw
$actual.Count | Should -Be 13
}
}
}

View File

@@ -0,0 +1,57 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
Param()
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe "Test reading multiple XLSX files of different row count" -Tag ReadMultipleXLSX {
It "Should find these xlsx files" {
Test-Path -Path $PSScriptRoot\rows05.xlsx | Should -BeTrue
Test-Path -Path $PSScriptRoot\rows10.xlsx | Should -BeTrue
}
It "Should find two xlsx files" {
(Get-ChildItem $PSScriptRoot\row*xlsx).Count | Should -Be 2
}
It "Should get 5 rows" {
(Import-Excel $PSScriptRoot\rows05.xlsx).Count | Should -Be 5
}
It "Should get 10 rows" {
(Import-Excel $PSScriptRoot\rows10.xlsx).Count | Should -Be 10
}
It "Should get 15 rows" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
$actual.Count | Should -Be 15
}
It "Should get 4 property names" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
$names = $actual[0].psobject.properties.name
$names.Count | Should -Be 4
$names[0] | Should -BeExactly "Region"
$names[1] | Should -BeExactly "State"
$names[2] | Should -BeExactly "Units"
$names[3] | Should -BeExactly "Price"
}
It "Should have the correct data" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
# rows05.xlsx
$actual[0].Region | Should -BeExactly "South"
$actual[0].Price | Should -Be 181.52
$actual[4].Region | Should -BeExactly "West"
$actual[4].Price | Should -Be 216.56
# rows10.xlsx
$actual[5].Region | Should -BeExactly "South"
$actual[5].Price | Should -Be 199.85
$actual[14].Region | Should -BeExactly "East"
$actual[14].Price | Should -Be 965.25
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,22 +1,22 @@
#Requires -Modules Pester
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Justification='False Positives')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases','',Justification='Testing for presence of alias')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases', '', Justification = 'Testing for presence of alias')]
param()
describe "Consistent passing of ranges." {
BeforeAll {
$path = "TestDrive:\test.xlsx"
if (-not (Get-command Get-Service -ErrorAction SilentlyContinue)) {
Function Get-Service {Import-Clixml $PSScriptRoot\Mockservices.xml}
}
$path = "TestDrive:\test.xlsx"
# if (-not (Get-command Get-Service -ErrorAction SilentlyContinue)) {
Function Get-Service { Import-Clixml $PSScriptRoot\Mockservices.xml }
# }
}
Context "Conditional Formatting" {
Context "Conditional Formatting" {
it "accepts named ranges, cells['name'], worksheet + Name, worksheet + column " {
Remove-Item -path $path -ErrorAction SilentlyContinue
$excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -AutoNameRange -Title "Services on $Env:COMPUTERNAME"
{Add-ConditionalFormatting $excel.Services.Names["Status"] -StrikeThru -RuleType ContainsText -ConditionValue "Stopped" } | Should -Not -Throw
{ Add-ConditionalFormatting $excel.Services.Names["Status"] -StrikeThru -RuleType ContainsText -ConditionValue "Stopped" } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 1
{Add-ConditionalFormatting $excel.Services.Cells["Name"] -Italic -RuleType ContainsText -ConditionValue "SVC" } | Should -Not -Throw
{ Add-ConditionalFormatting $excel.Services.Cells["Name"] -Italic -RuleType ContainsText -ConditionValue "SVC" } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 2
$warnvar = $null
Add-ConditionalFormatting $excel.Services.Column(3) `
@@ -25,25 +25,25 @@ describe "Consistent passing of ranges." {
$excel.Services.ConditionalFormatting.Count | Should -Be 2
$warnvar = $null
Add-ConditionalFormatting $excel.Services.Column(3) -Worksheet $excel.Services`
-underline -RuleType ContainsText -ConditionValue "Windows" -WarningVariable warnvar -WarningAction SilentlyContinue
-underline -RuleType ContainsText -ConditionValue "Windows" -WarningVariable warnvar -WarningAction SilentlyContinue
$warnvar | Should -BeNullOrEmpty
$excel.Services.ConditionalFormatting.Count | Should -Be 3
{Add-ConditionalFormatting "Status" -Worksheet $excel.Services `
-ForeGroundColor ([System.Drawing.Color]::Green) -RuleType ContainsText -ConditionValue "Running"} | Should -Not -Throw
{ Add-ConditionalFormatting "Status" -Worksheet $excel.Services `
-ForeGroundColor ([System.Drawing.Color]::Green) -RuleType ContainsText -ConditionValue "Running" } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 4
Close-ExcelPackage -NoSave $excel
}
it "accepts table, table.Address and worksheet + 'C:C' " {
$excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME"
{Add-ConditionalFormatting $excel.Services.Tables[0] `
-Italic -RuleType ContainsText -ConditionValue "Svc" } | Should -Not -Throw
$excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME"
{ Add-ConditionalFormatting $excel.Services.Tables[0] `
-Italic -RuleType ContainsText -ConditionValue "Svc" } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 1
{Add-ConditionalFormatting $excel.Services.Tables["ServiceTable"].Address `
-Bold -RuleType ContainsText -ConditionValue "windows" } | Should -Not -Throw
{ Add-ConditionalFormatting $excel.Services.Tables["ServiceTable"].Address `
-Bold -RuleType ContainsText -ConditionValue "windows" } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 2
{Add-ConditionalFormatting -Worksheet $excel.Services -Address "a:a" `
-RuleType ContainsText -ConditionValue "stopped" -ForeGroundColor ([System.Drawing.Color]::Red) } | Should -Not -Throw
{ Add-ConditionalFormatting -Worksheet $excel.Services -Address "a:a" `
-RuleType ContainsText -ConditionValue "stopped" -ForeGroundColor ([System.Drawing.Color]::Red) } | Should -Not -Throw
$excel.Services.ConditionalFormatting.Count | Should -Be 3
Close-ExcelPackage -NoSave $excel
}
@@ -52,29 +52,29 @@ describe "Consistent passing of ranges." {
Context "Formating (Set-ExcelRange or its alias Set-Format) " {
it "accepts Named Range, cells['Name'], cells['A1:Z9'], row, Worksheet + 'A1:Z9'" {
$excel = Get-Service | Export-Excel -Path test2.xlsx -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -RangeName servicerange -Title "Services on $Env:COMPUTERNAME"
{Set-format $excel.Services.Names["serviceRange"] -Bold } | Should -Not -Throw
{ Set-format $excel.Services.Names["serviceRange"] -Bold } | Should -Not -Throw
$excel.Services.cells["B2"].Style.Font.Bold | Should -Be $true
{Set-ExcelRange -Range $excel.Services.Cells["serviceRange"] -italic:$true } | Should -Not -Throw
{ Set-ExcelRange -Range $excel.Services.Cells["serviceRange"] -italic:$true } | Should -Not -Throw
$excel.Services.cells["C3"].Style.Font.Italic | Should -Be $true
{Set-format $excel.Services.Row(4) -underline -Bold:$false } | Should -Not -Throw
{ Set-format $excel.Services.Row(4) -underline -Bold:$false } | Should -Not -Throw
$excel.Services.cells["A4"].Style.Font.UnderLine | Should -Be $true
$excel.Services.cells["A4"].Style.Font.Bold | Should -Not -Be $true
{Set-ExcelRange $excel.Services.Cells["A3:B3"] -StrikeThru } | Should -Not -Throw
{ Set-ExcelRange $excel.Services.Cells["A3:B3"] -StrikeThru } | Should -Not -Throw
$excel.Services.cells["B3"].Style.Font.Strike | Should -Be $true
{Set-ExcelRange -Worksheet $excel.Services -Range "A5:B6" -FontSize 8 } | Should -Not -Throw
{ Set-ExcelRange -Worksheet $excel.Services -Range "A5:B6" -FontSize 8 } | Should -Not -Throw
$excel.Services.cells["A5"].Style.Font.Size | Should -Be 8
Close-ExcelPackage -NoSave $excel
}
it "Accepts Table, Table.Address , worksheet + Name, Column," {
$excel = Get-Service | Export-Excel -Path test2.xlsx -WorksheetName Services -PassThru -AutoNameRange -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME"
{Set-ExcelRange $excel.Services.Tables[0] -Italic } | Should -Not -Throw
{ Set-ExcelRange $excel.Services.Tables[0] -Italic } | Should -Not -Throw
$excel.Services.cells["C3"].Style.Font.Italic | Should -Be $true
{Set-format $excel.Services.Tables["ServiceTable"].Address -Underline } | Should -Not -Throw
{ Set-format $excel.Services.Tables["ServiceTable"].Address -Underline } | Should -Not -Throw
$excel.Services.cells["C3"].Style.Font.UnderLine | Should -Be $true
{Set-ExcelRange -Worksheet $excel.Services -Range "Name" -Bold } | Should -Not -Throw
{ Set-ExcelRange -Worksheet $excel.Services -Range "Name" -Bold } | Should -Not -Throw
$excel.Services.cells["B4"].Style.Font.Bold | Should -Be $true
{$excel.Services.Column(3) | Set-ExcelRange -FontColor ([System.Drawing.Color]::Red) } | Should -Not -Throw
{ $excel.Services.Column(3) | Set-ExcelRange -FontColor ([System.Drawing.Color]::Red) } | Should -Not -Throw
$excel.Services.cells["C4"].Style.Font.Color.Rgb | Should -Be "FFFF0000"
Close-ExcelPackage -NoSave $excel
}
@@ -82,41 +82,41 @@ describe "Consistent passing of ranges." {
}
Context "PivotTables" {
it "Accepts Named range, .Cells['Name'], name&Worksheet, cells['A1:Z9'], worksheet&'A1:Z9' "{
it "Accepts Named range, .Cells['Name'], name&Worksheet, cells['A1:Z9'], worksheet&'A1:Z9' " {
$excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -RangeName servicerange -Title "Services on $Env:COMPUTERNAME"
$ws = $excel.Workbook.Worksheets[1] #can get a worksheet by name or index - starting at 1
$end = $ws.Dimension.End.Address
$ws = $excel.Workbook.Worksheets[1] #can get a worksheet by name or index - starting at 1
$end = $ws.Dimension.End.Address
#can get a named ranged by name or index - starting at zero
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt0 -SourceRange $ws.Names[0]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt0 -SourceRange $ws.Names[0]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt0"] | Should -Not -BeNullOrEmpty
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.Names["servicerange"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.Names["servicerange"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt1"] | Should -Not -BeNullOrEmpty
#Can specify the range for a pivot as NamedRange or Table or TableAddress or Worksheet + "A1:Z10" or worksheet + RangeName, or worksheet.cells["A1:Z10"] or worksheet.cells["RangeName"]
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange "servicerange" -SourceWorkSheet $ws `
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange "servicerange" -SourceWorkSheet $ws `
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt2"] | Should -Not -BeNullOrEmpty
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt3 -SourceRange $ws.cells["servicerange"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt3 -SourceRange $ws.cells["servicerange"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt3"] | Should -Not -BeNullOrEmpty
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt4 -SourceRange $ws.cells["A2:$end"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt4 -SourceRange $ws.cells["A2:$end"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt4"] | Should -Not -BeNullOrEmpty
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt5 -SourceRange "A2:$end" -SourceWorkSheet $ws `
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt5 -SourceRange "A2:$end" -SourceWorkSheet $ws `
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt5"] | Should -Not -BeNullOrEmpty
Close-ExcelPackage -NoSave $excel
Close-ExcelPackage -NoSave $excel
}
it "Accepts Table, Table.Addres " {
$excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME"
$ws = $excel.Workbook.Worksheets["Services"] #can get a worksheet by name or index - starting at 1
$ws = $excel.Workbook.Worksheets["Services"] #can get a worksheet by name or index - starting at 1
#Can get a table by name or -stating at zero. Can specify the range for a pivot as or Table or TableAddress
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.tables["servicetable"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.tables["servicetable"]`
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt1"] | Should -Not -BeNullOrEmpty
{Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange $ws.tables[0].Address `
-PivotRows Status -PivotData Name } | Should -Not -Throw
{ Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange $ws.tables[0].Address `
-PivotRows Status -PivotData Name } | Should -Not -Throw
$excel.Workbook.Worksheets["pt2"] | Should -Not -BeNullOrEmpty
Close-ExcelPackage -NoSave $excel
}

Binary file not shown.

View File

@@ -1,3 +1,34 @@
# 7.6.0
- Fix -StartRow and -StartColumn being ignored.
# v7.5.2
- Changed the switch `-NotAsDictionary` to `-Raw`. Works with `-Worksheetname *` reads all the sheets in the xlsx file and returns an array.
# v7.5.1
- Fixed `Import-Excel` - Reset `EndRow` and `EndColumn` in the correct place.
# v7.5.0
## Fixes
- Importing multiple files with Import-Excel by pipeline uses only the first file for the row count https://github.com/dfinke/ImportExcel/issues/1172
## New Features
- Import-Excel now supports importing multiple sheets. It can either return a dictionary of all sheets, or as a single array of all sheets combined.
- `Import-Excel $xlfile *` # reads all sheets, returns all data in a dictionary
- `Import-Excel $xlfile * -NotAsDictionary` # reads all sheets, returns all data in a single array
- Added helper functions. Useful for working with an Excel package via `Open-ExcelPackage` or `-PassThru`
- `Enable-ExcelAutoFilter`
- `Enable-ExcelAutofit`
- `Get-ExcelSheetDimensionAddress`
# v7.4.2
- Thank you [James Mueller](https://github.com/jamesmmueller) Updated `ConvertFrom-ExcelToSQLInsert` to handle single quotes in the SQL statement.
- Thank you to Josh Hendricks
- Add images to spreadsheets. [Check it out](https://github.com/dfinke/ImportExcel/tree/master/Examples/AddImage)
- Catch up with him on [GitHub](https://github.com/joshooaj) and [Twitter](https://twitter.com/joshooaj) for the idea
# v7.4.1
- Implements: https://github.com/dfinke/ImportExcel/issues/1111

View File

@@ -1,2 +0,0 @@
# Examples

View File

@@ -1,2 +0,0 @@
# Charts

View File

@@ -1,28 +0,0 @@
# Multiplecharts
## PowerShell
```text
$xlFile = "$env:TEMP\ImportExcelExample.xlsx"
Remove-Item $xlFile -ErrorAction Ignore
$data = ConvertFrom-Csv @"
ID,Product,Quantity,Price,Total
12001,Nails,37,3.99,147.63
12002,Hammer,5,12.10,60.5
12003,Saw,12,15.37,184.44
12010,Drill,20,8,160
12011,Crowbar,7,23.48,164.36
"@
$chart1 = New-ExcelChartDefinition -YRange "Price" -XRange "Product" -Title "Item price" -NoLegend -Height 225
$chart2 = New-ExcelChartDefinition -YRange "Total "-XRange "Product" -Title "Total sales" -NoLegend -Height 225 -Row 9 -Column 15
$chart3 = New-ExcelChartDefinition -YRange "Quantity"-XRange "Product" -Title "Sales volume" -NoLegend -Height 225 -Row 15
$data | Export-Excel -Path $xlFile -AutoFilter -AutoNameRange -AutoSize -Show -ExcelChartDefinition $chart1,$chart2,$chart3
```
## Result
![](https://raw.githubusercontent.com/dfinke/ImportExcel/master/Examples/Charts/Multiplecharts.png)

View File

@@ -1,2 +0,0 @@
# Untitled

BIN
images/SalesData.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
images/SalesDataChart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

BIN
images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
images/logoWithInstall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB