【问题标题】:Export Large Data to Excel将大数据导出到 Excel
【发布时间】:2016-06-30 14:54:56
【问题描述】:

在我的 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?

【问题讨论】:

    标签: c# asp.net excel gridview


    【解决方案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();
            }
        }
    

    【讨论】:

    • 您好-seif-sammain,当我尝试执行您的代码时,我收到“System.OutOfMemoryException”。错误
    【解决方案2】:

    我建议你看看 OpenXmlWriter 类,用于将大量数据导出到 excel(这应该可以防止缓存问题)。

    【讨论】:

      【解决方案3】:

      要将数据导出到 Excel,您可以使用 ClosedXML.Report 库 (https://github.com/ClosedXML/ClosedXML.Report)。相信我,这是一个很棒的图书馆,而且对她来说很容易使用。该库不需要 Excel 互操作。 ClosedXML.Report 根据您可以使用任何格式在 Excel 中创建的模板生成 Excel 文件。例如:

          var template = new XLTemplate(@".\Templates\report.xlsx");
      
          using (var db = new DbDemos())
          {
              var cust = db.customers.LoadWith(c => c.Orders).First();
              template.AddVariable(cust);
              template.Generate();
          }
      
          template.SaveAs(outputFile);
      

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 1970-01-01
        • 2013-10-06
        • 2016-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多