2010-12-01 3 views
0

J'essaie d'ouvrir un fichier XLSX (pour que je puisse créer une date) Iam en utilisant le code ci-dessous.Impossible d'ouvrir un fichier Excel en utilisant System.Data.OleDb.OleDbConnection

System.Data.OleDb.OleDbConnection oleDbCon; 
System.Data.OleDb.OleDbDataAdapter oleDbDataAd; 
oleDbCon = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties=Excel 8.0;"); 

mais quand le chemin du fichier contient un fichier avec l'extension XLSX, je reçois une erreur « Table externe n'est pas dans le format attendu. »

Le code ci-dessus fonctionne correctement lorsque le fichier est d'extension xls.

Dois-je changer la chaîne de connexion?

Une aide? Merci d'avance.

Répondre

4

Changé la chaîne de connexion à

public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
    + path + ";Extended Properties=Excel 12.0;"; 
2
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" 
    + Path +";"+"Extended Properties=Excel 8.0;"; 
OleDbConnection conn = new OleDbConnection(strConn); 
conn.Open(); 
string strExcel = ""; 
OleDbDataAdapter myCommand = null; 
DataSet ds = null; 
strExcel="select * from [sheet1$]"; 
myCommand = new OleDbDataAdapter(strExcel, strConn); 
ds = new DataSet(); 
myCommand.Fill(ds,"table1"); 
return ds; 
+0

Ceci est le même code qui fonctionne bien pour Word 2003 .. Besoin de changer la chaîne de connexion pour le faire fonctionner pour 2007 – Ananth

Questions connexes