Cannot find central directory error

I am trying to read data from an excel file.

FileStream stream = File.Open (@"C:\Temp\F1\SMRPAC974-00024COMINVDETEXTRACT.xlsx", FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); excelReader.Close(); string csvData = ""; int row_no = 0; while (row_no < result.Tables[0].Rows.Count) { for (int i = 0; i < result.Tables[0].Columns.Count; i++) { csvData += result.Tables[0].Rows[row_no][i].ToString() + ";"; } row_no++; csvData += "\n"; } 

The problem that I am currently facing is an error that "Cannot find the central directory." I do not know what this means that I even tried to move the excel file to different locations, but I still encounter the same error.

+8
c #
source share
3 answers

An exception:

Cannot find central directory

indicates that one of the following is likely:

  • The file is damaged.
  • The file is not really a .xslx file (are you sure this is not a .xls file?)
  • There is an error in the library that you use to read the file

From your code, it looks like you are using ExcelDataReader and trying to open an XML ( xlsx ) file. Are you sure that the file is not actually an .xls file that someone incorrectly named .xlsx ? You can verify this using:

 IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); 

instead:

 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 
+15
source share

verify that you are using the .xlsx or .xls file.

If you are using .xlsx, use

  IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 

if you use .xls use

  IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); 

Hope this helps. It worked for me.

+2
source share

You will also get this error if the Excel file is password protected.

0
source share

All Articles