从Excel单元格捕获时间值 [英] Capturing Time Values from an Excel Cell

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

问题描述

我正在编写一个Excel应用程序,它将从Excel文件读取和写入指定的值,并将其显示给用户。但是,当我尝试从具有数字格式或键入'hh:min'(Hour:Min)我无法获得这个价值。



这是我的代码...

  ws [dateTimePicker1。 Value.Day + 1] .get_Range(F+ i.ToString(),Type.Missing); 
if(range.Value2!= null)
val + = - + range.Value2.ToString(); //将FXX设置为val
lbHK1.Items.Add(val);

其中...




  • ws =我的工作表

  • dateTimePicker1 =我的日期时间选择器,可以帮助我决定哪个文件将被打开

  • i =是一个整数,可以帮助我确定该单元格的行号

  • range =是从Microsoft.Office.Interop.Excel.Range创建的对象



在我的例子中,当 i = 11 时, F11 是包含时间值为 06:30 (在Excel中, fx:06:30:00 )的单元格。但是,当我尝试获取该值时,它会返回一个 double 类型,例如 0.263888888888889



如何将Excel格式的值正确格式化,而不是无意义的双重值?

解决方案

Excel内部存储时间为双精度,包含24小时制的小数位数:因此6:30 AM将为0.2708333


I am writing an Excel app that will read and write specified values from an Excel file, and display them to the user. However, when I try to read from a cell which has a Number Format or a function typed 'hh:min' (Hour:Min) I cannot obtain that value how exactly I want.

Here is my code...

ws[dateTimePicker1.Value.Day + 1].get_Range("F" + i.ToString(), Type.Missing);
    if (range.Value2 != null)  
        val += " - " + range.Value2.ToString();   //Sets FXX to val
    lbHK1.Items.Add(val);

Where...

  • ws = my worksheet
  • dateTimePicker1 = my date time picker which helps me decide which file will be opened
  • i = is an integer that helps me decide Row number of that cell
  • range = is an object created from Microsoft.Office.Interop.Excel.Range

In my example, when i = 11, F11 is the cell that contains the time value which is 06:30 (in Excel, fx : 06:30:00). However, when I try to get that value, it returns a double type such as 0.263888888888889

How can I get the value formatted correctly as it is displayed in Excel, rather than a meaningless double value?

解决方案

Excel stores times internally as doubles containing decimal fractions of a 24-hour day: so 6:30 AM would be 0.2708333

这篇关于从Excel单元格捕获时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆