日期:2014-05-17  浏览次数:20506 次

gridview导入到Excel 导出后只有gridview以外的数据,刚好没有gridview中的数据
本帖最后由 edchen 于 2013-01-17 16:55:47 编辑
gridview导入到Excel 导出后只有gridview以外的数据,刚好没有gridview中的数据

//cs 

public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void btnExcelError_Click(object sender, EventArgs e)
    {
        GridViewToExcel(RecordToolbar1);
    }

    public void GridViewToExcel(GridView _GridView)
    {
        Response.Clear();
        Response.BufferOutput = true;
        //设定输出的字符集
        Response.Charset = "GB2312";
        //假定导出的文件名为MyExcelFile.xls
        Response.AppendHeader("Content-Disposition", "attachment;filename=MyExcelFile.xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
        System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
        _GridView.RenderControl(textWriter);
        //把HTML写回浏览器
        Response.Write(stringWriter.ToString());
        Response.End();   
    }


//前台

                            
<asp:GridView ID="RecordToolbar1" runat="server" AutoGenerateColumns="False" Width="98%"
                                Height="60px" text-align="center" CssClass="grid" EnableViewState="False">
                                <HeaderStyle CssClass="gridHeader" />
                                <PagerSettings Visible="false" />
                                <RowStyle CssClass="gridItem" />
                                <Columns>
   &nbs