unable to get table column names from database in excel file.

Posted by Sam.198979 under C# on 5/24/2013 | Points: 10 | Views : 1322 | Status : [Member] | Replies : 0
Hi
With this code am able to get data from database to excel file.
But am not getting column names.
Can u help me.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn;
string connectionstring = null;
string sql = null;
string data = null;
int i = 0;
int j = 0;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;


xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

connectionstring ="Data Source=IN-45810;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
cnn = new SqlConnection(connectionstring);
cnn.Open();
sql = "SELECT * FROM Emp";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);

for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}

xlApp.Visible = true;
}
Thanks Regrades
sam.198979

sam.198979


Responses

(No response found.)

Login to post response