Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The database named db have three tables called A,B,C,How to acheive this effect:in a dropdownlist user select one table(A,B,C from DB) and click a button ,than export the table which format is excel?(no showing the table in the web page,just select the table,click the button and that export the table)

I can operate it manually by set the excel's datasource,but I don't know how to acheive it by coding?
Posted
Updated 7-Jul-12 19:36pm
v2
Comments
DaveAuld 8-Jul-12 1:31am    
You do not tell us where you are selecting the table names from, is it a web app, from within excel userforms, or some other input medium. Please edit your question with more info, and set the tags as well. Also sort the subject to a meaningful one and not just a repeat of the question, e.g. Select and export SQL table to Excel file from Web page.
PEIYANGXINQU 8-Jul-12 1:37am    
OK,I have edited again,Thank you!

hi,
take a look at this post:
Export Data to Excel, Word, PDF without Automation from DataBase[^]

Hope it will help,
Cheers
 
Share this answer
 
Comments
PEIYANGXINQU 8-Jul-12 3:20am    
Thank you,I find a bug in the software,when export time ,it seems wrong
Hi,

Check following link :
Exporting Data to Excel[^]
hope it will help you:)
 
Share this answer
 
C#
SqlDataAdapter da = new SqlDataAdapter("select * from Sample", con); // SqlDataAdapter da = new SqlDataAdapter( "select * from"+"'Dropdownlist1.selecteditem.text'"+"" ", con);
    DataSet ds = new DataSet();
    da.Fill(ds);

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

   System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


   dgrid.DataSource = ds;


   htmlWrite.WriteLine("hello");

   dgrid.DataBind();//Data Grid Bind

   dgrid.RenderControl(htmlWrite);

   Response.Clear();
    string str="Student";
    Response.AddHeader("content-disposition", "attachment;filename='"+str+"'"+".xls");

    Response.Charset = "";

   Response.Cache.SetCacheability(HttpCacheability.NoCache);

   Response.ContentType = "application/vnd.xls";

   Response.Write(stringWrite.ToString());
   Response.End();

Hope it will helpful for you
 
Share this answer
 
v2

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