• 导出文件


    //导出文件

    public static void ToDocument( DataSet ds , string type , string filename )
      {
       DataRow[] drArray = ds.Tables[0].Select("") ;
       string strHeader = "" ;
       //根据DataSet得到要导出的内容
       for( int i = 0 ; i < ds.Tables[0].Columns.Count ; i++ )
       {
        if( i == ds.Tables[0].Columns.Count -1 )
        {
         strHeader += ds.Tables[0].Columns[i].Caption + "\n" ;
        }
        else
        {
         strHeader += ds.Tables[0].Columns[i].Caption + "\t" ;
        }
       }

       string strData = "" ;
       //逐行获取数据
       foreach( DataRow dr in drArray )
       {
        for( int i = 0 ; i < ds.Tables[0].Columns.Count ; i++ )
        {
         if( i == ds.Tables[0].Columns.Count -1 )
         {
          strData += dr[i].ToString() + "\n" ;
         }
         else
         {
          strData += dr[i].ToString() + "\t" ;
         }
        }
       }

       ToDocument(strHeader+strData,type,filename) ;
      }


     

    public static void ToDocument(System.Web.UI.Control body,string type,string filename)
      {
       string fileType = "application/ms-excel";  //文件类型

       //转换用户输入的type,全部为大写防止选择失败
       type = type.ToUpper().Trim();

       switch(type)
       {
        case "EXCEL":
        {
         filename+=".xls";
         fileType = "application/ms-excel";
         break;
        }
        case "WORD":
        {
         filename+=".doc";
         fileType = "application/ms-word";
         break;
        }
        case "JPEG":
        {
         filename+=".jpeg";
         fileType = "image/JPEG";
         break;
        }
        case "GIF":
        {
         filename+=".gif";
         fileType = "image/GIF";
         break;
        }
        case "HTML":
        {
         filename+=".html";
         fileType = "text/HTML";
         break;
        }
        case "CSV":
        {
         filename+=".csv";
         fileType = "application/octet-stream";
         break;
        }
        case "TXT":
        {
         filename+=".txt";
         fileType = "text/plain";
         break;
        }
       }
       //清除Response缓存内容
       HttpContext.Current.Response.Clear();
       HttpContext.Current.Response.Buffer= true;

       //确定字符的编码格式
       HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(filename));
       HttpContext.Current.Response.ContentType = fileType;
       HttpContext.Current.Response.Charset ="GB2312";
       HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");

       try
       {
        body.Page.EnableViewState = false;
       }
       catch
       {}

       //表体空间
       System.IO.StringWriter swBody = new System.IO.StringWriter() ;
       System.Web.UI.HtmlTextWriter hwBody = new System.Web.UI.HtmlTextWriter (swBody);
       body.RenderControl(hwBody);

       //消除乱码特别设定,非常规方法
       string strExcel = "" ;
       strExcel = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";
       strExcel += hwBody.InnerWriter.ToString();

       HttpContext.Current.Response.Write(strExcel);
       HttpContext.Current.Response.End();
      }

    调用方法:Common.ToDocument(ds,"excel","进出库流水信息") ;

    ToDocument方法在Common文件中。

  • 相关阅读:
    mmap和MappedByteBuffer
    Linux命令之TOP
    Linux命令之ss
    MySql Cluster
    HttpClient配置
    注释驱动的 Spring cache 缓存介绍
    Spring AOP 实现原理与 CGLIB 应用
    AspectJ本质剖析
    B树
    imagick-3.1.0RC2 安装错误
  • 原文地址:https://www.cnblogs.com/jameshappy/p/1368054.html
Copyright © 2020-2023  润新知