• .net导出到Excel与Word中(带上下标)


    //输出到excel的函数,可直接copy到 cs页面
        private void OutExcel(GridView dg, string name)
        
    {
            dg.Visible 
    = true;
            Response.Clear();
            Response.Buffer 
    = true;
            Response.Charset 
    = "GB2312";
            name 
    = "attachment;filename=" + name;
            Response.AppendHeader(
    "Content-Disposition", name);
            Response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType 
    = "application/ms-excel";
            dg.EnableViewState 
    = false;
            System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
            dg.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

        
    //输出到word的函数,可直接copy到 cs页面
        private void OutWord(GridView dg, string name)
        
    {
            dg.Visible 
    = true;
            Response.Clear();
            Response.Buffer 
    = true;
            Response.Charset 
    = "GB2312";
            name 
    = "attachment;filename=" + name;
            Response.AppendHeader(
    "Content-Disposition", name);
            Response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType 
    = "application/ms-word";
            dg.EnableViewState 
    = false;
            System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
            dg.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

        
    //重载VerifyRenderingInServerForm方法,调用页面必须加入否则会提示错误
       public override void VerifyRenderingInServerForm(Control control)
        

        }

        
    //调用方法 OutWord(Student, "File name.doc");
        protected void Button1_Click(object sender, EventArgs e)
        
    {       
            OutWord(Student, 
    "File name.doc");

        }

        
    //调用方法 OutExcel(Student, "File name.xls"); 
        protected void Button2_Click(object sender, EventArgs e)
        
    {
            OutExcel(Student, 
    "File name.xls");        
        }

    原网址:http://hi.baidu.com/sharp528108/blog/item/03217012279e3c52f819b884.html
    为了避免标头出现乱码
    用下边代码对标题进行编码
    System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)+".xls";

  • 相关阅读:
    记一次oracle新建用户及分配指定表权限的操作记录
    [转]word中不显示mathtype公式,只显示方框,双击后可以再mathtype里面看到公式
    C、C++成员变量对齐
    include头文件:C++标准库与C标准库
    [转]本专业部分国际会议及刊物影响因子排名
    使用Winbase.h
    [转]printf 字符串格式化
    1.6.2如何使用位逻辑运算(例如与、或、位移)来实现位向量?
    文章中图表自动编号
    取样问题 总数n事先不知道,等概率取样 (编程珠玑chapter12 课后题10)
  • 原文地址:https://www.cnblogs.com/sunheyubo/p/1124903.html
Copyright © 2020-2023  润新知