Set a title, the pattern, size and make it bold

This commit is contained in:
Doug Finke
2015-04-13 20:15:58 -04:00
parent d243c3aed0
commit 681c68f15a
2 changed files with 54 additions and 4 deletions

View File

@@ -60,7 +60,13 @@ function Export-Excel {
$Path,
[Parameter(ValueFromPipeline)]
$TargetData,
$WorkSheetname="Sheet1",
[string]$WorkSheetname="Sheet1",
[string]$Title,
[OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern="None",
[bool]$TitleBold,
[int]$TitleSize=22,
#[System.Drawing.Color]$TitleBackgroundColor,
#[string]$TitleBackgroundColor,
[string[]]$PivotRows,
[string[]]$PivotColumns,
[string[]]$PivotData,
@@ -88,12 +94,26 @@ function Export-Excel {
}
$ws = $pkg.Workbook.Worksheets.Add($WorkSheetname)
$Row = 1
if($Title) {
$ws.Cells[$Row, 1].Value = $Title
$ws.Cells[$Row, 1].Style.Font.Size = $TitleSize
$ws.Cells[$Row, 1].Style.Font.Bold = $TitleBold
$ws.Cells[$Row, 1].Style.Fill.PatternType = $TitleFillPattern
#if($TitleBackgroundColor) {
# $ws.Cells[$Row, 1].Style.Fill.BackgroundColor.SetColor([System.Drawing.Color]::Blue)
#}
$Row = 2
}
} Catch {
if($AlreadyExists) {
throw "$WorkSheetname already exists."
} else {
throw $Error[0].Exception.InnerException
throw $Error[0].Exception.Message
}
}
}
@@ -130,7 +150,11 @@ function Export-Excel {
$wsPivot.View.TabSelected = $true
$pivotTableDataName=$WorkSheetname + "PivotTableData"
$range="{0}:{1}" -f $ws.Dimension.Start.Address, $ws.Dimension.End.Address
$startAddress=$ws.Dimension.Start.Address
if($Title) {$startAddress="A2"}
$range="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$range], $pivotTableDataName)
if($PivotRows) {

View File

@@ -9,6 +9,11 @@ Know Issues
What's new
-
#### 4/13/2015
* You can add a title to the Excel "Report" `Title`, `TitleFillPattern`, `TitleBold`, `TitleSize`, `TitleBackgroundColor`
* Thanks to [Irwin Strachan](http://pshirwin.wordpress.com) for this and other great suggestions, testing and more
#### 4/10/2015
* Renamed `AutoFitColumns` to `AutoSize`
* Implemented `Export-MultipleExcelSheets`
@@ -20,13 +25,34 @@ What's new
#### 4/8/2015
* Implemented exporting data to **named sheets** via the -WorkSheename parameter.
#### Examples
Examples
-
`gsv | Export-Excel .\test.xlsx -WorkSheetname Services`
`dir -file | Export-Excel .\test.xlsx -WorkSheetname Files`
`ps | Export-Excel .\test.xlsx -WorkSheetname Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM`
#### Example Adding a Title
You can set the pattern, size and of if the title is bold.
$p=@{
Title = "Process Report as of $(Get-Date)"
TitleFillPattern = "LightTrellis"
TitleSize = 18
TitleBold = $true
Path = "$pwd\testExport.xlsx"
Show = $true
AutoSize = $true
}
Get-Process |
Where Company | Select Company, PM |
Export-Excel @p
![image](https://raw.githubusercontent.com/dfinke/ImportExcel/master/images/Title.png)
#### Example Export-MultipleExcelSheets
![image](https://raw.githubusercontent.com/dfinke/ImportExcel/master/images/ExportMultiple.gif)