How do I export a Gridview Control to Excel in DotNetNuke?

冷暖自知 提交于 2020-01-06 15:01:31

问题


Is there an alternative to using this since using a <form runat="server"> will cause a 500 error.

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

EDITED

With the assistance from @HansDerks I ended up using the following(a jazzed up version of the solution provided.):

protected void Export_Click(object sender, System.EventArgs e)
    {
        StringWriter writer = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
        GridView gridView = new GridView();
        gridView.DataSource = MySqlDataSource;
        gridView.AutoGenerateColumns = true;
        gridView.DataBind();
        gridView.HeaderRow.Style.Add("background-color", "#003c74");
        gridView.HeaderRow.Style.Add("color", "#ffffff");
        for (int i = 0; i < gridView.Rows.Count; i++)
        {
            GridViewRow row = gridView.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.BackColor = System.Drawing.Color.AliceBlue;
            }
        }
        gridView.RenderControl(htmlWriter);
        htmlWriter.Close();

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=filename.xls");
        Response.Charset = "";
        Response.Write(writer.ToString());
        Response.End();
    }

I hope you guys will find it useful. Thanks everyone!


回答1:


If you have the datasource you can try this (replace datatable with relevant datasource)

private void DataTableToExcel(DataTable dataTable)
{
    StringWriter writer = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
    GridView gridView = new GridView();
    gridView.DataSource = dataTable;
    gridView.AutoGenerateColumns = true;
    gridView.DataBind();
    gridView.RenderControl(htmlWriter);
    htmlWriter.Close();

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
    Response.Charset = "";
    Response.Write(writer.ToString());
    Response.End();
}



回答2:


If you use the DNN grid control, which is based on a Telerik control, you can export the visible contents of the grid with:

protected void OnExportCSVClick(Object sender, EventArgs e)
{
    grdMemberTypes.MasterTableView.ExportToCSV();
}

This applies to DNN 7.0 though.

Unfortunately, if you have paging turned on, it only outputs the data from the visible rows.



来源:https://stackoverflow.com/questions/18766914/how-do-i-export-a-gridview-control-to-excel-in-dotnetnuke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!