• asp.net数据导出到Excel (从网络摘操)


    一、无格式的导出
         就是导出时没有任何格式,完全对文件写入,这个比较难看,但是实用。

     /// <summary>
        
    /// 该方法实现将数据导入到Excel文件中,其中的DataTable dt就是你需要将数据写入到Excel中的数据;
        
    /// </summary>
        
    /// <param name="dt">导出的数据源</param>
        
    /// <param name="w">文件流</param>

        public void ExportExcel(DataTable dt, System.IO.StreamWriter w)
        
    {
            
    //HttpResponse resp;
            
    //resp = Page.Response;
            
    //resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            
    //resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            string colHeaders = "", ls_item = "";

            
    //定义表对象与行对象,同时用DataSet对其值进行初始化          
            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() + "\n";
                }

                
    else
                
    {
                    colHeaders 
    += dt.Columns[i].Caption.ToString() + "\t";
                }


            }

            
    // resp.Write(colHeaders);
            w.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() + "\n";
                    }

                    
    else
                    
    {
                        ls_item 
    += row[i].ToString() + "\t";
                    }


                }

                w.Write(ls_item);
                ls_item 
    = "";

            }

            
    //resp.End();
            w.Flush();
            w.Close();
        }

    二、直接从Gridview里导出
        这种导出是所见既所得的方式,就是显示在gridview里是什么样导出就是什么样了,这个导出就好看多了,谢谢~!@
     this.Panel1.Visible = false;           
                Response.Clear();
                Response.Buffer 
    = true;
                Response.Charset 
    = "GB2312";
                
    string filename = "Task"+System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString()+".xls";
                Response.AppendHeader(
    "Content-Disposition""attachment;filename="+System.Web.HttpUtility.UrlEncode(filename));
                
    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentType 
    = "application/ms-excel";//设置输出文件类型为excel文件。 
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
                
    this.GridView1.RenderControl(oHtmlTextWriter); //读取数据了
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();
                
    this.Panel1.Visible = true;     

    由于ReaderControl方法会重调客户端的一个方法验证,如果不重载的话会报错
     public override void VerifyRenderingInServerForm(Control control)
        
    {
            
    //base.VerifyRenderingInServerForm(control);   
        }
      
  • 相关阅读:
    【实验】利用系统自带脚本utlsampl.sql创建scott用户及样本数据
    有哪些优秀的沟通思路?
    srand()以及rand()函数用法
    微信公众号
    Sublime Text 3 全程详细图文原创教程(持续更新中。。。)
    Android应用的缓冲界面启动界面
    ListView技巧
    android线性布局参数
    CocoaPods的一波三则
    003.开发者账号异同
  • 原文地址:https://www.cnblogs.com/ringwang/p/1033429.html
Copyright © 2020-2023  润新知