Processing of Excel import errors (xls, XLSX)
Recently, the following problems were encountered during the process of creating a small function to import an Excel file to the datagriview:
The link string of The Link Excel is written in this way.
String strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + strfilename + "; extended properties = Excel 8.0 ";
In this way, everything goes smoothly when writing the connection string to import * XLS. When I import the * XLSX file, the following error is reported: "system. Data. oledb. oledbexception: The External table is not in the expected format ";
So I searched the internet and found that to import * XLSX files, I need to change the link string:
The changed connection string is as follows:
String strconnection = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + strfilename + "; extended properties = Excel 12.0 ";
Change jet to ace and 4.0 to 8.0. After the test is completed, import the * XLSX file smoothly. There is little joy in your heart. Secretly happy, so easy to deal with, and then run again
Check whether the file * XLS is compatible. If something bad occurs, an error is returned. Speechless, I was thinking about Microsoft's broken technology, and it was not compatible. Later, I went online to search for a method.
Compatible with XLS and XLSX. However, after searching for half a day, no better solution was found. Therefore, only the Excel file that is opened (the suffix is determined) (xls is the link to a string with the else link to a later version). This solves the problem.
. Close up and share it. If you have a good method, please kindly advise. Thank you.