excelpowershellpowershell-module

PowerShell module ImportExcel only returning up to column AF2 when many other columns available to the right of AF2


I am using the PowerShell module ImportExcel from here: https://github.com/dfinke/ImportExcel

To eliminate my complex code I am just running the following: (xxx indicates folders I dont want to show)

$ExcelFilePath = "C:\Users\<xxx>\code\<xxx>\<xxx>\AllSqlServersV2.xlsx"
$Excel = Open-ExcelPackage -Path $ExcelFilePath
$Excel.Recommended_for_instances.Dimension

And here is the output for $Excel.Recommended_for_instances.Dimension:

Start   : OfficeOpenXml.ExcelCellAddress
End     : OfficeOpenXml.ExcelCellAddress
Table   : 
Address : A1:AF2 <=== 👀🛑
IsName  : False
Rows    : 2
Columns : 32 <=== 👀🛑

And here is a screen shot of the data. You will see a vertical red line after column AF to show that I can get any data after this point. (sorry for the very wide picture)

All the data: enter image description here

A snapshot of the data point that I am having issues getting past: enter image description here

As you can see, I am not doing anything out of the ordinary. The interesting thing is, if I open the Excel file, press ctrl s only (save the file without making any changes) then the output of the above code becomes correct:

Start   : OfficeOpenXml.ExcelCellAddress
End     : OfficeOpenXml.ExcelCellAddress
Table   : 
Address : A1:BA2 <=== 👀✅
IsName  : False
Rows    : 2
Columns : 53 <=== 👀✅

I have looked for a reason for this all day but can't find anything useful.

Any help would be great.

cheers

Russ


Solution

  • I've compared your original spreadsheet file in the GitHub issue versus the one re-saved by Excel and it looks like there's a couple of parts that Excel saves differently to whatever tool you used to generate the original file.

    These differences alter how EPPlus (the underlying library used by ImportExcel) processes the file, which at least explains the different results you're seeing in the Dimension property in ImportExcel.

    Here's some screenshots from BeyondCompare comparing the unzipped original file and the one saved in Excel:

    enter image description here

    Some things to note:

    There's some additional differences further down the file:

    enter image description here

    Summary

    There's a couple of things Excel saves differently compared to your original file that mean EPPlus can process the saved file correctly, and this surfaces up in the Dimension property ImportExcel module.

    Maybe these differences are allowed in older / newer versions of the OpenXML spec, or maybe Excel has some additional error handling for reading and correcting some common errors in files, but as it stands, EPPlus (and vicariously, ImportExcel) doesn't like the files generated by your application :-(