简体   繁体   English

从Excel工作表获取时间值

[英]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. 我正在使用Microsoft.Office.Interop.Excel从Excel工作表中检索工作时间。 My code seems to be working properly however I keep getting the values COM Object for Monday and Tuesday . 我的代码似乎正常工作,但是我一直在MondayTuesday获取值COM Object

Why is it not returning the actual values in cell D11 and D13 from my spreadsheet?? 为什么不从电子表格返回单元格D11和D13中的实际值?

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. 我也为wswb获得了相同的COM Object值,不确定是否有任何相关性,以至于我只是将其扔在那里。

MS Excel stores the dates as float values. MS Excel将日期存储为浮点值。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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