dfinke / ImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel

Home Page:https://www.powershellgallery.com/packages/ImportExcel/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NumberFormat Dates

CraigChamberlain opened this issue · comments

Hi,

I've been trying to write some dates into a spreadsheet and it's defaulting to full datetime with General display.

I'm going to make an expression $Date = @{Name = "Date"; Expression = {$_.Date.ToString('dd/MM/yyyy')}} but I was trying to use a parameter like.

-NumberFormat "'dd/mm/yyyy';;;;0" where my table has 5 colums and the final just shows ints.

I'm having problems getting the NumberFormat to work at all, getting a style.xml error on opening file, even on something simple like
-NumberFormat ";;;;0"

Should dates be possible and could we get an example, maybe with a table with a few alphanumeric columns along with a few numeric?

Thanks

Craig

Dates are possible. It is not clear to me what you trying to do. Please check out the examples folder in the repo for dates.

Hi sorry to be a pest dfinke but I did not get to the bottom of this. It seems to be possible to have more than one date format separated by ';' as per ColorizeNumbers.ps1 and PosNegNumbers.ps1

So I tried to create a little date example to merge request into the repo. However, I can't get it to behave how I expect. Should it be possible. I've experimented will all sorts of combinations and can not get the dates to behave using Export-Excel -NumberFormat

try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}

$file = "dates.xlsx"

Remove-Item $file -ErrorAction Ignore
$date1 = Get-Date -Year 2023 -Day 4 -Month 12
$date2 = Get-Date -Year 2022 -Day 5 -Month 11
$date3 = Get-Date -Year 2021 -Day 6 -Month 1

$data = $(
    New-PSItem $date1 $date1 100 "Some Text"
    New-PSItem $date2 $date2 1 "Some Other Text" 
    New-PSItem $date3 $date3 1.2 "Some More Text"
)


$data | Export-Excel -Path $file -Show -AutoSize -NumberFormat "yyyy;dd/mm/yyyy;0.#0"

Expected:

p1 p2 p3 p4
2023 04/12/2023 100.0 Some Text
2022 05/11/2022 1.0 Some Other Text
2021 06/01/2021 1.20 Some More Text

Got:

p1 p2 p3 p4
############# ############# 1900 Some Text
############# ############# 1900 Some Other Text
############# ############# 1900 Some More Text

N.B #### Default long date format

However, I did manage to achieve something by setting columns individually.

$excel = Open-ExcelPackage $file
Set-Column -ExcelPackage $excel -Column 1 -NumberFormat 'yyyy'
Close-ExcelPackage $excel -Show

At the very least this is a work around but maybe there a parsing bug or a method or a working example that could go in an Export-Excel examples.

Correct. You can’t do it all with export-excel. Set-excelcolumn and set-excelrange are the way to go.