Deepak Roy
Posts: 1
Nickname: roy007
Registered: Jan, 2006
|
|
Re: Work with Excel from C#
|
Posted: Jan 17, 2006 1:28 AM
|
|
Pass Your Page, Dataset and the Gridview to this function ----------------------------------------------------------- public void ExportToExcel(Page pg,DataSet DsModule,GridView Gv) { string FileN; FileN = "report1.xls"; // Set the content type to Excel pg.Response.ClearContent(); pg.Response.ClearHeaders(); pg.EnableViewState = false; pg.Response.Clear(); pg.Response.ContentType = "application/vnd.ms-excel"; pg.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileN); pg.EnableViewState = false; //'Remove the charset from the Content-Type header pg.Response.Charset = ""; System.IO.StringWriter myTextWriter = new System.IO.StringWriter(); HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myTextWriter); //'Get the HTML for the control DataGrid dg = new DataGrid(); DataView dv = new DataView(DsModule.Tables[0]); int i = 0; foreach (DataColumn dc in DsModule.Tables[0].Columns) { dv.Table.Columns.ColumnName = Gv.Columns.ToString(); i++; } dg.DataSource = dv; dg.HeaderStyle.Font.Bold = true; dg.DataBind(); dg.RenderControl(myHtmlTextWriter); //'Write the HTML to the browser pg.Response.Write(myTextWriter.ToString()); //'End the response pg.Response.End(); }
|
|