zoukankan      html  css  js  c++  java
  • 笔记0624

    1,现在,GridView中已经绑定了数据,接下来的任务就是导出到Excel。下面是button事件中的代码

    Response.ClearContent();
    
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    
    Response.ContentType = "application/excel";
    
    StringWriter sw = new StringWriter();
    
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    
    gvUsers.RenderControl(htw);
    
    Response.Write(sw.ToString());
    
    Response.End();

    并且还需要override一下VerifyRenderingInServerForm方法(这一点非常重要,否则在点击按钮后会报错,译者注)代码如下:

    public override void VerifyRenderingInServerForm(Control control)
    
    {
    
    }

     2.1

     public void ToExcel()//整个GRIDVIEW导出到EXCEL
        {
            string filename="网银终端" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            string style = @"<style> .text { mso-number-format:@; } </script> "; //解决第一位字符为零时不显示的问题
           
            this.GridView1.AllowPaging = false;
            this.GridView1.DataBind();
    
            filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);//解决导出EXCEL时乱码的问题
            Response.ClearContent;
           
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/excel";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
    
            System.IO.StringWriter sw = new System.IO.StringWriter();//定义一个字符串写入对象
            HtmlTextWriter htw = new HtmlTextWriter(sw);//将html写到服务器控件输出流
           
            this.GridView1.RenderControl(htw);//将控件GRIDVIEW中的内容输出到HTW中
            Response.Write(style);
            Response.Write(sw);
            Response.End();
    
            this.GridView1.AllowPaging = true;
        }

    2.2

    public void CreateExcel(DataSet ds, string FileName)//整个GRIDVIEW导出到EXCEL.xls
        {
            FileName=HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);//解决导出时文件名汉字显示乱码的问题
    
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            string colHeaders = "", ls_item = "";
    
            //定义表对象与行对象,同时用DataSet对其值进行初始化
            DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;
    
            //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列,加n
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "
    ";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "	";
                }
    
            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息
    
            //逐行处理数据  
            foreach (DataRow row in myRow)
            {
                //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列,加n
                    {
                        ls_item += row[i].ToString() + "
    ";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "	";
                    }
    
                }
                resp.Write(ls_item);
                ls_item = "";
    
            }
            resp.End();
        }

     2.3

    GridView导入excel中代码很简单的如

     protected void ExportExcel(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            this.GridData.AllowPaging = false;
            BindData();//数据绑定
            this.GridData.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
    
         this.GridData.AllowPaging = true;
    
    BindData();//数据绑定
    
    }

     //

    2.4Response中的Clear(),ClearContent(),ClearHeaders() 方法

    Response.Clear()方法 Clear方法删除所有缓存中的HTML输出。但此方法只删除Response显示输入信息,不删除Response头信息。 Response.ClearContent()方法 ClearContent与Clear方法区别就是ClearContent方法不仅删除Response显示输出信息而且还删除Response头信息

    Response.ClearHeaders()方法 ClearHeaders方法只删除头信息,而不删除Response显示输出信息

    2.5 Response.AddHeader

    Response.AddHeader使用实例
    1.文件下载,指定默认名
    Response.AddHeader("content-type","application/x-msdownload");
    Response.AddHeader("Content-Disposition","attachment;filename=文件名.rar");
    2.刷新页面
    Response.AddHeader (“REFRESH”, ”60;URL=newpath/newpage.asp”)
    这等同于客户机端<META>元素:
    <META HTTP-EQUIV=”REFRESH”, “60;URL=newpath/newpage.asp”>
    3.页面转向
    Response.Status = “302 Object Moved”
    Response.Addheader “Location”, “newpath/newpage.asp”
    这等同于使用Response.Redirect方法:
    Response.Redirect “newpath/newpage.asp”
    4.强制浏览器显示一个用户名/口令对话框
    Response.Status= “401 Unauthorized”
    Response.Addheader “WWW-Authenticate”, “BASIC”
    强制浏览器显示一个用户名/口令对话框,然后使用BASIC验证把它们发送回服务器(将在本书后续部分看到验证方法)。
    5.如何让网页不缓冲
    Response.Expires = 0
    Response.ExpiresAbsolute = Now() - 1
    Response.Addheader "pragma","no-cache"
    Response.Addheader "cache-control","private"
    Response.CacheControl = "no-cache
     
    不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.
    代码如:
    <% response.ContentType ="text/html" %> 
    <!--#i nclude virtual="/ContentType.html" -->

    显示的为网页,而
    <% response.ContentType ="text/plain" %> 
    <!--#i nclude virtual="/sscript/ContentType.html" -->

    则会显示html原代码.

    以下为一些常用的 ContentType
    GIF images 
    <% response.ContentType ="image/gif" %> 
    <!--#i nclude virtual="/myimage.gif" -->

    JPEG images 
    <% response.ContentType ="image/jpeg" %> 
    <!--#i nclude virtual="/myimage.jpeg" -->

    TIFF images 
    <% response.ContentType ="image/tiff" %> 
    <!--#i nclude virtual="/myimage.tiff" -->

    MICROSOFT WORD document 
    <% response.ContentType ="application/msword" %> 
    <!--#i nclude virtual="/myfile.doc" -->

    RTF document 
    <% response.ContentType ="application/rtf" %> 
    <!--#i nclude virtual="/myfile.rtf" -->

    MICROSOFT EXCEL document 
    <% response.ContentType ="application/x-excel" %> 
    <!--#i nclude virtual="/myfile.xls" -->

    MICROSOFT POWERPOINT document 
    <% response.ContentType ="application/ms-powerpoint" %> 
    <!--#i nclude virtual="/myfile.pff" -->

    PDF document 
    <% response.ContentType ="application/pdf" %> 
    <!--#i nclude virtual="/myfile.pdf" -->

    ZIP document 
    <% response.ContentType ="application/zip" %> 
    <!--#i nclude virtual="/myfile.zip" -->



    下面是更详细的ContentType
    'ez' => 'application/andrew-inset', 
    'hqx' => 'application/mac-binhex40', 
    'cpt' => 'application/mac-compactpro', 
    'doc' => 'application/msword', 
    'bin' => 'application/octet-stream', 
    'dms' => 'application/octet-stream', 
    'lha' => 'application/octet-stream', 
    'lzh' => 'application/octet-stream', 
    'exe' => 'application/octet-stream', 
    'class' => 'application/octet-stream', 
    'so' => 'application/octet-stream', 
    'dll' => 'application/octet-stream', 
    'oda' => 'application/oda', 
    'pdf' => 'application/pdf', 
    'ai' => 'application/postscript', 
    'eps' => 'application/postscript', 
    'ps' => 'application/postscript', 
    'smi' => 'application/smil', 
    'smil' => 'application/smil', 
    'mif' => 'application/vnd.mif', 
    'xls' => 'application/vnd.ms-excel', 
    'ppt' => 'application/vnd.ms-powerpoint', 
    'wbxml' => 'application/vnd.wap.wbxml', 
    'wmlc' => 'application/vnd.wap.wmlc', 
    'wmlsc' => 'application/vnd.wap.wmlscriptc', 
    'bcpio' => 'application/x-bcpio', 
    'vcd' => 'application/x-cdlink', 
    'pgn' => 'application/x-chess-pgn', 
    'cpio' => 'application/x-cpio', 
    'csh' => 'application/x-csh', 
    'dcr' => 'application/x-director', 
    'dir' => 'application/x-director', 
    'dxr' => 'application/x-director', 
    'dvi' => 'application/x-dvi', 
    'spl' => 'application/x-futuresplash', 
    'gtar' => 'application/x-gtar', 
    'hdf' => 'application/x-hdf', 
    'js' => 'application/x-javascript', 
    'skp' => 'application/x-koan', 
    'skd' => 'application/x-koan', 
    'skt' => 'application/x-koan', 
    'skm' => 'application/x-koan', 
    'latex' => 'application/x-latex', 
    'nc' => 'application/x-netcdf', 
    'cdf' => 'application/x-netcdf', 
    'sh' => 'application/x-sh', 
    'shar' => 'application/x-shar', 
    'swf' => 'application/x-shockwave-flash', 
    'sit' => 'application/x-stuffit', 
    'sv4cpio' => 'application/x-sv4cpio', 
    'sv4crc' => 'application/x-sv4crc', 
    'tar' => 'application/x-tar', 
    'tcl' => 'application/x-tcl', 
    'tex' => 'application/x-tex', 
    'texinfo' => 'application/x-texinfo', 
    'texi' => 'application/x-texinfo', 
    't' => 'application/x-troff', 
    'tr' => 'application/x-troff', 
    'roff' => 'application/x-troff', 
    'man' => 'application/x-troff-man', 
    'me' => 'application/x-troff-me', 
    'ms' => 'application/x-troff-ms', 
    'ustar' => 'application/x-ustar', 
    'src' => 'application/x-wais-source', 
    'xhtml' => 'application/xhtml+xml', 
    'xht' => 'application/xhtml+xml', 
    'zip' => 'application/zip', 
    'au' => 'audio/basic', 
    'snd' => 'audio/basic', 
    'mid' => 'audio/midi', 
    'midi' => 'audio/midi', 
    'kar' => 'audio/midi', 
    'mpga' => 'audio/mpeg', 
    'mp2' => 'audio/mpeg', 
    'mp3' => 'audio/mpeg', 
    'aif' => 'audio/x-aiff', 
    'aiff' => 'audio/x-aiff', 
    'aifc' => 'audio/x-aiff', 
    'm3u' => 'audio/x-mpegurl', 
    'ram' => 'audio/x-pn-realaudio', 
    'rm' => 'audio/x-pn-realaudio', 
    'rpm' => 'audio/x-pn-realaudio-plugin', 
    'ra' => 'audio/x-realaudio', 
    'wav' => 'audio/x-wav', 
    'pdb' => 'chemical/x-pdb', 
    'xyz' => 'chemical/x-xyz', 
    'bmp' => 'image/bmp', 
    'gif' => 'image/gif', 
    'ief' => 'image/ief', 
    'jpeg' => 'image/jpeg', 
    'jpg' => 'image/jpeg', 
    'jpe' => 'image/jpeg', 
    'png' => 'image/png', 
    'tiff' => 'image/tiff', 
    'tif' => 'image/tiff', 
    'djvu' => 'image/vnd.djvu', 
    'djv' => 'image/vnd.djvu', 
    'wbmp' => 'image/vnd.wap.wbmp', 
    'ras' => 'image/x-cmu-raster', 
    'pnm' => 'image/x-portable-anymap', 
    'pbm' => 'image/x-portable-bitmap', 
    'pgm' => 'image/x-portable-graymap', 
    'ppm' => 'image/x-portable-pixmap', 
    'rgb' => 'image/x-rgb', 
    'xbm' => 'image/x-xbitmap', 
    'xpm' => 'image/x-xpixmap', 
    'xwd' => 'image/x-xwindowdump', 
    'igs' => 'model/iges', 
    'iges' => 'model/iges', 
    'msh' => 'model/mesh', 
    'mesh' => 'model/mesh', 
    'silo' => 'model/mesh', 
    'wrl' => 'model/vrml', 
    'vrml' => 'model/vrml', 
    'css' => 'text/css', 
    'html' => 'text/html', 
    'htm' => 'text/html', 
    'asc' => 'text/plain', 
    'txt' => 'text/plain', 
    'rtx' => 'text/richtext', 
    'rtf' => 'text/rtf', 
    'sgml' => 'text/sgml', 
    'sgm' => 'text/sgml', 
    'tsv' => 'text/tab-separated-values', 
    'wml' => 'text/vnd.wap.wml', 
    'wmls' => 'text/vnd.wap.wmlscript', 
    'etx' => 'text/x-setext', 
    'xsl' => 'text/xml', 
    'xml' => 'text/xml', 
    'mpeg' => 'video/mpeg', 
    'mpg' => 'video/mpeg', 
    'mpe' => 'video/mpeg', 
    'qt' => 'video/quicktime', 
    'mov' => 'video/quicktime', 
    'mxu' => 'video/vnd.mpegurl', 
    'avi' => 'video/x-msvideo', 
    'movie' => 'video/x-sgi-movie', 
    'ice' => 'x-conference/x-cooltalk' 

    //---------------------下面是从资源文件获取图片。并显示
     public class GetImage
        {
            public static System.Drawing.Image GetSrc(string name)
            {
                System.Resources.ResourceManager rm = new ResourceManager("ClassLibrary.ResourceTestImg",
           System.Reflection.Assembly.GetExecutingAssembly());//ResourceTestImg为资源文件名称,ClassLibrary//为命名空间
                return rm.GetObject(name) as System.Drawing.Image;
            }
           
        }

    显示代码
     protected void Page_Load(object sender, EventArgs e)
     {

                System.Drawing.Image img = ClassLibrary.GetImage.GetSrc("_15958260");
               //_15958260为在ResourceTestImg.resx资源管理窗口中看到资源图片名称
                         
                img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }

  • 相关阅读:
    Maven
    Mybatis
    WinDbg的安装、配置和功能(转发)
    gRPC —— gRPC 基础: C#(待续)
    gRPC —— 通讯协议
    gRPC —— 安全认证
    gRPC —— 概念
    gRPC —— 概览
    grpc和protocol buffer介绍&实例(转载)
    protocol buffers ——git 源码
  • 原文地址:https://www.cnblogs.com/jonson1126/p/3152142.html
Copyright © 2011-2022 走看看