简体   繁体   中英

Can't read xlsx file using OLEDB in C#

I have a website that users can upload an xlsx-file to. My application then reads the content and does some stuff. But when some users upload a file my application can't read the content with the error 'External table is not in the expected format'

One of the users is using Excel 2007. The weird thing is that if I open his file with my Excel 2013 and save it without changing anything, it works fine.

This is the oledb connectionstring:

OleDbConnection oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source=" + filepath + @";
        Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");

And I have installed Microsofts AccessDatabaseEngine.

Another thing is that if I rename the newly saved file (Excel 2013) to *.zip then I can open it with rar/explorer, but if I try to do the same with his file (Excel 2007) I can't open it.

Edit: I now installed Office 2007 on my PC so I have both 2007 and 2013. I then did the zip-trial and, with the same file, every time I saved with 2007, I couldn't open with winrar but when I saved with 2013 it worked fine. I did this multiple times with the same file back and forth. Same result.

Edit2: Did as suggested and used EPPlus instead. Same result. Works fine when saved with Excel 2013 but doesn't work when saved with Excel 2007.

After some more googling I came upon the answer which is posted here: Error reading xlsx (2007) file in EPPLUS

It appears that there is an error when the document is password-protected and saved with Excel 2007. When I removed all protection and saved it again (still in Excel 2007), it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM