简体   繁体   English

将大数据导出到Excel

[英]Export Large Data to Excel

In my gridview i have 30000 records,while i export to excel, it export upto near 12000 records only,bellow my code to export to excel. 在我的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();

Here how can i export all(30k) gridview records to excel? 在这里,我如何将所有(30k)gridview记录导出到excel?

Here is a sample code to save your grid data to an excel file 这是将您的网格数据保存到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();
        }
    }

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

To export data to Excel, you can use the ClosedXML.Report library ( https://github.com/ClosedXML/ClosedXML.Report ). 要将数据导出到Excel,可以使用ClosedXML.Report库( https://github.com/ClosedXML/ClosedXML.Report )。 Believe me, this is a wonderful library and easy for her to use. 相信我,这是一个很棒的图书馆,她很容易使用。 The library does not need Excel Interop. 该库不需要Excel Interop。 ClosedXML.Report generates an Excel file based on a template that you can create in Excel using any formatting. ClosedXML.Report基于您可以使用任何格式在Excel中创建的模板生成Excel文件。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM