从Excel工作表获取时间值

从Excel工作表获取时间值

本文介绍了从Excel工作表获取时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft.Office.Interop.Excel.Application(); $ $ $ $ $ $ $ $ $

工作簿wb = app.Workbooks.Open(fname);
工作表ws = wb.Worksheets [1];

//读取一些数据
var Monday = ws.get_Range(D11);
var Tuesday = ws.get_Range(D13);

我正在使用 Microsoft.Office.Interop.Excel 从Excel表中检索工作时间。我的代码似乎正常工作,但是我不断得到 COM对象星期一周二



为什么不从我的电子表格中返回单元格D11和D13中的实际值?



我是对于 ws wb 的$ ,不知道这是否有任何相关性,我只会把它抛出去。

解决方案

MS Excel将日期存储为浮点值。整数部分表示日期,小数部分保持小时,分和秒。



检查此代码,提取小时数以及分钟和秒数,也许您需要他们:

  float excelValue = 0.4f; 

int miliseconds =(int)Math.Round(excelValue * 86400000);
int hour = miliseconds /(60 / * minutes * / * 60 / * seconds * / * 1000);
miliseconds =毫秒 - 小时* 60 / *分钟* / * 60 / *秒* / * 1000;
int minutes = miliseconds /(60 / * seconds * / * 1000);
miliseconds = miliseconds - minutes * 60 / * seconds * / * 1000;
int seconds = miliseconds / 1000;


      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.

解决方案

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;

这篇关于从Excel工作表获取时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:36