简体   繁体   中英

Getting time values from an Excel sheet

      Microsoft.Office.Interop.Excel.Application app = 
                                new Microsoft.Office.Interop.Excel.Application();

      Workbook wb = app.Workbooks.Open(fname);
      Worksheet ws = wb.Worksheets[1];

      // read some data                    
      var Monday = ws.get_Range("D11");
      var Tuesday = ws.get_Range("D13");

I am using Microsoft.Office.Interop.Excel to retrieve hours worked from an Excel sheet. My code seems to be working properly however I keep getting the values COM Object for Monday and Tuesday .

Why is it not returning the actual values in cell D11 and D13 from my spreadsheet??

I am also getting the same COM Object values for ws and wb , not sure if this has any relevance thought I would just throw that out there.

MS Excel stores the dates as float values. The integer part represents the days and the fractional part keeps the hours, minutes and seconds.

Check this code that extracts the hours and also the minutes and seconds, maybe you need them:

float excelValue = 0.4f;

int miliseconds = (int)Math.Round(excelValue*86400000);
int hour = miliseconds/( 60/*minutes*/*60/*seconds*/*1000 );
miliseconds = miliseconds - hour*60/*minutes*/*60/*seconds*/*1000;
int minutes = miliseconds/( 60/*seconds*/*1000 );
miliseconds = miliseconds - minutes*60/*seconds*/*1000;
int seconds = miliseconds/1000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM