是否可以使用此替代方法,因为使用<form runat="server">
会导致500错误。
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
的 EDITED 的
在@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();
}
我希望你们会觉得它很有用。谢谢大家!
答案 0 :(得分:2)
如果你有数据源,你可以尝试这个(用相关的数据源替换datatable)
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();
}
答案 1 :(得分:0)
如果您使用基于Telerik控件的DNN网格控件,则可以使用以下命令导出网格的可见内容:
protected void OnExportCSVClick(Object sender, EventArgs e)
{
grdMemberTypes.MasterTableView.ExportToCSV();
}
这适用于DNN 7.0。
不幸的是,如果您打开了分页,它只会输出可见行中的数据。