Import values from excel sheet [issue with dates and time]

Jun 7, 2014 at 4:27 AM
Dear colleagues,
First I'd like to emphasize, that I read any possible topic related with dates and field formats.
Because I'm trying the possibilities of this library (but also I'm learning programming) I wanted to check whether the imported field from excel sheet is a date, datetime, time or any other stuff I prepared some excel with the dates and other type of fields.
Unfortunately I can't determine correctly what type of field is delivered in the excel sheet. To make my project faster and less time consuming I tried the method which I found on this forum. Instead of:
DataFromExcel[row, column] = excelReader[column].ToString();
I put:
DataFromExcel[row, column] = excelReader.GetValue(column).ToString();
"Value" should fix the problem with receiving field value with it's format without any changes. So for my test I did an excel sheet saved as XLS and XLSX. This is how I fill the array with data and then I display it with the messageBox:
DataFromExcel = new object[rows, cols+1];
            int row=0;
            while (excelReader.Read())
                for (int column = 0; column<cols; column++)
                    if (excelReader[column] != null)
                        DataFromExcel[row, column] = excelReader.GetValue(column).ToString(); // read the data from the excel sheet field
                        DataFromExcel[row, column] = "";

                    if (excelReader[2] != null) // check the type of the field (in my case it's DateTime )
                        DataFromExcel[row, 5] = excelReader.GetValue(2).GetType();

            for (int i = 1; i < rows; i++) // check what's imported.
                MessageBox.Show("ROW " + i + ": " + DataFromExcel[i, 0] + " ^ " + DataFromExcel[i, 1] + " ^ " + DataFromExcel[i, 2] + " ^ " + DataFromExcel[i, 3] + " ^ " + DataFromExcel[i, 4] + " ^ " + DataFromExcel[i, 5]);
And those are the results: For the XLSX file the datetime field is correctly imported (and shown in messageBox) and the provided type is System.DateTime. However for the XLS file instead of correct date I get the value e.g. '40613,3476851852' and the value type is shown as 'System.Double'.

My questions are:
  1. Is it possible to correctly check what is the type of the field in Excel sheet, so I could prepare some formatting for delivered data?
  2. Is it a bug with this dates format provided by the same method (GetValue) from the XLS file?
  3. Is there any other method to "download" data from Excel file with the values as they are written in the sheet (regardless to their type and format)?
I'd like to say, that I'd like to do something with the values delivered from the Excel sheet but I would like to assume, that the format of the excel sheet may be different every time I'd like to load it.

I'd appreciate for the support.

Best regards,
Jun 12, 2014 at 3:43 PM
If there is a discrepancy in the date/time behaviour between XLS files and XLSW files, the first thing you should check is if you created the binary reader using one of the methods that has the convertOADate parameter, and set the value of the parameter to true, i.e.
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(filestream, true);