如何通过C#从excel表读取数据表时,将时间检索为时间本身而不是日期时间 [英] how to retrieve time as time itself rather than date time while reading to datatable from excel sheet via C#

查看:131
本文介绍了如何通过C#从excel表读取数据表时,将时间检索为时间本身而不是日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过C#代码从excel表读取数据表时检索时间本身而不是日期时间

解决方案

Excel将时间值存储为日期 [ ^ ](日期时间 [ ^ ]。请参阅此处:数据模型中的数据类型(Excel) [ ^ ]



我认为您需要将日期格式化为时间:自定义日期和时间格式字符串 [ ^ ], DateTime的字符串格式(C#) [ ^ ]



 DateTime date1 =  new  DateTime( 2013  4  24  19  27  15  18 ); 
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString( hh:mm:ss.f,ci));
// 显示07:27:15.0
Console.WriteLine(date1。 ToString( hh:mm:ss.F,ci));
// 显示07:27:15


How to retrieve time as time itself rather than date time while reading to datatable from excel sheet via C# Code behind

解决方案

Excel stores time values as Date[^] (DateTime[^] in C#). See here: Data types in Data Models (Excel)[^]

I think you need to format date as "time": Custom Date and Time Format Strings[^], String Format For DateTime (C#)[^]

DateTime date1 = new DateTime(2013, 4, 24, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15


这篇关于如何通过C#从excel表读取数据表时,将时间检索为时间本身而不是日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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