When I try read Excel with OLE DB all values are empty

ぐ巨炮叔叔 提交于 2019-12-11 10:34:20

问题


I have done a little program to parser excel. It works fine only when before to execute it I open Excel file manually (is not it strange?). I.e. first I open excel file, second I execute program and I get good results

If I don't open excel before to execute it I get empty values

My connection string (excel file has extension .XLSX):

connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                   "Data Source=" + path + "\\" + f.Name + ";" +
                   "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";

My code to open connection with oleDB:

using (OleDbConnection cnn = new OleDbConnection(connectionString))
{
    cnn.Open();
    ...
    String sql = "SELECT * FROM [" + sheetNames[i] + "]";
    OleDbDataAdapter da = new OleDbDataAdapter(sql, cnn);
    DataTable dt = new DataTable();
    da.Fill(dt); // Now 'dt' should has all data
}

Also, I have installed AccessDatabaseEngine.exe and AccessRuntime.exe

Obviously, my purpose is run the program without having to manually open the file. Any suggestion?

Thanks for your time.


回答1:


I found it a real pain when I tried to get OleDb and Excel to play nicely together. Fortunately, I found a much better approach: EPPlus

EPPlus is a .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx).

Open source, feature rich and easy to use. If at all possible, use it instead of OleDb.



来源:https://stackoverflow.com/questions/33033034/when-i-try-read-excel-with-ole-db-all-values-are-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!