I am using C#NET2008 and Microsoft Excel 2003.
I encounter an interesting problem. I am trying to retrieve the Field Name from DataReader to insert onto Excel Spreadsheet Row onto each column as Header.
Also having problem trying to aligned the Column as Center.So that all text are Center alignment.
Sample coding that is not working;
xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["OrderID"].Name);
Here are the overall coding.
Expand|Select|Wrap|Line Numbers
- sqlconn = new SqlConnection(connstr);
- sqlconn.Open();
- sqlcmd = new SqlCommand(strSql, sqlconn);
- DR = sqlcmd.ExecuteReader()
- if (DR.HasRows == false)
- {
- MessageBox.Show("Customer Does not have Invoices");
- } //endif
- else
- {
- xlExcel.Visible = true;
- Excel.Range xlRange;
- Excel.Range xlwrkSheet_Range;
- //create column header
- intRow = 5;
- int icol;
- int iRecCnt = DR.FieldCount;
- for (icol = 1; icol < iRecCnt; icol++)
- {
- switch (icol)
- {
- case 1:
- xlWrkSheet.Cells[intRow, icol] = Convert.ToString (DR["OrderID"].Name);
- xlWrkSheet.Columns.ColumnWidth=10;
- break;
- case 2:
- xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["OrderDate"].Name);
- xlWrkSheet.Columns.ColumnWidth = 12;
- break;
- case 3:
- xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["RequireDate"].name);
- xlWrkSheet.Columns.ColumnWidth = 12;
- break;
- case 4:
- xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["ShipDate"].Name);
- xlWrkSheet.Columns.ColumnWidth = 12;
- break;
- }
- }