点击导出按钮我想导出aspx gridview?

时间:2018-03-28 09:45:38

标签: c# asp.net devexpress aspxgridview

我想在单击按钮时导出ASPxGridView作为导出。

我正在使用以下代码但未获得所需的结果:

protected void btn_excel_Click1(object sender, EventArgs e)
    {
        ASPxGridViewExporter1 . WriteXlsToResponse();
    }

3 个答案:

答案 0 :(得分:2)

试试这个

 using (var exporter = new NpoiExport())
    {

        DataTable dt = new DataTable();
        dt = (DataTable)Session["Excel"];
        exporter.ExportDataTableToWorkbook(dt, "Result");

        string saveAsFileName = string.Format("Results-{0:d}.xls", DateTime.Now);


        Response.ContentType = "application/excel";

        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));

        Response.Clear();

        Response.BinaryWrite(exporter.GetBytes());

        Response.End();
    }

答案 1 :(得分:0)

如果要将网格视图数据导出为ex​​cel,则使用数据表。将gridview数据绑定到数据表中,并将数据表数据导出到excel。

答案 2 :(得分:0)

导出到Excel使用此代码。

<asp:GridView ID="GrdAccess" runat="server" AutoGenerateColumns="false" Width="70%" AllowPaging="true" PageSize="20" CssClass="Grid" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" PagerStyle-CssClass="pgr" OnPageIndexChanging="GrdAccess_PageIndexChanging"></asp:GridView>

protected void ExportToExcel(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                GrdAccess.AllowPaging = false;
                this.getProjectDetails();

                GrdAccess.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in GrdAccess.HeaderRow.Cells)
                {
                    cell.BackColor = GrdAccess.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in GrdAccess.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = GrdAccess.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = GrdAccess.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                GrdAccess.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }


    public override void VerifyRenderingInServerForm(Control control)
        {

        }