2015-01-15 2 views
0

문제가 있습니다. 내 Excel 파일에서 내 Datagridview로 이미 데이터를 가져올 수 있지만 필자의 통보로 시간을 내 데이터 파일에 10 진수 값을 가져올 때이 문제를 도와 줄 수있는 혼란스러운 희망입니다.DataGridView 로의 Excel : 가져온 경우 Excel에서의 시간 값이 십진수 값이됩니다.

이것은 가져 오기위한 코드입니다. 참고 : 내 기본 폼에서 가져 오기 작업을 트리거하지만 내 두 번째 폼의 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(); 
     } 
    }  

시간이 10 진수 값이되는 일부 행에 대해 의견을 말했습니다.

여기는 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

이 값은 my 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

내가 데이터를 가져올 때 DataGridview로 데이터를 가져올 때 "12:42 PM"대신 "0.529166666666667"이 표시됩니다. – Solem

답변

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; 
+0

내 아이디어를 어떻게 요약할까요? – Solem

+0

worksheet.Cells [i + 1, 3] .Value는 excelValue 변수입니다. 다른 시간 값과 동일합니다. –

+0

나는 미안하다. – Solem

관련 문제