How to convert XLS files to XLSX so that they can be used in #PowerBI
I recently was having a challenge which we all have had when working with Power BI.
The customer I was consulting to have all their data extracts saved in XLS (The dreaded XLS!)
I thought that there must be a way to convert them quickly and easily, which I found after Googling ( I honestly cannot find the link to where I got most of the script to give them credit) and as I like to call hacking a PowerShell script.
Now there is no reason to be scared of PowerShell it is extremely powerful and with my example below you only need to change one variable in the PowerShell script, and it will do the conversion for you.
Not only that you can automate PowerShell scripts so that they can be run on a schedule, which could be part of your Power BI data refresh schedule.
NOTE: You have to have Excel installed.
Here are the steps below for the PowerShell script
-
I opened PowerShell and ran as Administrator
- I then copied the script below
$folderpath = "C:\Convert XLS" Add-Type -AssemblyName Microsoft.Office.Interop.Excel $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook write-host $xlFixedFormat $excel = New-Object -ComObject excel.application $excel.visible = $true $filetype ="*xls" Get-ChildItem -Path $folderpath -Include $filetype -recurse | ForEach-Object ` { $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf(".")) "Converting $path" $workbook = $excel.workbooks.open($_.fullname) $path += ".xlsx" $workbook.saveas($path, $xlFixedFormat) $workbook.close() $oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\old" write-host $oldFolder if(-not (test-path $oldFolder)) { new-item $oldFolder -type directory } move-item $_.fullname $oldFolder } $excel.Quit() $excel = $null [gc]::collect() [gc]::WaitForPendingFinalizers()
- The only line you change is Line 1 which is the $folderpath
In my example I had the following
I then changed the $folderpath to “C:\Convert XLS”
I then ran the script, which if you are running on your computer will open and close Excel, which is nothing to be concerned with.
Once completed I then went back to my folder and I had the following XLSX files
And all the XLS files are moved to the old folder
Conclusion
I was now able to import the data into Power BI and use it for some amazing reports.
I hope that you found this useful and if you got any questions or suggestions please leave them in the comments below.
Ooooooh that’s neat. Definitely saving this for later.
Awesome, glad you liked it
Why not use excelcnv as stated here?
http://justgeeks.blogspot.com/2014/08/free-convert-for-excel-files-xls-to-xlsx.html
HI there
Yes that too could work,I just prefer PowerShell as it gives me more control and flexibility
Hi Gilbert,
why do you need to convert xls to xlsx for PowerBI? You can do report with XLS too, or there are any stuffs that you can’t do it?
Hi there
My customer was using the Personal Gateway at a time when it did not allow refreshing of XLS files.
YOURE MY F****NG HERO!!!!! thx 4 sharing
Awesome, glad you found it useful
This worked flawlessly! Wonderful script! Thank you. Do you also have one for .doc to .docx? Our Cyber team recently implemented new GPO’s restricting the use of .xls and .doc so this tool is resolving all my user complaints about their old spreadsheets. Now I need to do this for Word as well.
Hi there
This might be able to be done by changing the details. I am certainly no PowerShell expert but I am certain it can be done
this script is awsome !!
Glad you liked it!
Thanks for sharing this.
My 64 bit Power BI Desktop was not accepting xls files (got this error: https://docs.microsoft.com/en-us/power-bi/desktop-access-database-errors). Rather than causing more compatibility errors or doing the conversion manually, I thought there must be someway to do this. This solution took minutes and saved me hours!
That is so awesome to hear! Thanks for letting me know and glad to hear that it helped you!
First of all, you are an absolute life saver. Second of all, I’d like to be a little greedy and as if you can help me with one more thing here. I’d like to insert into this script that the first 9 rows of the sheet are deleted prior to the file saving as xlsx. For reference, the sheet name is will always be the file name excluding the “xls” of course, so please let me know if that’s possible and any guidance you can give would be amazing. Either way, this was truly helpful and has saved a ton of wasted effort.
Hi there
Thanks for the comment, I am sure that this possibly can be done. I am no PowerShell expert, and if you had to search for it I am sure someone would have a solution?
Fantastic ..working like butter
Awesome, thanks for letting me know it helped!
Would this have been the original script page: https://gist.github.com/gabceb/954418
I cannot recall 100% but that does look familiar.