How to read formatted data from an Excel file

Hi,

I'm writing a small app that reads from an excel file. All works fine if all of the cells in the excel file is formatted as strings.

Problem:

Cell has a date: Format - > 1/2/2006 or

Cell has a custom format -> ddd (This format gives short days of the week (Mon, Tue)).

If I read the data from these cells, I get a number instead of the date string or day of the week.

                // Open the file.
                ExcelWorkBookSchedule = ExcelObj.Workbooks.Open(
                            "FilePath", 0, false, 5,
                            "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false,
                            0, true, true, false);

                // Sheets within the workbook.
                Excel.Sheets sheets = ExcelWorkBookSchedule.Sheets;

                // Specific work sheet (first one) from the group of sheets.
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

               // Range of cells to read.
               Excel.Range range = worksheet.get_Range("A2", Type.Missing);
               

               // Below is the "Problem Line". The cell in the excel sheet will show the

              //  date but, when read, it reads a number equiv. I guess excel uses this

              //  to calculate dates

               string strVal = range.Value2.ToString();

               listView1.Items.Add(new ListViewItem(strVal)); 

Anyone know how to convert this number at runtime to the string format

Thanks for your time.

 



Answer this question

How to read formatted data from an Excel file

  • johnko

    Hi,

    I have same problem like you. Do you have some news about it

    Thx in advance,


  • How to read formatted data from an Excel file