Export Gridview Data to excel

Posted by kbm5evm-16703 under ASP.NET on 2/12/2013 | Points: 10 | Views : 3138 | Status : [Member] | Replies : 3
Hi...

I want to export data from grid view to excel. i completed the basic thing i.e, from labels in gridview i exported to excel. but i need to export dropdownlistvalues also. is it possible to get dropdownlist to excel by coding.


Thanks in advance....




Responses

Posted by: Kusumas on: 2/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Follow below mentioned link

http://www.codeproject.com/Tips/477436/Export-Gridview-Data-to-Excel-in-ASP-NET

If it will use mark as resolved...

kbm5evm-16703, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 2/13/2013 [Member] Starter | Points: 25

Up
0
Down
hiii
better to try this


using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
string strcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
}
public void getdata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from mytab", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvdetails.DataSource = ds;
gvdetails.DataBind();

}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "myapp.xlsx"));
Response.ContentType = "application/ms-excel";
//StringWriter sw = new StringWriter();
//HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
//gvdetails.DataBind();
getdata();
Response.End();
}
}
if have any doubts with this post send me aquery
Thanks & Regards
Aswini Aluri

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

kbm5evm-16703, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 2/13/2013 [Member] Starter | Points: 25

Up
0
Down
protected void excel_Click_btn(object sender, EventArgs e)
{
string Str = "write sql query here "(the query which retrieves data to gridview)
SqlConnection con = con_manager.getcon();
SqlCommand cmd = new SqlCommand(Str, con);
SqlDataAdapter sda = new SqlDataAdapter(Str, con);
sda.Fill(dt);
GenerateTableData(dt);
string temp3 = string.Format(" Report Created Successfully..!");
writeToLogFile_status(temp3);

}
public void GenerateTableData(DataTable dtdata)
{
string name = "CompleteReport" + System.DateTime.Now.ToString("ddMMMMyyyy") + ".xls";
string attach = "attachment;filename=" + name;
Response.ClearContent();
Response.AddHeader("content-disposition", attach);
Response.ContentType = "application/ms-excel";
if (dtdata != null)
{
foreach (DataColumn dc in dtdata.Columns)
{
Response.Write(dc.ColumnName + "\t");
//sep = ";";
}
Response.Write(System.Environment.NewLine);
foreach (DataRow dr in dtdata.Rows)
{
for (int i = 0; i < dtdata.Columns.Count; i++)
{
Response.Write(dr[i].ToString() + "\t");
}
Response.Write("\n");
}
Response.End();
}


}

kbm5evm-16703, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response