导出Excel--处理标题

来源:互联网 发布:isp 网络拓扑 编辑:程序博客网 时间:2024/05/22 14:34
if (this.rptList.Items.Count < 1)
            {
                EventMessage.MessageView(1, Icon_Type.Error, "对不起,你要导出的列表里没有数据,请重新操作!", "javascript:history.back();");
            }
            //定义文档类型、字符编码
            string title = "学生欠费信息表" + "_" + DateTime.Today.ToShortDateString();
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "gb2312";
            string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8) + ".xls";
            Response.AppendHeader("Content-Disposition", filename);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
            Response.ContentType = "application/vnd.ms-excel";
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");

            this.EnableViewState = true;
            // 定义一个输入流
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            oStringWriter.Write("<table><tr align='center' style='border:1px #000 solid;'><td width='80'>记录编号</td><td width='80'>用户姓名</td><td width='80'>缴费学生姓名</td><td width='80'>缴费年度</td><td width='80'>年度应缴金额</td><td width='80'>实缴金额</td><td width='80'>欠费金额</td><td width='150'>记录时间</td></tr>");
            this.rptList.RenderControl(oHtmlTextWriter);
            oStringWriter.Write("</table>");

            //this.rptList.RenderControl(oHtmlTextWriter);
            //this 表示输出本页,或其他支持obj.RenderControl()属性的控件
            Response.Write(oStringWriter.ToString());
            Response.End();
原创粉丝点击