Export to Excel

B

ben

Hi,
I have an aspx page with an export to excel functionality....it opens
a dialog box asking the client to save/open the xls file.However this
works fine on my machine but does not work on another PC and
onclicking the export button( where all the code is written)....it
does nothing.....i am pasting the code below...any info will be useful
Also, one of the columns in the datagrid is 16 charasters long(it is
numberic data but stored as varchar in DB); it gets converted to
numeric form in the spreadsheet..how do i make it as text....
Thanks in advance for any inputs

private void Page_Load(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer=true;
Response.ContentType="application/vnd.ms-excel";

//Response.AddHeader("content-disposition","attachment;filename=myfile.xls";
Response.AddHeader("content-disposition","attachment;filename=myfile"+"_"+DateTime.Now.Day.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Year.ToString()+"_"+DateTime.Now.Hour.ToString()+"_"+DateTime.Now.Minute.ToString()+"_"+DateTime.Now.Second.ToString()+".xls");
Response.Charset="";
this.EnableViewState=false;


System.IO.StringWriter SW= new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw= new HtmlTextWriter(SW);


string connstr="";
string selecttext="";
SqlConnection myconnection=new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(selecttext,myconnection);
myconnection.Open();
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand=cmd;
DataTable myds= new DataTable();
da.Fill(myds);
DataGrid dg= new DataGrid();
dg.DataSource= myds;
dg.DataBind();
//this.ClearControls(dg);
dg.RenderControl(hw);

Response.Write(SW.ToString());
Response.End();
// Put user code to initialize the page here
}


This is the code in the aspx page which will open on clicking the
export to excel button on the main aspx page.....however nothign will
get displayed in this and the dialog box to save/open will directly
pop up....

P.S Checked the folder options to be accurate(for XLS format)

Thanks again

Ben Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

export to excel 1
Export To Excel 1
Export to Excel shows an empty sheet 1
Export Datagrids to Excel 1
Export Grid View to Excel 1
DataGrid export to excel 2
export dataset to excel 5
export to excel 0

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top