0

I use the following code to read the contents of the xls file using ExcelDataReader.

List<string> excelFiles = GetExcelFileNamesInDirectory(Application.persistentDataPath);

        for (int i = 0; i < excelFiles.Count; i++)
        {
            using (var stream = File.Open(excelFiles[i], FileMode.Open, FileAccess.Read))
            {
                // Auto-detect format, supports:
                //  - Binary Excel files (2.0-2003 format; *.xls)
                //  - OpenXml Excel files (2007 format; *.xlsx)

                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {

                    // Choose one of either 1 or 2:
                    fileLog.text = reader.Name;

                    // 1. Use the reader methods
                    do
                    {
                        while (reader.Read())
                        {
                            // reader.GetDouble(0);
                        }
                    } while (reader.NextResult());

                    // 2. Use the AsDataSet extension method
                    var result = reader.AsDataSet();


                    // The result of each spreadsheet is in result.Tables
                }
            }
        }

there is one excel file and hence the code passes the for loop. Unfortunately, the code does not cross the line using (var stream.. and it ends right before creating reader using ExcelReaderFactory. am I missing any reference ?

  • It would be interesting to see GetExcelFileNamesInDirectory method definition. More likely the responses from the method are not valid filenames. Could you add definition of GetExcelFileNamesInDirectory to the OP – Anu Viswan Nov 16 '19 at 03:22
  • What do you mean by "ends right before creating reader"? Does it throw an exception? – Klaus Gütter Nov 16 '19 at 06:41
  • you should start by putting a try/catch block around the entire thing. Your process or user may not have sufficient permission to open the file, it may be read-locked by another process or the path is wrong. Also we do not know what `GetExcelFileNamesInDirectory` returns. The strings returned from that method could be invalid file paths. – Steffen Winkler Nov 16 '19 at 15:36

0 Answers0