Click here to Skip to main content
11,478,174 members (75,517 online)
Click here to Skip to main content

export sql server or other database query in to excel or word by using c# programming

, 8 Aug 2007 19.8K 411 21
Rate this:
Please Sign up or sign in to vote.
export sql server or other database query in to excel or word by using c# programming

Introduction

hi. i need a procedure for export a large data reported from sql server in excel document. by using my method we can do it. this method is vary high speed.

Background

this code is use full for database query (large data). i assume that that data is text base and do not contain any image and binary data. if other, use must have some modification in this code.

Using the code

my idea is based on office 2003 feature for supporting a HTML data. you can make a HTML data and open with every office application. for generate excel document use can use a TABLE tag. each table cell is excel cell's too.

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

base of HTML generation is following code.

while (dr.Read())
    {
       sbrHTML.Append("<TR>");
       for (int i = 0; i < dr.FieldCount; i++)
       {
          sbrHTML.Append("<TD>" + good_value(dr.GetValue(i).ToString()) + "</TD>");
       }
       sbrHTML.Append("</TR>");
    } 

after generation save a document by .xls .

the excel document was made (is it a simple?).

now in your application use following code...

 clsExcel clsexcel = new clsExcel("sql text", "report header");
 clsexcel.save();

note that. if your language is right to left. you must use this code before you HTML

اگر متن فارسي داريد بايد از اين كد استفاده نماييد

sbrHTML.Append("<meta http-equiv=\"Content-Type\" 
        content=\"text/html;charset=UTF-8\">
    <style>.text { mso-number-format:\\@; } </style><TABLE 
        class=\"text\" Border=1 ID=\"Table1\">");
 

if you can't run this code. mail to me.

thanks.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

jalal khodabandeh
Web Developer
Greece Greece
No Biography provided

Comments and Discussions

 
GeneralReinvent the wheel Pin
Mark Nischalke9-Aug-07 4:04
mvpMark Nischalke9-Aug-07 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 9 Aug 2007
Article Copyright 2007 by jalal khodabandeh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid