C# Excel Date value is coming as numeric value

2019-09-03 11:27发布

An excel file is generated by a third party app. I am writing a utility to further process that Excel file. When I read a column containing date value, I am getting a numeric string like 05/02/2016 (dd/MM/yyyy) is coming as 42405.

I have tried using .Text, .Formula, .Value, .Value2 - all the approaches are returning the numeric value.

I know about FromOADateTime, but my requirement is to read the date value as a string, exactly the way it is displayed on the screen.

Thanks.

EDIT 1: When I convert the column to Text in Excel, by using Format Cells and selecting Text, all the date values convert to numeric values even in Excel! Does that give any clue? Probably the third party app stores the data as numeric value, but Excel displays it as a date string on screen. I want to read the value displayed on screen.

EDIT 2:

This problem does not come with all the date values in that column. My first impression is that the third party app is not consistent while writing date values to Excel. But I have no control over it.

EDIT 3: Here is the link to download file: http://wikisend.com/download/316956/Prob.xls . In this file, I need to read all date values in Column B as text starting from Row 13

And here is the link to the screenshot of how it looks in my machine: http://wikisend.com/download/443994/Screenshot1.jpg

EDIT 4: Found the culprit in my code with the help of NineBerry's answer: a few lines before reading the Text property, I was calling ws.Columns.ClearFormats() and ws.Rows.ClearFormats();

I needed to do this as per this: How to get the range of occupied cells in excel sheet…, in order to correctly get the used range in the sheet!

The original problem is solved. Now the problem is how to use ws.UsedRange without calling ClearFormats() to correctly get the range of used cells.

My specific requirement is to get the range of used ROWS having data in any cells up to Col H. I Dont need the entire UsedRange in Excel, UsedRange within Col H is good enough for me.

Any solutions to this? Or should I post a new thread?

标签: c# excel date
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-03 12:17

Using the Text property does give the string that would be displayed to a user.

      private void button_Click(object sender, EventArgs e)
      {
           string path = @"C:\Test.xlsx";

           Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
           Workbook wb = excel.Workbooks.Open(path);
           Worksheet excelSheet = wb.ActiveSheet;

           //Read a specific cell
           Range cell = excelSheet.Cells[1, 3] as Range;

           // Show text content
           MessageBox.Show(cell.Text);

           wb.Close();
      }

If you don't see here what you see in Excel, you have to give us more details. Maybe provide a sample Excel file where a cell does not behave as expected.


This code DOES output the content of the second column in the file provided exactly as displayed in Excel:

  private void button1_Click(object sender, EventArgs e)
  {
       string path = @"D:\Downloads\Windows\Prob.xls";

       Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
       Workbook wb = excel.Workbooks.Open(path);
       Worksheet excelSheet = wb.ActiveSheet;

       //Read a specific cell
       for (int i = 1; i <= 150; i++ )
       {
            Range cell = excelSheet.Cells[i + 1, 2] as Range;
            textBox1.AppendText(cell.Text + Environment.NewLine);

       }
       wb.Close();
  }

If you don't see that, you should check that you are actually reading the correct file, and that you do not modify the file yourself from code before reading.

查看更多
登录 后发表回答