Quantcast
Channel: Excel Data Reader - Read Excel files in .NET
Viewing all articles
Browse latest Browse all 448

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

$
0
0
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
                    }
                    else
                    {
                        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();
                    }
                    
                }
                row++;
            }
            excelReader.Close();


            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,
Michal

Viewing all articles
Browse latest Browse all 448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>