Excel date field conversion problem

I currently have an excel sheet with one of the columns that is in a date format.

What I see when I open the spreadsheet is like 12/29/09, and the program sees 40176.

I realized that this value is present when changing a column to general text.

My question is, how can I read the value 12/29/09 instead of 40176, or how can I change 40176 to a valid date?

My program is in C #. Must read in C #


Here is an example of my connection code if it helps anyone.

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
string myPath = @"C:\Test.xls";
excelApp.Workbooks.Open(myPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true, 1, 0);

 Microsoft.Office.Interop.Excel.Sheets sheets = excelApp.Worksheets;
 Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
 excelApp.Visible = true;

 if(((Microsoft.Office.Interop.Excel.Range)excelApp.Cells[r, 1]).Value2 != null)
     DateString = ((Microsoft.Office.Interop.Excel.Range)excelApp.Cells[r, 1]).Value2.ToString();
+3
source share
4 answers

DateTime.FromOADate(), double DateTime.

+12

, DateTime.FromOADate() DateTime. , , 12/29/09 , cell.Text .

+2

excel TEXT(val,format).

:

A2 =today(), , 40189 A2

=TEXT(today(),"mm/dd/yyyy"), , , "01/30/2012"

+1

.

Text(Value,FormatText)

1) 40176 excel A2 2),

 =TEXT(A2,"mm/dd/yy")

A3

Then the value will be 12/29/09. exactly as you requested

Attention:

If you put a single quote instead of a double quote, it may not work

0
source

Source: https://habr.com/ru/post/1706783/


All Articles