"The file you are trying to open, [filename], is in a different format than specified by the file extension" error opening Excel file

July 01, 2013 by Anuraj

.Net .Net 3.0 / 3.5 .Net 4.0 Windows Forms

Today I got another problem with Excel file opening. One of the my colleague wrote code to generate XLS / XLSX file using OleDb object. And it was working perfectly. Only problem, while opening the XLS / XLSX file, MS Excel was displaying an error like this.

And clicking Yes will open the file properly. The root cause of the problem was connection string. He wrote some code to identify x64 and x32 systems, and based on the platform, he was changing the connection string. For the x64 bit version his connection string was like this. And it was generating an XLSX file.

Provider=Microsoft.ACE.OLEDB.12.0; 
Data Source={0}; Extended Properties=Excel 12.0

I modified the connection string like this.

Provider=Microsoft.ACE.OLEDB.12.0; 
Data Source={0}; Extended Properties=Excel 12.0 Xml

And it worked. Microsoft also providing some solutions like changing the Group Security setting. After updating the registry setting you’ll not receive this error.

Happy Programming.

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub