当我将网格视图导出到 Excel 时出现此消息错误我的数据库是 SQL Server 并且我的表有 8000 行当我只选择 1 行到 gridview 它导出到 excel 时没有错误但是当我选择所有行时出现这个问题这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.SqlClient.SqlDataReader reader = objCalep.SelectAll(true, false, "[تاريخ الاستشهاد]");
// System.Data.SqlClient.SqlDataReader reader = objCCP.CPSelectNo(2700);
DataTable dataTable = new DataTable();
dataTable.Load(reader);
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//System.Data.Common.DbDataRecord drv = e.Row.DataItem as System.Data.Common.DbDataRecord;
DataRowView dataRowView = e.Row.DataItem as DataRowView;
DataRow drv = dataRowView.Row;
Object ob = drv["تاريخ الاستشهاد"];
if (!Convert.IsDBNull(ob))
{
DateTime dateTime = (DateTime)ob;
e.Row.Cells[4].Text = dateTime.ToString("dd/MM/yyyy");
e.Row.Cells[4].Width = 100;
}
}
}
protected void btn_Add_Click(object sender, ImageClickEventArgs e)
{
PrepareGridViewForExport(GridView1);
ExportGridView();
}
private void ExportGridView()
{
string attachment = "attachment; filename=حلب.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
这是图像错误错误消息