Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Member
176 Points
580 Posts
May 01, 2012 12:56 PM|LINK
Hello there. First of all I must say that I am a newbie when it comes to net. Here is my problem.
I need call the
protected void ExportToExcel(object sender, EventArgs e)
in the
protected void Page_Load(object sender, EventArgs e)
for your execution.
If you have link for similar task, please give it me.
Your help would be very appreciated.thanks for your time and hints.
using System; using System.IO; using System.Data; using System.Data.Odbc; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _inc_excel : System.Web.UI.Page { private DataTable GetData(OdbcCommand cmd) { DataTable dt = new DataTable(); OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString); System.Data.Odbc.OdbcDataAdapter sda = new OdbcDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = myConnectionString; try { myConnectionString.Open(); sda.SelectCommand = cmd; sda.Fill(dt); return dt; } catch (Exception ex) { throw ex; } finally { myConnectionString.Close(); sda.Dispose(); myConnectionString.Dispose(); } } protected void Page_Load(object sender, EventArgs e) { ExportToExcel(object sender, EventArgs e); } protected void ExportToExcel(object sender, EventArgs e) { string strQuery = "select CustomerID, ContactName, City, PostalCode" + " from customers"; OdbcCommand cmd = new OdbcCommand(strQuery); DataTable dt = GetData(cmd); GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); for (int i = 0; i < GridView1.Rows.Count; i++) { GridView1.Rows[i].Attributes.Add("class", "textmode"); } GridView1.RenderControl(hw); string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } }
cms9651
Member
176 Points
580 Posts
How to call different protected void in the Page_Load ?
May 01, 2012 12:56 PM|LINK
Hello there.
First of all I must say that I am a newbie when it comes to net.
Here is my problem.
I need call the
in the
for your execution.
If you have link for similar task, please give it me.
Your help would be very appreciated.
thanks for your time and hints.
using System; using System.IO; using System.Data; using System.Data.Odbc; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _inc_excel : System.Web.UI.Page { private DataTable GetData(OdbcCommand cmd) { DataTable dt = new DataTable(); OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString); System.Data.Odbc.OdbcDataAdapter sda = new OdbcDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = myConnectionString; try { myConnectionString.Open(); sda.SelectCommand = cmd; sda.Fill(dt); return dt; } catch (Exception ex) { throw ex; } finally { myConnectionString.Close(); sda.Dispose(); myConnectionString.Dispose(); } } protected void Page_Load(object sender, EventArgs e) { ExportToExcel(object sender, EventArgs e); } protected void ExportToExcel(object sender, EventArgs e) { string strQuery = "select CustomerID, ContactName, City, PostalCode" + " from customers"; OdbcCommand cmd = new OdbcCommand(strQuery); DataTable dt = GetData(cmd); GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); for (int i = 0; i < GridView1.Rows.Count; i++) { GridView1.Rows[i].Attributes.Add("class", "textmode"); } GridView1.RenderControl(hw); string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } }