Feroz Khan

Feroz Khan

  • 1.1k
  • 300
  • 92.3k

Generating Excel from DataTable

Aug 28 2023 4:04 PM

Hello Everyone, 

i am generating excel file and its generating as well only issue is some numeric value format is not correct format and because of that i cannot perform any formula. below snap for your refrence.

Code:

cs.dt = (System.Data.DataTable)dataGridView1.DataSource;
System.Data.DataTable dtclone = cs.dt.Clone();
dtclone.Columns[7].DataType = typeof(decimal); //In your case you need to change WaitTime and AssistTime
dtclone.Columns[8].DataType = typeof(decimal);

foreach (DataRow row in cs.dt.Rows)
{
    dtclone.ImportRow(row);
}
int b = 0;
string[,] dataa = new string[dtclone.Rows.Count, dtclone.Columns.Count];
foreach (DataRow row in dtclone.Rows)
{
    int j = 0;
    foreach (DataColumn c in dtclone.Columns)
    {
        dataa[b, j] = row[c].ToString();
        j++;
        // Thread.Sleep(100);
    }
    b++;
}
xlWorkSheet.Range[xlWorkSheet.Cells[4, 1], xlWorkSheet.Cells[dtclone.Rows.Count + 3, dtclone.Columns.Count]].value = dataa;


Answers (3)