我有问题。我可以将已经数据从我的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导入的一行示例:我用&#34; |&#34;让您轻松了解细胞值。
1/15/2015 12:42 | 33.4 | 12:42 PM | 33.4 | 33.4 | 12:42 PM | 60.8 | 12:42 PM | 60.8 | 60.8 12:42 PM | 100632.25 | 12:42 PM | 100632.25 | 100632.25 | 12:42 PM | 0 | 0 | 0 | 0 | 0
虽然这个值是我的datagridview中显示的值:这是上面一行的导入的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
希望你能帮助我。如果这是一个重复的问题,请不要给我负面的分数感谢
答案 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;