asp.net repeater绑定数据,然后导出Excel表格,求高手给个实例 5
3个回答
展开全部
repeater绑定和gridview绑定一样
repeater.datasource=dt;
repeater.databind();
事件调用:toExecl(repeater);
调用1:protected void toExecl(System.Web.UI.WebControls.GridView gv)
{
OutExcel.Export(gv, "application/ms-excel", "FileName.xls");
}
调用2:
public static void Export(System.Web.UI.WebControls.repeater ctrl, string FileType, string FileName)
{
DisableControls(ctrl);
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF7).ToString());
HttpContext.Current.Response.ContentType = FileType; //image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
for (int i = 0; i < ctrl.Columns.Count; i++)
{
if (ctrl.HeaderRow.Cells[i].Controls.Count>0)
{
LinkButton linkButton = ((LinkButton)(ctrl.HeaderRow.Cells[i].Controls[0]));//获取标题行第i个单元格的<a>标签(LinkButton)
string value = linkButton.Text;//保存<a>标签的文本
ctrl.HeaderRow.Cells[i].Controls.Clear();//把标题行第i个单元格的所有控件删除(把<a>标签删除)
ctrl.HeaderRow.Cells[i].Text = value;//把<a>标签的文本赋给标题行第i个单元格的文本
linkButton.Dispose();
}
ctrl.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
具体你在看看。不难。
repeater.datasource=dt;
repeater.databind();
事件调用:toExecl(repeater);
调用1:protected void toExecl(System.Web.UI.WebControls.GridView gv)
{
OutExcel.Export(gv, "application/ms-excel", "FileName.xls");
}
调用2:
public static void Export(System.Web.UI.WebControls.repeater ctrl, string FileType, string FileName)
{
DisableControls(ctrl);
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF7).ToString());
HttpContext.Current.Response.ContentType = FileType; //image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
for (int i = 0; i < ctrl.Columns.Count; i++)
{
if (ctrl.HeaderRow.Cells[i].Controls.Count>0)
{
LinkButton linkButton = ((LinkButton)(ctrl.HeaderRow.Cells[i].Controls[0]));//获取标题行第i个单元格的<a>标签(LinkButton)
string value = linkButton.Text;//保存<a>标签的文本
ctrl.HeaderRow.Cells[i].Controls.Clear();//把标题行第i个单元格的所有控件删除(把<a>标签删除)
ctrl.HeaderRow.Cells[i].Text = value;//把<a>标签的文本赋给标题行第i个单元格的文本
linkButton.Dispose();
}
ctrl.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
具体你在看看。不难。
展开全部
public void ToExcel(System.Web.UI.Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}//导入excel
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}//导入excel
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-01-14
展开全部
不
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询