gridview自建表头绑定数据,网页上浏览OK。导出到EXCEL的时候表头那里就出问题了。
贴出问题部分的代码,大家帮我看下。以下是建表头的的部分代码
 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        string date1 = TextBox1.Text;
        string date2 = TextBox2.Text;
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:                //总表头
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();
                //第一行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Attributes.Add("bgcolor", "white");
                tcHeader[0].Attributes.Add("BorderColor", "#333300");
                tcHeader[0].Attributes.Add("BorderWidth", "1px");
                tcHeader[0].Attributes.Add("colspan", "10");  //合并第一行的10列
                tcHeader[0].Text = "BRT一号线站台运量收入统计</th></tr><tr>";
                //第二行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Attributes.Add("bgcolor", "white");
                tcHeader[1].Attributes.Add("colspan", "10");  //合并第二行的10列
                tcHeader[1].Text = TextBox1.Text + "~" + TextBox2.Text + "</th></tr><tr>";
                ......
   }
}
以下是导出文件的代码
  private void Export1(GridView gvUser, string Style, string FileType, string FileName)
    {
        Response.ClearContent();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        //page.Response.ContentType = "application/ms-excel";
        Page.EnableViewState = false;
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //turn off paging
        gvUser.RenderControl(hw);
        Response.Write(Style);
        Response.Write(sw.ToString());
        //page.Response.Flush();
        Response.End();
    }
如果把红色字体那段换成tcHeader[1].Text = “2019-07-01” + "~" + “2019-07-07” + "</th></tr><tr>";就没问题。表头的内容只能写死才能导出吗?这种动态绑定的该怎么导出?大神们优先就我用的建表头的方法和导出方法看该怎么改代码
        

解决方案 »

  1.   

    看图啊,导出的时候日期导不出来,程序上没问题TextBox1.Text=“2019-07-01”
    TextBox2.Text=“2019-07-07”
    tcHeader[1].Text = “2019-07-01” + "~" + “2019-07-07” + "</th></tr><tr>";
    tcHeader[1].Text = TextBox1.Text + "~" + TextBox2.Text + "</th></tr><tr>";
    这2个表头创建的语句在导出的时候有什么区别
      

  2.   

    调试一下看看TextBox1.Text 有没值啊?
      

  3.   

    有值啊,第一张图就是在网页上调试的时候查询出来的结果,一切正常
    下面一张图就是导出来的EXCEL文件,第二行的日期就不见了
      

  4.   

    改一下:
    string myText =TextBox1.Text + "~" + TextBox2.Text + "</th></tr><tr>";
    tcHeader[1].Text =myText  ;
    这里打个断点。
    tcHeader[1].Text =myText  ;
    看一下myText  的值是什么。
    不回打断点?请按F9
      

  5.   

    myText "2019-07-26~2019-07-30</th></tr><tr>" string
    问题的症结在于string myText =TextBox1.Text + "~" + TextBox2.Text + "</th></tr><tr>";取控件值导出的时候就导不出TextBox1控件的值,
    写死tcHeader[1].Text = “2019-07-01” + "~" + “2019-07-07” + "</th></tr><tr>";导出就没问题
    在网页上显示,包括断点查询2种方法tcHeader[1].Text的值都是对应的日期段。是不是我的导出语句读取不到TextBox控件的值?
      

  6.   

    你的TextBox1不是服务器控件吧?换成服务器控件
      

  7.   

    TEXTBOX还能不是服务器控件?<asp:TextBox ID="TextBox1" runat="server"  onclick="WdatePicker()"></asp:TextBox>