2个回答
展开全部
首先呢 你得把你的数据绑定到一个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
/// <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
展开全部
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);
}
{
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询