Getting time values from an Excel sheet

只愿长相守 提交于 2019-12-01 11:36:49

问题


      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.Excelto 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.


回答1:


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;


来源:https://stackoverflow.com/questions/23642985/getting-time-values-from-an-excel-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!