使用文本框将错误导出到Excel Gridview列

时间:2011-03-08 09:09:57

标签: c# .net asp.net excel gridview

我有Gridview喜欢这张照片。很容易在我的最后一栏,我记下那一行。 (简单地说,我编辑,选择,更新gridview经典属性。)

enter image description here

如您所见,我的上一栏有一个TextboxMultiline

这是我的最后一列gridview代码;

<EditItemTemplate>
            <asp:TextBox ID="txtTNOT" runat="server" Height="35" TextMode="MultiLine" DataSourceID="SqlDataSource8"></asp:TextBox>
            <asp:SqlDataSource ID="SqlDataSource8" runat="server" 
              ConnectionString="<%$ ConnectionStrings:SqlServerCstr %>" 
              SelectCommand="SELECT [T_NOT] FROM [TAKIP] WHERE T_HESAP_NO = @T_HESAP_NO ">
                <SelectParameters>
                  <asp:Parameter Name="T_HESAP_NO" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            </EditItemTemplate>

然后我使用此代码将此Gridview导出到Excel;

protected void LinkButton1_Click(object sender, EventArgs e)
  {
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=TahTakip.xls");
    Response.Charset = "";


    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    GridView1.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
  }

此步骤没有问题。问题是当我打开这个excel文件时,NOT列与其他列不同。它有Textbox喜欢这张图片;

enter image description here

我不想成为Textbox文件中的Excel。只需要一个像左栏一样的NOT值的普通单元格。

我该怎么做?

Best Regars,

Soner

1 个答案:

答案 0 :(得分:1)

您需要导出数据表而不是GridView,因为您在gridview中有无法导出到Excel的控件

编辑:你可以这样做..

protected void btnExportExl_Click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    string attachment = "attachment; filename=BusinessUnit.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);

    GridView grd = new GridView();
    grd.DataSource = datatable.DefaultView; // you get the datatable from DB that will not have controls!!!
    grd.DataBind();

    grd.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

请注意,在实际网格中我有一些图像按钮,当我导出时,我再次从DB获取数据表