Excel 到 DataGridView:来自 excel 的时间值在导入时变为十进制值

Excel to DataGridView: Time value from excel becomes decimal values when imported

我有问题。我已经可以将数据从我的 excel 文件导入到我的 Datagridview,但是我注意到我的 excel 文件中的时间在导入到我的 datagridview 时变成了十进制值,这令人困惑希望你能帮助我解决这个问题.

这是我的导入代码。注意:我从我的主窗体触发了我的导入操作,但填充了我的第二个窗体的 datagridview。

 private void importWeatherReportToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ImportWeatherData import = new ImportWeatherData();

        if (import.datareport_dgv.Rows.Count > 0)
        {
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open Excel File";
            openFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Open(openFileDialog1.FileName);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
                int rcount = worksheet.UsedRange.Rows.Count;                         
                for (int i = 1; i < rcount; i++)
                {
                   DataGridViewRow todayRow2 = new DataGridViewRow();
                   todayRow2.CreateCells(import.datareport_dgv);
                   int index = 0;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 1].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 2].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 3].Value; //time error 
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 4].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 5].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 6].Value; //time error
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 7].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 8].Value; //time error
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 9].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 10].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 11].Value; //time error
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 12].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 13].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 14].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 15].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 16].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 17].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 18].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 19].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 20].Value;
                   todayRow2.Cells[index++].Value = worksheet.Cells[i + 1, 21].Value;

                   //add this row to the grid
                   import.datareport_dgv.Rows.Add(todayRow2);
                   //import.datareport_dgv.Rows.Add(worksheet.Rows[i+1].Cells[j++].Value, worksheet.Rows[i+1].Cells[j++].Value);                        
                }                                         
            }
            import.Show();
        }
    }       

我在时间变成十进制值的一些行上做了评论。

这是我从 excel 导入的一行的示例:我用“|”分隔它让您轻松了解单元格值。

1/15/2015 12:42 |33.4 |12:42 下午 |33.4 |33.4 |12:42 下午 |60.8 |12:42 下午 |60.8 |60.8 12:42 下午 |100632.25 |12:42 下午 |100632.25 |100632.25 |12:42 下午 |0 |0 |0 |0 |0

虽然此值是我的数据网格视图中显示的值:这是上一行的导入 excel 行。

1/15/2015 12:42:35 PM   |33.40 °C   |0.529166666666667  |33.40 °C   |33.40 °C   |0.529166666666667  |61%    |0.529166666666667  |61%    |61%    |0.529166666666667  |100,632Pa  |0.529166666666667  |100,632Pa  |100,632Pa  |0.529166666666667  |0  |0  |0  |0  |0

希望你能帮助我。如果这是一个重复的问题,请不要给我负分谢谢

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

看我对这个问题的回答: Getting time values from an Excel sheet

对于每个时间值,您的代码应该与此类似。

float excelValue = worksheet.Cells[i + 1, 3].Value;

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;

todayRow2.Cells[index++].Value = hour + ":" + minutes + ":" + seconds;