IT Community - Software Programming, Web Development and Technical Support

Export table values into excel

This is a discussion on Export table values into excel within the C# Programming forums, part of the Software Development category; Hi, I want export the records to excel file from table. Can any one give me an idea?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-14-2008, 02:33 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Export table values into excel

Hi,

I want export the records to excel file from table. Can any one give me an idea?
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-14-2008, 02:37 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Export table values into excel

Hi,

you can do this using the following code.

string fileExcel, filePath, fileName, strLine, sql;
FileStream objFileStream;
StreamWriter objStreamWriter;
Random nRandom = new Random(DateTime.Now.Millisecond);
SqlConnection cnn = new SqlConnection("Data Source=.; Initial Catalog=Test; User ID=test1; Password=123456");

//Create a random file name.
fileExcel = "t" + nRandom.Next().ToString() + ".xls";

//Set a virtual folder to save the file.
//Make sure to change the application name to match your folder.
filePath = Server.MapPath("\\ExcelCSTest");
fileName = filePath + "\\" + fileExcel;

//Use FileSystem objects to create the .xls file.
objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream);

//Use a DataReader object to connect to the database.
cnn.Open();
sql = "select * from users";
SqlCommand cmd = new SqlCommand(sql, cnn);
SqlDataReader dr;
dr = cmd.ExecuteReader();

//Initialize the string that is used to build the file.
strLine = "";

//Enumerate the field names and the records that are used to build
//the file.
for (int i = 0; i <= dr.FieldCount - 1; i++)
{
strLine = strLine + dr.GetName(i).ToString() + Convert.ToChar(9);
}

//Write the field name information to the file.
objStreamWriter.WriteLine(strLine);

//Reinitialize the string for data.
strLine = "";

//Enumerate the database that is used to populate the file.
while (dr.Read())
{
for (int i = 0; i <= dr.FieldCount - 1; i++)
{
strLine = strLine + dr.GetValue(i).ToString()+ Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
}

//Clean up.
dr.Close();
cnn.Close();
objStreamWriter.Close();
objFileStream.Close();
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-18-2008, 02:13 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: Export table values into excel

string excelConnectionString = @"Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended
Properties=""Excel 8.0;HDR=YES;""";

using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select ID,Data FROM [Data$]", connection);

connection.Open();


using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=.;
Initial Catalog=Test;Integrated Security=True";

// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate row values in a table oxygen Database Support 1 02-08-2008 01:49 AM
What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server oxygen Database Support 6 11-23-2007 06:17 AM
Export to Excel option is not working in Crystal Report with service pack 2 tsureshk ASP and ASP.NET Programming 1 10-06-2007 03:57 AM
export java program result into excel chandana Java Server Pages (JSP) 1 09-14-2007 08:08 AM
When designing a database table, how do you avoid missing column values for non-prima Arun Database Support 2 07-18-2007 10:43 PM


All times are GMT -7. The time now is 01:54 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0