Search code examples
c#.netexceloledb

Unable to read xlsx file through OleDb with the size more than ~1mb


I'd like to use sql bulk copy in order to load data from *.xlsx file to the data base. But, I've faced the problem when file size is more than approximately 1mb. When I try to open OleDbConnection I get an error

No error message available, result code: E_FAIL(0x80004005)

Does anyone have an idea about such behavior?

P.S. If file size is less than mentioned above everything works as expected.

string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;";

// Create the connection object 
OleDbConnection oledbConn = new OleDbConnection(connString);
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + WorkSheetName + "$" + DataRange + "]", oledbConn);
OleDbDataReader dr = cmd.ExecuteReader();

string ProfDbBulkCopyConnString = ConfigurationManager.ConnectionStrings["DbBulkCopyConnString"].ToString();
SqlBulkCopy sb = new SqlBulkCopy(ProfDbBulkCopyConnString);
sb.ColumnMappings.Add("Status", "ActionStatus");
sb.ColumnMappings.Add("Process", "ProcessExec");
sb.DestinationTableName = "dba.Execute";
sb.WriteToServer(dr);

Solution

  • Just a brief description of fixing. For more information I recommend to visit:

    Essentially, xlsx format is some kind of zip archive with a bunch of xml files. So, first of all ACEOLEDB provider try to unzip all data directly to memory buffer, but in case of large xlsx file provider unable to unzip all data to memory buffer and it forced to create temp file on the hard drive. If user hasn't permission to the folder Content.MSO on hard drive mentioned problem appears. Path to folder depends on your enviroment. In my case it is C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO (32-bit driver on 64-bit Windosw Server 2008 R2). So, grant access to Content.MSO for user "IIS AppPool\DefaultAppPool" and problem goes away.