| | |
transfer contends from database table to excel
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 172
Reputation:
Solved Threads: 16
If you try to export data from sql server to excel then use this stored procedure as bellow
Cretae an excel file in C: and name it Test.xls
CREATE proc EXP_FROM_SQLServer_TO_Excel
(
@File_Name as varchar(100) = ' ', - -The file content that would be exported from sql server
@Query AS VARCHAR(100) - -What you want exactly put in the excel sheet
)
as
BEGIN
--Provider
DECLARE @Provider VARCHAR(100)
–Excel Connection string
DECLARE @ECS VARCHAR(100)
--Command
DECLARE @Cmd VARCHAR(100)
–file name
DECLARE @FN VARCHAR(100)
Select @FN = 'C:\' + @File_Name + '.xls'
SELECT @Cmd = 'Copy C:\Test.xls ' + @ FN
EXEC MASTER..XP_CMDSHELL @Cmd, NO_OUTPUT
SET @Provider = 'Microsoft.Jet.OLEDB.4.0'
SET @ESC = 'Excel 8.0;Database=' + @FN
EXEC('insert into OPENrowset(''' + @provider + ''',''' + @ExcelString + ''',''SELECT Field1, …….., Fieldn FROM [Sheet2$]'') –replace the Sheet2$ by your sheet name
select Field1, …….., Fieldn from YourDataTable')
SET NOCOUNT OFF
END
Then you can call this stored procedure from SQL Server or even from C# code as follow
SqlConnection oConnection = new SqlConnection("ConnectionString");
SqlCommand oCommand = new SqlCommand();
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "EXP_FROM_SQLServer_TO_Excel";
SqlParameter FileName = new SqlParameter("File_Name", yourInputValue);
SqlParameter Query = new SqlParameter("Query", yourinputvalue2);
oCommand.Parameters.Add(FileName);
oCommand.Parameters.Add(Query);
oCommand.ExecuteNonQuery();
I tried it and It works for sure
Cretae an excel file in C: and name it Test.xls
CREATE proc EXP_FROM_SQLServer_TO_Excel
(
@File_Name as varchar(100) = ' ', - -The file content that would be exported from sql server
@Query AS VARCHAR(100) - -What you want exactly put in the excel sheet
)
as
BEGIN
--Provider
DECLARE @Provider VARCHAR(100)
–Excel Connection string
DECLARE @ECS VARCHAR(100)
--Command
DECLARE @Cmd VARCHAR(100)
–file name
DECLARE @FN VARCHAR(100)
Select @FN = 'C:\' + @File_Name + '.xls'
SELECT @Cmd = 'Copy C:\Test.xls ' + @ FN
EXEC MASTER..XP_CMDSHELL @Cmd, NO_OUTPUT
SET @Provider = 'Microsoft.Jet.OLEDB.4.0'
SET @ESC = 'Excel 8.0;Database=' + @FN
EXEC('insert into OPENrowset(''' + @provider + ''',''' + @ExcelString + ''',''SELECT Field1, …….., Fieldn FROM [Sheet2$]'') –replace the Sheet2$ by your sheet name
select Field1, …….., Fieldn from YourDataTable')
SET NOCOUNT OFF
END
Then you can call this stored procedure from SQL Server or even from C# code as follow
SqlConnection oConnection = new SqlConnection("ConnectionString");
SqlCommand oCommand = new SqlCommand();
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "EXP_FROM_SQLServer_TO_Excel";
SqlParameter FileName = new SqlParameter("File_Name", yourInputValue);
SqlParameter Query = new SqlParameter("Query", yourinputvalue2);
oCommand.Parameters.Add(FileName);
oCommand.Parameters.Add(Query);
oCommand.ExecuteNonQuery();
I tried it and It works for sure
•
•
Join Date: Dec 2007
Posts: 205
Reputation:
Solved Threads: 11
can you be more specific and write the code instead of suggesting what is wrong in my code.it will be more help if u would code the line for saving the file.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
I cant express how angry you make me.
If you are incapable of changing the value of a variable or setting a variable to a value and your home work is to do th is.. You are on the wrong course.
If you are incapable of changing the value of a variable or setting a variable to a value and your home work is to do th is.. You are on the wrong course.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Other Threads in the C# Forum
- Previous Thread: converting VB to C sharp for Quality Center
- Next Thread: Loading Database data into Labels for viewing?
| Thread Tools | Search this Thread |
.net access activedirectory ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener listview mandelbrot math mouseclick mysql networking operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics storing stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualbasic visualstudio webbrowser windows winforms wpf xml






