开发者

C# Excel Reader turns Timestamps into a decimal number

When I use the Excel Reader it reads in everything fine except for time stamps. It turns, for example, 15:59:35 into .67290509259259268

How do I stop this from happening?

object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

That is my array that is holding the values that are read in from the exce开发者_开发知识库l sheet. Not sure if that is the reason.


Try DateTime.FromOADate - however, the numeric value you mentioned in the question doesn't actually correspond to the time you mentioned.


The reason for this is Excel stores all it's DateTimes as floating point numbers. The decimal part is the time component while the integer part represents the date.

You can get use Range.Text value to get the text, which should be formatted correctly. I don't think you can use this quite in the same way as above (trying to do the same myself so not got the actual approach yet). Also be wary it might be slow reading the text (reading number formats is v.slow).

Alternatively try using a library, FlexCel is a very good one we use, or Apose for a more complete solution.

Itterative Approach (this is almost certainly considerably slower than get_Value returning an object[,]).

if (excelRange!= null)
{
   int nRows = excelRange.Rows.Count;
   int nCols = excelRange.Columns.Count;
   for (int iRow = 1; iRow <= nRows; iRow++)
   {
      for (int iCount = 1; iCount <= nCols; iCount++)
      {
         excelRange= (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, iCount];
         String text = excelRange.Text;
      }
   }
}

(Edit: Removed other examples that were actually for Sharepoint.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜