Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
i want to convert from sql table to excelusing sql query...i used the below query..its run without any errors.But its not generating the excel file.

SQL
DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp select * from TESTCONTROL where COUNTERID =1 out D:\Downloads\testing.xls -U sa -P sql123 -c'
Exec xp_cmdshell @cmd

EXEC sp_configure 'xp_cmdshell', 1
Posted

Hi,

You can do this operation with SSIS (SQL Server Integration Services) package, which automate the whole Export to Excel task.

Then deploy that package using SQL Server Agent Jobs. This is a more neat and clean solution as I found.

Regards,
Bhagyesh
 
Share this answer
 
Comments
deenasudhakar 21-Apr-14 9:57am    
Hi Bhagyesh,
Can You pls explain this. I am new to this

Thanks,
Sudhakar
Bh@gyesh 22-Apr-14 0:18am    
Hi Sudhakar,
Please refer following link to create SSIS packages.
http://www.codeproject.com/Articles/402958/SSIS-Package-Export-Data-from-Database-Daily-to-Ne
http://www.youtube.com/watch?v=CJI6pPueyzM

Thanks,
Bhagyesh
Ref this code,
private void GetData()
{
DataTable dtTable = new DataTable();
SqlConnection conn = new SqlConnection(connString);
conn.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("Your select query", conn);
SqlDataAdapter daTable = new SqlDataAdapter(cmd);
daTable.Fill(dt);
string attachment = "attachment; filename=flename.xls";
// Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
 
Share this answer
 

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