C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Export database to Excel With Formatting


Posted Date: 24 Jun 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following C# code is used to write the data from database to excel in formatted way.

I have used HTML Tags for the formatting.

We need to add the necessary HTML tags. The tags used here are Table, TR, Font and TH.

It can be customized according to the needs.



public void WriteDatabaseToExcel(string sFileName)

{

StreamWriter sw;
SqlDataReader dr;

SqlConnection conn;


try

{


//Sql Connection Estblishment

string ConnString = "Initial Catalog=dataPool;Data Source=localhost;Integrated Security=SSPI;Persist Security Info=False";

conn = new Sqlconnection(connString);

SqlCommand cmd = new SqlCommand("Select [First Name], [Last Name],Dob From Table1", conn);


//Open the Sql connection
Conn.Open();


//Read the data to the DataReader

SqlDataReader dr = new SqlDataReader();

dr = cmd.ExecuteReader();


//prepare Formatted HTML String to write to the Excel


StringBuilder sb = new StringBuilder();


//Making HTML

sb.Append("< meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\" >);


//Draw the table

sb.Append("< TABLE class=\"text\" Border=1 ID=\"Table1\" >");
sb.Append("< TR>< TD ColSpan=4 >< Font Size=5 > < /Font >< /TD >< TR >");
sb.Append("< TR >");


//Write the Header Column Details

for (int i = 0; i < dr.FieldCount; i++)
{

sb.Append("< TH >" + dr.GetName(i) + "< /TH >");

}

sb.Append("< /TR >");


//Write the content for each cell

while (dr.Read())

{

sb.Append(" < TR >");

for (int i = 0; i < dr.FieldCount; i++)
{

sb.Append("< TD >" + dr.GetValue(i).ToString() + "< /TD >");

}

sb.Append("< /TR >"); //New line

}

dr.Close();

sb.Append("< /TABLE >");



//WRITING the formatted string to the Excel File

sw = new StreamWriter(sFile);

sw.Write(sb.ToString());
sw.Close();



}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);


}

finally
{


//Clean up of resources

if !(dr = Nul)
dr.Close();

if (!(conn = Null) && (conn.State == ConnectionState.Open))
conn.close();

}

//Opening the Excel File


Process.Start(sFileName);


}
}




USAGE


WriteDatabaseToExcel ("C:\TestExcel.xls");


After writing the content, it will open the excel file.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export database to Excel With Formatting  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to parse an expression and check for parenthesis (round brackets)?
Previous Resource: String Format for DateTime
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use