将Repeater中的数据导出到Excel中

解决方案 »

  1.   

    与datagrid和gridview一样的,通过rendercontrol导出.
      

  2.   

    把GridView换成repeater就可以. public static void ExportExcel(GridView rp, string strFileName)
    {
     
    strFileName = System.Web.HttpUtility.UrlEncode(strFileName,System.Text.Encoding.UTF8);

     
     
    System.Web.HttpContext.Current.Response.Clear(); 
    System.Web.HttpContext.Current.Response.Buffer = true; 
    System.Web.HttpContext.Current.Response.Charset = "gb2312";
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
     
    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN",true);
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
     
    rp.RenderControl(oHtmlTextWriter); 
     
    rp = null;
    System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace("<td","<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
    System.Web.HttpContext.Current.Response.Buffer = false; 
    System.Web.HttpContext.Current.Response.End();
    }
      

  3.   

     protected void btn_InExecl_Click(object sender, EventArgs e)
        {
                       string str = "";
                DataTable dt = this.dp.RunSqlTextReturnDataSet(" select * from tb").Tables[0];
                this.rp_InExecl.DataSource = dt;
                this.rp_InExecl.DataBind();
                Guid guid = Guid.NewGuid();
                base.Response.Clear();
                base.Response.AddHeader("content-disposition", "attachment;filename=" + guid.ToString() + ".xls");
                Response.Charset = "GB2312";
                Response.ContentEncoding = Encoding.UTF8;
                base.Response.ContentType = "application/vnd.xls";
                StringWriter writer = new StringWriter();
                HtmlTextWriter writer2 = new HtmlTextWriter(writer);
                this.rp_InExecl.RenderControl(writer2);
                base.Response.Write(writer.ToString());
                base.Response.End();
        }
      

  4.   


    string FileName="C:\\Documents and Settings\\Administrator\\桌面\\公司" + System.DateTime.Now.Year.ToString() + "年全年销售计划一览表.xls";
            System.Data.DataTable dt = DAL.Am_SalesTargetDAL.SelAllToExcel();
            FileStream objFileStream;
            StreamWriter objStreamWriter;
            string strLine="";
            objFileStream = new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);
            objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Unicode);        for(int i=0;i<dt.Columns.Count;i++)
            {
                strLine=strLine+dt.Columns[i].ColumnName.ToString()+Convert.ToChar(9);
            }        objStreamWriter.WriteLine(strLine);
            strLine="";        for(int i=0;i<dt.Rows.Count;i++)
            {
                //strLine = strLine + dt.Rows[i].ToString() + Convert.ToChar(9);
                for(int j=0;j<dt.Columns.Count;j++)
                {
                    strLine=strLine+dt.Rows[i][j].ToString()+Convert.ToChar(9);
                }
                objStreamWriter.WriteLine(strLine);
                strLine="";
            }        objStreamWriter.Close();
            objFileStream.Close();