我正在将gridview导出到excel中,但无法正常工作我为什么不这样做?我是否必须为此重写任何渲染功能? [英] i m exporting gridview into excel but its not working i dnt knw why?should i have to override any render function for this?
本文介绍了我正在将gridview导出到excel中,但无法正常工作我为什么不这样做?我是否必须为此重写任何渲染功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要将gridview导出到excel中,但无法正常工作,为什么?
通过使用以下代码->
i m exporting gridview into excel but its not working i dnt knw why?
By using the code that is->
try
{
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv_appt.AllowPaging = false;
// gv_Employes.DataBind();
//Change the Header Row back to white color
gv_appt.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gv_appt.HeaderRow.Cells.Count; i++)
{
gv_appt.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gv_appt.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gv_appt.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
foreach (GridViewRow row in gv_appt.Rows)
{
LinkButton appgenrt = (LinkButton)row.FindControl("lbtn_apptGenerate");
LinkButton email = (LinkButton)row.FindControl("lbtn_email");
LinkButton edit = (LinkButton)row.FindControl("btn_editappt");
LinkButton del = (LinkButton)row.FindControl(" btl_aapdlt");
appgenrt.Visible = false;
email.Visible = false;
edit.Visible = false;
del.Visible = false;
gv_appt.Columns[7].Visible = false;
gv_appt.Columns[8].Visible = false;
gv_appt.Columns[9].Visible = false;
gv_appt.Columns[10].Visible = false;
}
gv_appt.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch { }
推荐答案
如果出现错误:"Control" gv_appt类型"GridView"的必须放置在带有runat = server的表单标签中.",然后
您需要在代码中放置以下方法:
If you are getting error: "Control ''gv_appt'' of type ''GridView'' must be placed inside a form tag with runat=server.", then
you need to place the following method in your code behind:
public override void VerifyRenderingInServerForm(Control control)
{
//You may keep it empty. But needed.//
}
单击此处 [
Click here[^] for detail.
myDataGrid.Visible = true;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
myDataGrid.Visible = false;
将导出类库添加到您的项目中.
请参考以下链接:
用于将数据导出到CSV/Excel文件的C#类库 [
Add export class library to your project..
Refer the following link:
C# class library for exporting data to CSV/Excel file[^]
http://ramanisandeep.wordpress.com/2009/04/07/export-gridview-to-excel/ [ ^ ]
如何将GridView导出到Excel,渲染单元格,添加艺术字并填充自定义模板 [ ^ ]
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx [^ ]
^ ]
http://ramanisandeep.wordpress.com/2009/04/07/export-gridview-to-excel/[^]
How to export GridView to Excel, render cells, add WordArt, and fill a custom template[^]
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx[^]
http://blogs.msdn.com/b/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx[^]
这篇关于我正在将gridview导出到excel中,但无法正常工作我为什么不这样做?我是否必须为此重写任何渲染功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文