在我的gridview中我有30000条记录,当我导出到excel时,它只导出近12000条记录,而不是我的代码导出到excel。
GridView1.AllowPaging = false;
DataTable dt = (DataTable)Session["tabledata"];
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "Red");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "Red");
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
这里如何将所有(30k)gridview记录导出为excel?
答案 0 :(得分:1)
以下是将网格数据保存到Excel文件的示例代码
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
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.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();
}
}
答案 1 :(得分:1)
我建议您查看OpenXmlWriter类,以便将大量数据导出到excel(这应该可以防止缓存问题)。
答案 2 :(得分:0)
要将数据导出到Excel,您可以使用ClosedXML.Report库(https://github.com/ClosedXML/ClosedXML.Report)。相信我,这是一个很棒的图书馆,很容易让她使用。该库不需要Excel Interop。 ClosedXML.Report基于您可以使用任何格式在Excel中创建的模板生成Excel文件。例如:
scanf_s("%d", &zahlen[i]);