将大数据导出到Excel
在我的GridView中,我有30000条logging,而我出口到Excel,它只出口到近12000条logging,以下我的代码导出为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)gridviewloggingexcel?
以下是将您的网格数据保存到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(); } }
我build议你看看OpenXmlWriter类来导出大量的数据到Excel(这应该防止caching问题)。
- 从ExcelSheetparsingDateTime
- 导出到Excel – 在Windows Server 2008 IIS7上的问题
- 如何在asp.net中使用C#将使用jQuery flotdynamic生成的图表导出到Excel中?
- 在带有阿拉伯语内容的aspx页面中将HTML表格导出到Excel中
- 从文件加载Excel文件并更新内存中的单元格值
- 如何显示窗口提示下载excel文件?
- 创buildEXCEL并打开它
- 保存工作簿时出错:System.Runtime.InteropServices.COMException:从HRESULTexception:0x800A03EC
- 循环工作表在Excel中使用C#