0

I have a program that takes data out of an excel sheet and puts it in a text doc. My problem is that some cells are dates but not all. Atm the output format is dd/MM/yyyy 00:00:00 in my textdoc. Is there a way to check if its a date and if yes chop off the time part?

I tried something with TryParse as you can see but I honestly don't know if it even makes sense

 DateTime dDate;



 var allRowTexts = new List<string>();
 for (int row = 5; worksheet.Cells[row, 5].Value != null; row++)
 {

   // loop all configured Columns
   var rowText = new StringBuilder();
   foreach (var col in configuredColumns)
   {

    var cell = worksheet.Cells[row, col];

    if (cell.Value == null)
    {
     rowText.Append("leer" + "\t\t\t\t");
    }

    else
    {
       if (DateTime.TryParse(cell.Value.ToString(), out dDate))
       {
        String.Format("{0:dd/MM/yyyy}", dDate);
        rowText.Append(dDate+"\t\t");
       }
       else
       {
       rowText.Append(cell.Value.ToString()+"\t");
       }

     }
   } 
 }
 allRowTexts.Add(rowText.ToString()+"\n");
T.Steph
  • 17
  • 7
  • 4
    Seems like a reasonable approach. Noticed one thing - you're not using the return value from `String.Format("{0:dd/MM/yyyy}", dDate);` - assign that to a variable and then add that to `rowText`. – stuartd Feb 14 '19 at 12:58
  • 1
    Worked! Thank you very much – T.Steph Feb 14 '19 at 13:07
  • How about formatting the cell value https://stackoverflow.com/questions/40209636/epplus-number-format/40214134#40214134 – VDWWD Feb 14 '19 at 14:26

0 Answers0