导出带有模板的repeator

来源:互联网 发布:单片机节日彩灯设计 编辑:程序博客网 时间:2024/04/28 11:40


  #region 创建单元格
  private HtmlTableCell CreatCell(string innertext)
  {
   return this.CreatCell(1,innertext);
  }

  private HtmlTableCell CreatCell(int type,string innertext)
  {
   return this.CreatCell(type,0,0,innertext);
  }

  private HtmlTableCell CreatCell(int type,int colspan,string innertext)
  {
   return this.CreatCell(type,0,colspan,innertext);
  }

  private HtmlTableCell CreatCell(int type,int rowspan,int colspan,string innertext)
  {
   HtmlTableCell htc;
   htc=new HtmlTableCell();
   htc.ColSpan=colspan;
   htc.RowSpan=rowspan;
   htc.InnerText=innertext;
   if(type==0)//表头
   {
    
    htc.Style.Add("color","#CCCCFF");
    htc.Style.Add("FONT-WEIGHT","true");
    htc.BgColor="#003399";
   }
   return htc;
  }
  #endregion

  #region 获取表
  private HtmlTable GetHtmlTableFromRepeater(Repeater rp)
  {

   /*<tr style="FONT-WEIGHT: bold; COLOR: #ccccff; BACKGROUND-COLOR: #003399">
   <td rowspan="3">地区</td>
   <td rowspan="3">月份</td>
   <td rowspan="3">工号</td>
   <td rowspan="3">人员归属</td>
   <td rowspan="3">营销模式</td>
   <td rowspan="3">新增用户数</td>
   <td colspan="6">客户评测系统监控情况</td>
   <td colspan="2">其他方式监控情况</td>
   <td colspan="2">合计违规情况</td>
   <td colspan="12">扣罚结果</td>
  <tr style="FONT-WEIGHT: bold; COLOR: #ccccff; BACKGROUND-COLOR: #003399">
   <td rowspan="2">准确度80%以上单数</td>
   <td rowspan="2">比例</td>
   <td rowspan="2">准确度40%-80%单数</td>
   <td rowspan="2">比例</td>
   <td rowspan="2">合计监控单数</td>
   <td rowspan="2">比例</td>
   <td rowspan="2">违规单数</td>
   <td rowspan="2">比例</td>
   <td rowspan="2">合计违规单数</td>
   <td rowspan="2">比例</td>
   <td colspan="4">客户评测系统</td>
   <td colspan="4">其他监控模式</td>
   <td colspan="4">合计情况</td>
  </tr>
  <tr style="FONT-WEIGHT: bold; COLOR: #ccccff; BACKGROUND-COLOR: #003399">
   <td>涉及人数</td>
   <td>辞退人数</td>
   <td>取消佣金(提成)单数</td>
   <td>扣罚总金额</td>
   <td>涉及人数</td>
   <td>辞退人数</td>
   <td>取消佣金(提成)单数</td>
   <td>扣罚总金额</td>
   <td>涉及人数</td>
   <td>辞退人数</td>
   <td>取消佣金(提成)单数</td>
   <td>扣罚总金额</td>
  </tr>*/
   System.Web.UI.HtmlControls.HtmlTable htable=new HtmlTable();
   htable.Border=1;
   htable.BorderColor="#3366CC";
   htable.CellPadding=1;
   htable.CellSpacing=0;
   HtmlTableRow htr;
   htr=new HtmlTableRow();
   htr.Align="center";
   htr.Cells.Add(this.CreatCell(0,3,0,"地区"));
   htr.Cells.Add(this.CreatCell(0,3,0,"月份"));
   htr.Cells.Add(this.CreatCell(0,3,0,"工号"));
   htr.Cells.Add(this.CreatCell(0,3,0,"人员归属"));
   htr.Cells.Add(this.CreatCell(0,3,0,"营销模式"));
   htr.Cells.Add(this.CreatCell(0,3,0,"新增用户数"));
   htr.Cells.Add(this.CreatCell(0,6,"客户评测系统监控情况"));
   htr.Cells.Add(this.CreatCell(0,2,"其他方式监控情况"));
   htr.Cells.Add(this.CreatCell(0,2,"合计违规情况"));
   htr.Cells.Add(this.CreatCell(0,12,"扣罚结果"));
   
            htable.Rows.Add(htr);
   htr=new HtmlTableRow();
   htr.Align="center";
   htr.Cells.Add(this.CreatCell(0,2,0,"准确度80%以上单数"));
   htr.Cells.Add(this.CreatCell(0,2,0,"比例"));
   htr.Cells.Add(this.CreatCell(0,2,0,"准确度40%-80%单数"));
   htr.Cells.Add(this.CreatCell(0,2,0,"比例"));
   htr.Cells.Add(this.CreatCell(0,2,0,"合计监控单数"));
   htr.Cells.Add(this.CreatCell(0,2,0,"比例"));
   htr.Cells.Add(this.CreatCell(0,2,0,"违规单数"));
   htr.Cells.Add(this.CreatCell(0,2,0,"比例"));
   htr.Cells.Add(this.CreatCell(0,2,0,"合计违规单数"));
   htr.Cells.Add(this.CreatCell(0,2,0,"比例"));
   htr.Cells.Add(this.CreatCell(0,4,"客户评测系统"));
   htr.Cells.Add(this.CreatCell(0,4,"其他监控模式"));
   htr.Cells.Add(this.CreatCell(0,4,"合计情况"));

   htable.Rows.Add(htr);
   htr=new HtmlTableRow();
   htr.Align="center";

   htr.Cells.Add(this.CreatCell(0,"涉及人数"));
   htr.Cells.Add(this.CreatCell(0,"辞退人数"));
   htr.Cells.Add(this.CreatCell(0,"取消佣金(提成)单数"));
   htr.Cells.Add(this.CreatCell(0,"扣罚总金额"));
   htr.Cells.Add(this.CreatCell(0,"涉及人数"));
   htr.Cells.Add(this.CreatCell(0,"辞退人数"));
   htr.Cells.Add(this.CreatCell(0,"取消佣金(提成)单数"));
   htr.Cells.Add(this.CreatCell(0,"扣罚总金额"));
   htr.Cells.Add(this.CreatCell(0,"涉及人数"));
   htr.Cells.Add(this.CreatCell(0,"辞退人数"));
   htr.Cells.Add(this.CreatCell(0,"取消佣金(提成)单数"));
   htr.Cells.Add(this.CreatCell(0,"扣罚总金额"));
   htable.Rows.Add(htr);
   
   System.Web.UI.WebControls.Label lbTemp=new Label();
   LinkButton lbtnTemp=new LinkButton();
   foreach(RepeaterItem item in rp.Items)
   {
    htr=new HtmlTableRow();
    htr.Align="center";
    foreach(Control ctr in item.Controls)
    {
     
     if(ctr is Label)
     {
      lbTemp=(System.Web.UI.WebControls.Label)ctr;
      htr.Cells.Add(this.CreatCell(lbTemp.Text));

     }
     else if(ctr is LinkButton)
     {
      //string name=ctr.GetType().ToString();
      lbtnTemp=(LinkButton)ctr;
      htr.Cells.Add(this.CreatCell(lbtnTemp.Text));
     }
    }
    htable.Rows.Add(htr);
   }
   return htable;
  }
  #endregion

#region 从HtmlTable导出
  public static void ExpReport(string reportname,System.Web.UI.HtmlControls.HtmlTable ht,Encoding content,Encoding filename)
  {
   
   System.Web.HttpContext.Current.Response.Clear();
   System.Web.HttpContext.Current.Response.Buffer=true;//缓冲输出
   System.Web.HttpContext.Current.Response.ContentType="application/vnd.ms_excel";//输出流的HTTP MIME类型
   
   System.Web.HttpContext.Current.Response.ContentEncoding=content;//输出流的HTTP字符集
   System.IO.StringWriter oStringWriter=new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter oHtmlTextWriter=new HtmlTextWriter(oStringWriter);//在WEB窗体写出一系列连续的HTML特定字符和文本
   
   ht.RenderControl(oHtmlTextWriter);//将DataGridPMG控件的内容输出到oHtmlTextWriter对象中
   
   System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(reportname,filename)+".xls"); //将HTTP头添加到输出流
   
   System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());//向HTTP输出内容流写入一个字符数组
   System.Web.HttpContext.Current.Response.Flush();//向客户端发送当前所有缓冲的输出
   System.Web.HttpContext.Current.Response.End();//将当前所有缓冲的输出发送到客户端,并停止该页的执行
  }
  #endregion