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.
An exception stating:
Cannot find central directory
indicates that one of the following is likely true:
.xlsx file (are you sure it isn't an .xls file?)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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With