asp.net GridView导出到excel

我是新手需要详细操作... 我是新手 需要详细操作 展开
 我来答
解忧解忧杂货铺
2012-05-19 · TA获得超过590个赞
知道小有建树答主
回答量:1536
采纳率:33%
帮助的人:694万
展开全部
首先呢 你得把你的数据绑定到一个GridView中 然后 下面的方法 就是导出了。
/// <summary>
/// 将超链接、下拉框等样式去掉
/// </summary>
/// <param name="gv"></param>
private void DisableControls(Control gv)
{
Literal l = new Literal();

string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(HyperLink))
{
l.Text = (gv.Controls[i] as HyperLink).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}

if (gv.Controls[i].HasControls())
{
DisableControls(gv.Controls[i]);
}
}
}

/// <summary>
/// 将GridView中的内容导出成Excel文件
/// 如文件名为空,则使用规则生成文件默认
/// </summary>
/// <param name="FileName">扩展名必须为xls,而不能是xlsx,否则不能直接在Excel中打开</param>
public void ToExcel(string FileName)
{
if (string.IsNullOrEmpty(FileName))
{
string filename = DateTime.Now.Ticks.ToString() + ".xls";
FileName = this.Parent.Page.Server.MapPath("~/Temp/" + filename);
}

//去除分页、排序
this.AllowPaging = false;
this.AllowSorting = false;

this.AutoDataBind();

//生成html
this.DisableControls(this);
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
this.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.RenderControl(hw);
string str = tw.ToString();
str = (Regex.Replace(str, @"(<a(.|\s)*?>)|</a>", "", RegexOptions.IgnoreCase));
HttpContext.Current.Response.Write(str);
HttpContext.Current.Response.End();

//还原分页、排序
this.AllowPaging = true;
this.AllowSorting = true;

this.AutoDataBind();
}
#endregion
幸运的杨晨晨
2015-10-30 · TA获得超过2.2万个赞
知道小有建树答主
回答量:1000
采纳率:66%
帮助的人:271万
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
ExcelOut(this.GridView1);
}

public void ExcelOut(GridView gv)
{
if (gv.Rows.Count > 0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式