c# - SQL Table Headers Not Saved To Excel -


the following code used save data sql table excel. problem is, not save table headers if table empty.

                worksheet4 = workbook.sheets[4];                 worksheet4.name = "adjs ";                  sql.datatable dtag = new sql.datatable();                 using (sqlconnection cn1 = new sqlconnection(constr))                 {                     using (sqldataadapter da4 = new sqldataadapter(query4.tostring(), cn1))                     {                         da4.fill(dtag);                      }                 }                 datacolumncollection dccollection4 = dtag.columns;                 (int = 1; < dtag.rows.count + 1; i++)                 {                      (int j = 1; j < dtag.columns.count + 1; j++)                     {                         if (i == 1)                              worksheet4.cells[i, j] = dccollection4[j - 1].tostring();                          else                              worksheet4.cells[i, j] = dtag.rows[i - 1][j - 1].tostring();                      }                  } 

any appreciated.

you add columns before start filling out data:

int = 1; foreach(datacolumn dc in dtag.columns) {       worksheet4.cells[1, a] = dc.columnname;       a++; } 

and start next row:

for (int = 2; < dtag.rows.count + 1; i++) {     (int j = 1; j < dtag.columns.count + 1; j++)     {          worksheet4.cells[i, j] = dtag.rows[i - 1][j - 1].tostring();     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -