mirror of
https://github.com/dfinke/ImportExcel.git
synced 2026-01-06 02:24:04 +00:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91b0e8b0ed | ||
|
|
b997e90e78 | ||
|
|
4d8710d017 | ||
|
|
0929a442a5 | ||
|
|
895a5c0cb2 | ||
|
|
d7901af8f3 | ||
|
|
34f55a3659 | ||
|
|
606988bcf6 | ||
|
|
441f2ada22 | ||
|
|
29ea7012d7 | ||
|
|
67ac63ddcf | ||
|
|
c939c6ecb0 | ||
|
|
96493e059b | ||
|
|
cb7f2a06f4 | ||
|
|
99beda7a10 | ||
|
|
0c8bb2300a | ||
|
|
c09083d350 | ||
|
|
556f0ac51e | ||
|
|
1e4fc59a25 | ||
|
|
5ab6a9116d | ||
|
|
f33215382a | ||
|
|
cf5d3f83d6 | ||
|
|
91da711635 | ||
|
|
eec5e343d4 | ||
|
|
d69b640edc | ||
|
|
2ca870a889 | ||
|
|
a320cfd28c | ||
|
|
aa1b042767 | ||
|
|
7e8416d67c | ||
|
|
229b60b25d | ||
|
|
5700989321 | ||
|
|
56e1704e7e | ||
|
|
8268bbc2e1 | ||
|
|
5657659331 | ||
|
|
0d4a32e266 | ||
|
|
1c8f8d2a3d | ||
|
|
f6a65677df | ||
|
|
86a7865fb2 | ||
|
|
8b3bf4f14f | ||
|
|
4d6193f549 | ||
|
|
6ebac7b6dc | ||
|
|
d71dd36d56 | ||
|
|
3697cdfeee | ||
|
|
1e172cf21f | ||
|
|
6da7553c98 | ||
|
|
5a444c620b | ||
|
|
4a09fc3570 | ||
|
|
d706a10276 | ||
|
|
1fd2f422cd | ||
|
|
283e50547d | ||
|
|
15211a6297 | ||
|
|
8905b8d401 | ||
|
|
e9b437af4e | ||
|
|
330e237727 | ||
|
|
c56b2cd33a | ||
|
|
1aa5c6da45 | ||
|
|
9eb894cf59 |
119
Examples/AddImage/Add-ExcelImage.ps1
Normal file
119
Examples/AddImage/Add-ExcelImage.ps1
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
Examples/AddImage/AddImage.ps1
Normal file
39
Examples/AddImage/AddImage.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Examples/AddImage/Octocat.jpg
Normal file
BIN
Examples/AddImage/Octocat.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
33
Examples/AddImage/README.md
Normal file
33
Examples/AddImage/README.md
Normal 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.
|
||||||
54
Examples/Freeze/FreezePane.ps1
Normal file
54
Examples/Freeze/FreezePane.ps1
Normal 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
|
||||||
64
Examples/VBA/AddModuleMultipleWorksheetVBA.ps1
Normal file
64
Examples/VBA/AddModuleMultipleWorksheetVBA.ps1
Normal 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
|
||||||
41
Examples/VBA/AddWorksheetVBA.ps1
Normal file
41
Examples/VBA/AddWorksheetVBA.ps1
Normal 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
|
||||||
6
FAQ/How to Create an Empty Excel File.md
Normal file
6
FAQ/How to Create an Empty Excel File.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Create an Empty Excel File
|
||||||
|
Use an empty string to export to an excel file.
|
||||||
|
```powershell
|
||||||
|
#Build an Excel file named: "file.xlsx" containing a worksheet: "MyWorksheet"
|
||||||
|
"" | Export-Excel -Path "C:\Test\file.xlsx" -WorksheetName "MyWorksheet"
|
||||||
|
```
|
||||||
41
FAQ/How to Read an Existing Excel File.md
Normal file
41
FAQ/How to Read an Existing Excel File.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# How to Read an Existing Excel File
|
||||||
|
## Enumerate the Excel File Contents
|
||||||
|
```powershell
|
||||||
|
#Load the Excel file into a PSCustomObject
|
||||||
|
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Visual of Data Structure
|
||||||
|
The File C:\Test\file.xlsx contains
|
||||||
|

|
||||||
|
|
||||||
|
After loading this data into ```$ExcelFile``` the data is stored like:
|
||||||
|

|
||||||
|
|
||||||
|
## Other Common Operations
|
||||||
|
|
||||||
|
### Load a Column
|
||||||
|
```powershell
|
||||||
|
$SpecificColumn = $ExcelFile."anotherHeader" #loads column with the header "anotherHeader" -- data stored in an array
|
||||||
|
```
|
||||||
|
|
||||||
|
### Load a Row
|
||||||
|
```powershell
|
||||||
|
$SpecificRow = $ExcelFile[1] #Loads row at index 1. Index 1 is the first row instead of 0.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Map Contents to Hashtable to Interpret Data
|
||||||
|
Sometimes mapping to a Hashtable is more convenient to have access to common Hashtable operations. Enumerate a Hashtable with the row's data by:
|
||||||
|
```powershell
|
||||||
|
$HashTable = @{}
|
||||||
|
$SpecificRow= $ExcelFile[2]
|
||||||
|
$SpecificRow.psobject.properties | ForEach-Object {
|
||||||
|
$HashTable[$_.Name] = $_.Value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
To then iterate through the enumerated Hashtable:
|
||||||
|
```powershell
|
||||||
|
ForEach ($Key in ($HashTable.GetEnumerator()) | Where-Object {$_.Value -eq "x"}){ #Only grabs a key where the value is "x"
|
||||||
|
#values accessible with $Key.Name or $Key.Value
|
||||||
|
}
|
||||||
|
```
|
||||||
34
FAQ/How to Write to an Existing Excel File.md
Normal file
34
FAQ/How to Write to an Existing Excel File.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Write to an Existing Excel File
|
||||||
|
### Enumerate the Excel File
|
||||||
|
The cmdlets ```Open-ExcelPackage``` and ```Close-ExcelPackage``` allow for direct modification to Excel file contents.
|
||||||
|
```powershell
|
||||||
|
$ExcelPkg = Open-ExcelPackage -Path "C:\Test\file.xlsx"
|
||||||
|
```
|
||||||
|
Contents of file.xlsx:
|
||||||
|

|
||||||
|
### Enumerate the Worksheet to View or Modify the Data
|
||||||
|
```powershell
|
||||||
|
$WorkSheet = $ExcelPkg.Workbook.Worksheets["sheet1"].Cells #open excel worksheet cells from worksheet "sheet1"
|
||||||
|
```
|
||||||
|
Visual of data structure:
|
||||||
|

|
||||||
|
A1 contains "someHeader", A2 contains "data1" etc.
|
||||||
|
### Modify a Specific Value in a File
|
||||||
|
Values can be accessed by row, column. Similar to a 2D array.
|
||||||
|
```powershell
|
||||||
|
$WorkSheet[1,4].Value = "New Column Header" #Starts at index 1 not 0
|
||||||
|
```
|
||||||
|
Contents of file.xlsx after modifying:
|
||||||
|

|
||||||
|
### Load Value at Specific Index
|
||||||
|
```powershell
|
||||||
|
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A
|
||||||
|
```
|
||||||
|
```$ValueAtIndex``` now contains: 
|
||||||
|
### Save File After Modifying
|
||||||
|
The changes will not display in the Excel file until Close-ExcelPackage is called.
|
||||||
|
```powershell
|
||||||
|
Close-ExcelPackage $ExcelPkg #close and save changes made to the Excel file.
|
||||||
|
```
|
||||||
|
**Note**: If the file is currently in use, Close-ExcelPackage will return an error and will not save the information.
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
RootModule = 'ImportExcel.psm1'
|
RootModule = 'ImportExcel.psm1'
|
||||||
|
|
||||||
# Version number of this module.
|
# Version number of this module.
|
||||||
ModuleVersion = '7.4.0'
|
ModuleVersion = '7.4.2'
|
||||||
|
|
||||||
# ID used to uniquely identify this module
|
# ID used to uniquely identify this module
|
||||||
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
||||||
|
|||||||
25
Private/Invoke-ExcelReZipFile.ps1
Normal file
25
Private/Invoke-ExcelReZipFile.ps1
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
function Invoke-ExcelReZipFile {
|
||||||
|
<#
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[OfficeOpenXml.ExcelPackage]$ExcelPackage
|
||||||
|
)
|
||||||
|
|
||||||
|
Write-Verbose -Message "Re-Zipping $($ExcelPackage.file) using .NET ZIP library"
|
||||||
|
try {
|
||||||
|
Add-Type -AssemblyName 'System.IO.Compression.Filesystem' -ErrorAction stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$TempZipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName())
|
||||||
|
$null = [io.compression.zipfile]::ExtractToDirectory($ExcelPackage.File, $TempZipPath)
|
||||||
|
Remove-Item $ExcelPackage.File -Force
|
||||||
|
$null = [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $ExcelPackage.File)
|
||||||
|
Remove-Item $TempZipPath -Recurse -Force
|
||||||
|
}
|
||||||
|
catch { throw "Error resizipping $path : $_" }
|
||||||
|
}
|
||||||
@@ -4,13 +4,15 @@ function Close-ExcelPackage {
|
|||||||
param (
|
param (
|
||||||
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
|
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
|
||||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||||
[switch]$Show,
|
[Switch]$Show,
|
||||||
[Switch]$NoSave,
|
[Switch]$NoSave,
|
||||||
$SaveAs,
|
$SaveAs,
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[String]$Password,
|
[String]$Password,
|
||||||
[switch]$Calculate
|
[Switch]$Calculate,
|
||||||
|
[Switch]$ReZip
|
||||||
)
|
)
|
||||||
|
|
||||||
if ( $NoSave) { $ExcelPackage.Dispose() }
|
if ( $NoSave) { $ExcelPackage.Dispose() }
|
||||||
else {
|
else {
|
||||||
if ($Calculate) {
|
if ($Calculate) {
|
||||||
@@ -27,6 +29,9 @@ function Close-ExcelPackage {
|
|||||||
else { $ExcelPackage.Save() }
|
else { $ExcelPackage.Save() }
|
||||||
$SaveAs = $ExcelPackage.File.FullName
|
$SaveAs = $ExcelPackage.File.FullName
|
||||||
}
|
}
|
||||||
|
if ($ReZip) {
|
||||||
|
Invoke-ExcelReZipFile -ExcelPackage $ExcelPackage
|
||||||
|
}
|
||||||
$ExcelPackage.Dispose()
|
$ExcelPackage.Dispose()
|
||||||
if ($Show) { Start-Process -FilePath $SaveAs }
|
if ($Show) { Start-Process -FilePath $SaveAs }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,12 +16,15 @@ function ConvertFrom-ExcelToSQLInsert {
|
|||||||
[switch]$NoHeader,
|
[switch]$NoHeader,
|
||||||
[switch]$DataOnly,
|
[switch]$DataOnly,
|
||||||
[switch]$ConvertEmptyStringsToNull,
|
[switch]$ConvertEmptyStringsToNull,
|
||||||
[switch]$UseMsSqlSyntax
|
[switch]$UseMsSqlSyntax,
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
$SingleQuoteStyle
|
||||||
)
|
)
|
||||||
|
|
||||||
$null = $PSBoundParameters.Remove('TableName')
|
$null = $PSBoundParameters.Remove('TableName')
|
||||||
$null = $PSBoundParameters.Remove('ConvertEmptyStringsToNull')
|
$null = $PSBoundParameters.Remove('ConvertEmptyStringsToNull')
|
||||||
$null = $PSBoundParameters.Remove('UseMsSqlSyntax')
|
$null = $PSBoundParameters.Remove('UseMsSqlSyntax')
|
||||||
|
$null = $PSBoundParameters.Remove('SingleQuoteStyle')
|
||||||
|
|
||||||
$params = @{} + $PSBoundParameters
|
$params = @{} + $PSBoundParameters
|
||||||
|
|
||||||
@@ -37,10 +40,15 @@ function ConvertFrom-ExcelToSQLInsert {
|
|||||||
if ($ConvertEmptyStringsToNull.IsPresent -and [string]::IsNullOrEmpty($record.$propertyName)) {
|
if ($ConvertEmptyStringsToNull.IsPresent -and [string]::IsNullOrEmpty($record.$propertyName)) {
|
||||||
'NULL'
|
'NULL'
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if ( $SingleQuoteStyle ) {
|
||||||
|
"'" + $record.$propertyName.ToString().Replace("'",${SingleQuoteStyle}) + "'"
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
"'" + $record.$propertyName + "'"
|
"'" + $record.$propertyName + "'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$targetValues = ($values -join ", ")
|
$targetValues = ($values -join ", ")
|
||||||
|
|
||||||
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
|
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
|
||||||
|
|||||||
@@ -253,7 +253,8 @@
|
|||||||
else { $firstTimeThru = $true }
|
else { $firstTimeThru = $true }
|
||||||
}
|
}
|
||||||
|
|
||||||
process { if ($PSBoundParameters.ContainsKey("InputObject")) {
|
process {
|
||||||
|
if ($PSBoundParameters.ContainsKey("InputObject")) {
|
||||||
try {
|
try {
|
||||||
if ($null -eq $InputObject) { $row += 1 }
|
if ($null -eq $InputObject) { $row += 1 }
|
||||||
foreach ($TargetData in $InputObject) {
|
foreach ($TargetData in $InputObject) {
|
||||||
@@ -321,7 +322,8 @@
|
|||||||
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
|
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
|
||||||
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
|
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
|
||||||
}
|
}
|
||||||
elseif ($v -isnot [String] ) { #Other objects or null.
|
elseif ($v -isnot [String] ) {
|
||||||
|
#Other objects or null.
|
||||||
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
|
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
|
||||||
}
|
}
|
||||||
elseif ($v[0] -eq '=') {
|
elseif ($v[0] -eq '=') {
|
||||||
@@ -362,7 +364,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
|
catch { throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
end {
|
end {
|
||||||
if ($firstTimeThru -and $ws.Dimension) {
|
if ($firstTimeThru -and $ws.Dimension) {
|
||||||
@@ -386,7 +389,8 @@
|
|||||||
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
|
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
|
||||||
#using a slightly odd syntax otherwise header ends up as a 2D array
|
#using a slightly odd syntax otherwise header ends up as a 2D array
|
||||||
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
|
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
|
||||||
if ($PSBoundParameters.ContainsKey($TargetData)) { #if Export was called with data that writes no header start the range at $startRow ($startRow is data)
|
if ($PSBoundParameters.ContainsKey($TargetData)) {
|
||||||
|
#if Export was called with data that writes no header start the range at $startRow ($startRow is data)
|
||||||
$targetRow = $StartRow
|
$targetRow = $StartRow
|
||||||
}
|
}
|
||||||
else { $targetRow = $StartRow + 1 } #if Export was called without data to add names (assume $startRow is a header) or...
|
else { $targetRow = $StartRow + 1 } #if Export was called without data to add names (assume $startRow is a header) or...
|
||||||
@@ -400,7 +404,8 @@
|
|||||||
foreach ($c in 0..($LastCol - $StartColumn)) {
|
foreach ($c in 0..($LastCol - $StartColumn)) {
|
||||||
$targetRangeName = @($script:Header)[$c] #Let Add-ExcelName fix (and warn about) bad names
|
$targetRangeName = @($script:Header)[$c] #Let Add-ExcelName fix (and warn about) bad names
|
||||||
Add-ExcelName -RangeName $targetRangeName -Range $ws.Cells[$targetRow, ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
|
Add-ExcelName -RangeName $targetRangeName -Range $ws.Cells[$targetRow, ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
|
||||||
try {#this test can throw with some names, surpress any error
|
try {
|
||||||
|
#this test can throw with some names, surpress any error
|
||||||
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress(($targetRangeName -replace '\W' , '_' ))) {
|
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress(($targetRangeName -replace '\W' , '_' ))) {
|
||||||
Write-Warning -Message "AutoNameRange: Property name '$targetRangeName' is also a valid Excel address and may cause issues. Consider renaming the property."
|
Write-Warning -Message "AutoNameRange: Property name '$targetRangeName' is also a valid Excel address and may cause issues. Consider renaming the property."
|
||||||
}
|
}
|
||||||
@@ -515,7 +520,8 @@
|
|||||||
}
|
}
|
||||||
catch { Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_" }
|
catch { Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_" }
|
||||||
|
|
||||||
if ($PSBoundParameters.ContainsKey("BoldTopRow")) { #it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
if ($PSBoundParameters.ContainsKey("BoldTopRow")) {
|
||||||
|
#it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
||||||
try {
|
try {
|
||||||
if ($Title) {
|
if ($Title) {
|
||||||
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
|
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
|
||||||
@@ -621,7 +627,8 @@
|
|||||||
if ($c.ConditionalType) {
|
if ($c.ConditionalType) {
|
||||||
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
|
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
|
||||||
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
|
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
|
||||||
ForeGroundColor = $c.ConditionalTextColor}
|
ForeGroundColor = $c.ConditionalTextColor
|
||||||
|
}
|
||||||
if ($c.Range) { $cfParams.Range = $c.Range }
|
if ($c.Range) { $cfParams.Range = $c.Range }
|
||||||
else { $cfParams.Range = $ws.Dimension.Address }
|
else { $cfParams.Range = $ws.Dimension.Address }
|
||||||
Add-ConditionalFormatting -Worksheet $ws @cfParams
|
Add-ConditionalFormatting -Worksheet $ws @cfParams
|
||||||
@@ -672,21 +679,7 @@
|
|||||||
else { $pkg.Save() }
|
else { $pkg.Save() }
|
||||||
Write-Verbose -Message "Saved workbook $($pkg.File)"
|
Write-Verbose -Message "Saved workbook $($pkg.File)"
|
||||||
if ($ReZip) {
|
if ($ReZip) {
|
||||||
Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library"
|
Invoke-ExcelReZipFile -ExcelPackage $pkg
|
||||||
try {
|
|
||||||
Add-Type -AssemblyName 'System.IO.Compression.Filesystem' -ErrorAction stop
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+"
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$TempZipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName())
|
|
||||||
$null = [io.compression.zipfile]::ExtractToDirectory($pkg.File, $TempZipPath)
|
|
||||||
Remove-Item $pkg.File -Force
|
|
||||||
$null = [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $pkg.File)
|
|
||||||
}
|
|
||||||
catch {throw "Error resizipping $path : $_"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$pkg.Dispose()
|
$pkg.Dispose()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# PowerShell + Excel = Better Together
|
# PowerShell + Excel = Better Together
|
||||||
|
|
||||||
|
|
||||||
Automate Excel via PowerShell without having Excel installed. Runs on Windows, Linux and MAC. Creating Tables, Pivot Tables, Charts and much more has just become a lot easier.
|
Automate Excel via PowerShell without having Excel installed. Runs on Windows, Linux and MAC. Creating Tables, Pivot Tables, Charts and much more has just become a lot easier.
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
@@ -8,12 +9,6 @@ Automate Excel via PowerShell without having Excel installed. Runs on Windows, L
|
|||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
Open `ImportExcel` as a remote repo in VS Code, without cloning it.
|
|
||||||
|
|
||||||
[](https://open.vscode.dev/dfinke/importexcel)
|
|
||||||
|
|
||||||
<br/>
|
|
||||||
|
|
||||||
| CI System | Environment | Status |
|
| CI System | Environment | Status |
|
||||||
| :--- | :--- | :--- |
|
| :--- | :--- | :--- |
|
||||||
| Azure DevOps | Windows | [](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=21&branchName=master) |
|
| Azure DevOps | Windows | [](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=21&branchName=master) |
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ param()
|
|||||||
describe "Consistent passing of ranges." {
|
describe "Consistent passing of ranges." {
|
||||||
BeforeAll {
|
BeforeAll {
|
||||||
$path = "TestDrive:\test.xlsx"
|
$path = "TestDrive:\test.xlsx"
|
||||||
if (-not (Get-command Get-Service -ErrorAction SilentlyContinue)) {
|
# if (-not (Get-command Get-Service -ErrorAction SilentlyContinue)) {
|
||||||
Function Get-Service { Import-Clixml $PSScriptRoot\Mockservices.xml }
|
Function Get-Service { Import-Clixml $PSScriptRoot\Mockservices.xml }
|
||||||
}
|
# }
|
||||||
}
|
}
|
||||||
Context "Conditional Formatting" {
|
Context "Conditional Formatting" {
|
||||||
it "accepts named ranges, cells['name'], worksheet + Name, worksheet + column " {
|
it "accepts named ranges, cells['name'], worksheet + Name, worksheet + column " {
|
||||||
|
|||||||
15
changelog.md
15
changelog.md
@@ -1,3 +1,18 @@
|
|||||||
|
# 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
|
||||||
|
- Refactored ReZip into separate function
|
||||||
|
- Deletes temp folder after rezipping
|
||||||
|
- Added -ReZip to `Close-ExcelPackage`
|
||||||
|
|
||||||
# v7.4.0
|
# v7.4.0
|
||||||
|
|
||||||
- Thank you to [Max Goczall](https://github.com/muschebubusche) for this contribution!
|
- Thank you to [Max Goczall](https://github.com/muschebubusche) for this contribution!
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
# Examples
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# Charts
|
|
||||||
|
|
||||||
@@ -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
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# Untitled
|
|
||||||
|
|
||||||
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Reference in New Issue
Block a user