0

有没有使用它的替代方法,因为使用 a<form runat="server">会导致 500 错误。

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

已编辑

在@HansDerks 的帮助下,我最终使用了以下内容(提供的解决方案的爵士版本。):

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();
    }

我希望你们会发现它有用。谢谢大家!

4

2 回答 2

2

如果你有数据源,你可以试试这个(用相关的数据源替换数据表)

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();
}
于 2013-09-13T07:39:23.297 回答
0

如果您使用基于 Telerik 控件的 DNN 网格控件,您可以使用以下命令导出网格的可见内容:

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

这适用于 DNN 7.0。

不幸的是,如果您打开了分页,它只会输出可见行中的数据。

于 2013-09-13T12:17:06.383 回答