导出错误到Excel Gridview列与文本框 [英] Export Error to Excel Gridview Column with Textbox

查看:224
本文介绍了导出错误到Excel Gridview列与文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Gridview 像这张照片。在我的最后一列很容易,我留下一行的笔记。 (简单地说,我做了编辑,选择,更新该gridview经典属性。)

I have a Gridview like this picture. Easyly in my last column, i keep a note for that row. (Simply, i made it edit, select, update that gridview classic properties.)

如您所见,我的最后一列有一个 Textbox Multiline

As you can see, my last column has a Textbox (Multiline)

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

Here is my last column gridview code;

<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 导出为ex​​cel使用此代码;

And then i export this Gridview to excel with this code;

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 像这张照片;

There is no problem since this step. The problem is when i open this excel file, NOT column is different the other columns. It has a Textbox like this picture;

我不想成为文本框在我的 Excel 文件中。只需要一个具有NOT值的正常单元格,例如左列。

I don't want to be a Textbox in my Excel file. Just want a normal cell with a NOT value like left column.

我该怎么做?

Regars,

Soner

推荐答案

你需要导出datatable而不是你的GridView,因为您在gridview中的控件无法导出到excel

you need to export datatable instead your GridView as you have controls in gridview that can't be export to 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

please note, in the actual grid I have some image button and when I am exporting then I get the datatable again from DB

这篇关于导出错误到Excel Gridview列与文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆