[英]C# how convert hh:mm from excel cells
my value in excel cell has total time: 227:20 and this format [hh]:mm我在 excel 单元格中的值的总时间为:227:20,此格式 [hh]:mm
but not convert in c# this format and show 11:20 in gridview但不转换为 c# 这种格式并在 gridview 中显示 11:20
my code:我的代码:
model.ezafekari = DateTime.FromOADate((double)ws.Cells[rowNum, 17].Value).ToString("HH:mm");
I'm assuming that your formula:我假设你的公式:
(double)ws.Cells[rowNum, 17].Value
Will yield what Excel will show as the General
formatted value for the cell (in my experience, that's what you see for date/time cells).将产生 Excel 将显示为单元格的General
格式值(根据我的经验,这就是您在日期/时间单元格中看到的内容)。 I'm not going to get code running against Excel.我不会让代码针对 Excel 运行。
Once you have that value, this should work:一旦你有了这个值,这应该可以工作:
var real = 9.472222222; // should be (double)ws.Cells[rowNum, 17].Value
var days = (int)Math.Truncate(real);
var fracDays = real - days;
var fracHours = fracDays * 24.0;
var hours = (int) Math.Truncate(fracHours);
var minutes = (int) Math.Round ((fracHours - hours) * 60);
var totalHours = (days * 24) + hours;
var toShow = $"{totalHours}:{minutes}";
If you step through that code, you will see:如果您单步执行该代码,您将看到:
Variable多变的 | Value价值 |
---|---|
days天 | 9 9 |
hours小时 | 11 11 |
minutes分钟 | 20 20 |
totalHours全部小时数 | 227 227 |
toShow以显示 | 227:20 227:20 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.