我正在编写一个从CSV文件中提取数据的程序(由于结构,通过Excel更容易处理)。有列保存日期和时间。日期列正确处理,但时间列(F)被解释为double。例如,在下面的循环中,它在第一个循环上看到值为0.00,在第二个循环上看到0.25,在第三个循环上看到0.26041666666666669,在第四个循环上看到0.27083333333333331o,依此类推。
for (i = startRow; i <= endRow; i++)
{
PeriodSales saleRow = new PeriodSales();
DateTime saleDate = Convert.ToDateTime((sheet.Cells[i, 5] as Excel.Range).Value);
var timeString = (sheet.Cells[i, 6] as Excel.Range).Value;
DateTime timeOfSale;
timeOfSale = new DateTime(saleDate.Year, saleDate.Month, saleDate.Day, 0, 0, 0);
// the lines below were commented out for testing purposes
(so I could see the value of timeString in the loop */
/* if (timeString != "0")
{
String[] timeArray = timeString.Split(':');
timeOfSale = new DateTime(saleDate.Year, saleDate.Month, saleDate.Day, Convert.ToInt32(timeArray[0]), Convert.ToInt32(timeArray[1]), 0);
}
else
{
timeOfSale = new DateTime(saleDate.Year, saleDate.Month, saleDate.Day, 0, 0, 0);
} */
基础CSV(在Notepad ++中)
答案 0 :(得分:2)
答案 1 :(得分:1)
你想用 timeOfSale = DateTime.FromOADate(timeString).TimeOfDay; 这将使用excel格式转换日期