Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have 4 data tables in database .

when i type username in textbox and button click on getdetails.
it should get that particular user details in exile file .
each table data must retrieve in new sheets.

am retrieving all data in excel from database.

can anyone help me.

Thanks & Regards
sam.198979
Posted
Comments
Sandeep Mewara 13-Jun-13 9:50am    
What help? What have you tried so far?
sam.198979 13-Jun-13 10:20am    
SqlConnection conn;
string connectionstring = null;
string sql = null;
string data = null;
int i = 0;
int j = 0;

Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel._Worksheet xlWorkSheet1;
Microsoft.Office.Interop.Excel._Worksheet xlWorkSheet2;
Microsoft.Office.Interop.Excel._Worksheet xlWorkSheet3;
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Range chartRange;


xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);


connectionstring = Connection.ConnectionString;
conn = new SqlConnection(connectionstring);
conn.Open();

sql = "SELECT * FROM SystemInfo";
//sql ="SELECT*From Table_1";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, conn);
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].Columns[j].ColumnName.ToString();

data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 3, j + 1] = data;
//xlWorkSheet1.Cells[i + 3, j + 1] = data;
}
}

sql = "SELECT*From ServiceInfo";
SqlDataAdapter dscmd1 = new SqlDataAdapter(sql, conn);
DataSet ds1 = new DataSet();
dscmd1.Fill(ds1);

for (i = 0; i <= ds1.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds1.Tables[0].Columns.Count - 1; j++)
{
//data = ds.Tables[0].Columns[j].ColumnName.ToString();

data = ds1.Tables[0].Rows[i].ItemArray[j].ToString();
//xlWorkSheet.Cells[i + 3, j + 1] = data;
xlWorkSheet1.Cells[i + 3, j + 1] = data;
}
}

sql = "SELECT*From Driveinfo";
SqlDataAdapter dscmd2 = new SqlDataAdapter(sql, conn);
DataSet ds2 = new DataSet();
dscmd2.Fill(ds2);

for (i = 0; i <= ds2.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds2.Tables[0].Columns.Count - 1; j++)
{
//data = ds.Tables[0].Columns[j].ColumnName.ToString();

data = ds2.Tables[0].Rows[i].ItemArray[j].ToString();
//xlWorkSheet.Cells[i + 3, j + 1] = data;
xlWorkSheet2.Cells[i + 3, j + 1] = data;
}
}

sql = "SELECT*From NetworkInfo";
SqlDataAdapter dscmd3 = new SqlDataAdapter(sql, conn);
DataSet ds3 = new DataSet();
dscmd3.Fill(ds2);

for (i = 0; i <= ds3.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds3.Tables[0].Columns.Count - 1; j++)
{
//data = ds.Tables[0].Columns[j].ColumnName.ToString();

data = ds3.Tables[0].Rows[i].ItemArray[j].ToString();
//xlWorkSheet.Cells[i + 3, j + 1] = data;
xlWorkSheet3.Cel
sam.198979 13-Jun-13 10:21am    
it's to long code
Ahmed Bensaid 13-Jun-13 10:31am    
Revise your instantiations (like I said in your previous post) :
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900