• 导出文件


    //导出文件

    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文件中。

  • 相关阅读:
    代理模式
    工厂模式
    Mysql索引数据结构为什么是B+树?
    mxgraph中mxStencil使用经验
    !dbobji.cpp@8615
    ForkJoinPool线程池
    保障线程安全的设计技术
    Java利用线程工厂监控线程池
    使用Arthas分析线上问题
    使用规则执行器代替 if else 判断
  • 原文地址:https://www.cnblogs.com/jameshappy/p/1368054.html
Copyright © 2020-2023  润新知