Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find central directory error

Tags:

c#

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 i'm currently tackling with is an error that "Cannot find central directory". I don't know what this means I have even tried moving the excel file to different locations but i'm still facing the same error.

like image 966
TheBells Avatar asked Dec 15 '15 14:12

TheBells


1 Answers

An exception stating:

Cannot find central directory

indicates that one of the following is likely true:

  1. The file is corrupt
  2. The file is not actually an .xlsx file (are you sure it isn't an .xls file?)
  3. The library you're using to read the file has a bug

From your code it looks like you're using ExcelDataReader and attempting to open an XML format (xlsx) file. Are you sure that the file isn't actually a .xls file that someone has mis-named as .xlsx? You could check this by using:

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

instead of:

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
like image 118
Rob Avatar answered Sep 18 '22 20:09

Rob