2

I am using excel data reader, and I've noticed it is not compatible with older excel 5.0/95 workbook files. Is there a way I can get the version of the .xls file before I send it into excel data reader to prevent sending in earlier versions of excel files?

here is what I am using now.

if (extension == ".XLS")
                    {
                        IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                        excelReader.IsFirstRowAsColumnNames = true;
                        result = excelReader.AsDataSet();
                    }
                    else
                    {
                        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        excelReader.IsFirstRowAsColumnNames = true;
                        result = excelReader.AsDataSet();
                    }
Mitch
  • 389
  • 1
  • 6
  • 19

2 Answers2

1

What you have is correct. But you could make it a bit cleaner. For example:

IExcelDataReader excelReader;

if (String.Compare(extension, ".xls", true) == 0){
    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
} else if (String.Compare(extension , ".xlsx", true) == 0){
    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}

excelReader.IsFirstRowAsColumnNames = true;
result = excelReader.AsDataSet();
Donal
  • 31,121
  • 10
  • 63
  • 72
  • yes thank you. No need to repeat that code. Any suggestions on how to determine if the .xls file is earlier than 97? – Mitch Oct 06 '14 at 19:19
  • @Mitch There is a way to read structured storage - but not sure if you want to go down that route. See here: http://stackoverflow.com/questions/2070985/programmatically-finding-an-excel-files-excel-version – Donal Oct 06 '14 at 19:47
0

There should be a string in the last line of an old excel file that specifies which version of Excel created it. I forget the exact wording for those really old ones, but for Excel 2003 it was "Microsoft Excel 2003 Worksheet"

Mr. Mascaro
  • 2,693
  • 1
  • 11
  • 18