Sujeet Raman

Sujeet Raman

  • 862
  • 927
  • 351.8k

Com object is not binding using Microsoft.Office.Interop.Excel in c#

Aug 20 2021 9:07 AM

I am binding an excel column using c#. I need to bind the date column in my excel. There is a constant year already in my excel cell like 1980. I need to append random "mm/dd "to it. I am using  Microsoft.Office.Interop.Excel here. i have tried the following code but it lost the constant year value. how can I fix? My cell format in excel is general. I tried different format though.

i am getting like  8/6/System.__ComObject instead of 08/06/1

for (int i = 2; i <= totalRows; i++)
{               
    xlWorksheet.Cells[i, 8] = GenerateRandomDates()+ xlWorksheet.Cells[i, 8] ; //not working expected 02/03/1980
} 
public string GenerateRandomDates()
{
    DateTime start = new DateTime(1995, 1, 1);
    int range = (DateTime.Today - start).Days;
    DateTime dt = start.AddDays(gen.Next(range));
    int month = dt.Month;
    int day = dt.Day;string formatted = string.Format("{0}/{1}/", month, day);
    return formatted;
}

Answers (3)