Search code examples
c#exceloledbopenxml

System Resource Exceeded Exception thrown by OleDB when reading Open Office XML generated spreadsheets


I am currently working on an Excel import/export utility that is leveraging existing OleDB code for the import process, with the spreadsheets being generated using Open Office XML. So far, the spreadsheets are generated properly formatted and are opened in Microsoft Excel 2019 without issue. For some reason though, when attempting to import the data, OleDB immediately throws an exception and fails with the exception message being, "System Resources Exceeded." A strange workaround I've found however, is to open the spreadsheet in Microsoft Excel 2019 and then save it, and then attempt to import it. This suggests to me that I'm doing something wrong when generating the spreadsheet.

Here's some relevant code:

            const query = "Select TOP 25 * from [Sheet1$]";

            var ds = new DataSet();
            var connectionString = $"Provider=Microsoft.Ace.OLEDB.12.0;Data Source={fileName};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";

            using (var con = new OleDbConnection(connectionString))
            using (var adpt = new OleDbDataAdapter(query, con))
            {
                    adpt.Fill(ds, "[Sheet1$]"); // Exception occurs here
            }
            

            DataTable table = ds.Tables[0];
            return table;

And here's a LINK to a sample spreadsheet I generated with my Open Office XML Sheet writing class. I didn't want to copy all the code into this question do to it being fairly lengthy. I'm willing to post it if necessary though.


Solution

  • While this isn't the most clear answer, I ended up getting this to work. I had to download the Open XML SDK Productivity Tool, and do a diff between my generated sheet and what Microsoft Excel generated on save. Using its code generation tool and some patience, I loosely copied what the generated code looked like until it worked.

    The noted differences between my workbook versus there's:

    • The Microsoft workbook took advantage of shared strings essentially everywhere
    • The Microsoft workbook included a lot of xml namespace declarations and decorators
    • The Microsoft workbook included additional parts that I wasn't originally adding. I ended up copying everything but the theme part.