Excel在ASP.NET中下载大数据集的页面
本文关键字:数据集 下载 ASP NET Excel | 更新日期: 2023-09-27 18:18:15
我开发了一个应用程序,我从数据库中获取数据,将其绑定到inffragistics网格,然后下载excel使用其导出实用程序。
当数据集很大(比如20000条或更多)时,这种方法有一个问题,它需要很长时间来处理和下载,并且通常它会死页并向用户显示空白页。
在excel 下载过程中是否有更好的方法来处理这个问题并进行合理的改进?
代码如下所示:
public void LoadExcelPostingData()
{
try
{
query = "Some complex query here with up to 10 columns";
dt.Clear();
dt = new DataTable();
db2.GetDataTable(query, CommandType.Text, ref dt);
grdJurdata.DataSource = dt;
grdJurdata.DataBind();
ExportToExcel();
}
catch (Exception ex)
{
lblresult.Text = "Grd Err : " + ex.Message;
}
}
private void ExportToExcel()
{
try
{
// Infragistics built in excel export utility
UltraWebGridExcelExporter2.Export(grdJurdata);
}
catch (Exception ex)
{ }
}
关于文件下载微软的MSDN提供了详细的说明
- 获取响应 对于响应,将内容类型设置为"APPLICATION/OCTET-STREAM"(这意味着没有应用程序打开文件)。
- 设置标题为"Content-Disposition","attachment;filename='" + + "'".
- 将文件内容写入响应
还请记住,永远不要使用Ajax请求下载文件,因为文件传输,它需要完整的PostBack请求
这是在MSDN
<%
try
{
System.String filename = "myFile.txt";
// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";
// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment; Filename='"" + filename +
"'"";
Response.AppendHeader("Content-Disposition", disHeader);
// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo("C:''downloadJSP''DownloadConv''myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);}
catch (System.Exception e)
// file IO errors
{
SupportClass.WriteStackTrace(e, Console.Error);
}
%>
我也建议你读一读这个好的讨论
编辑#1:您的情况的另一个解决方案是创建一个新页面来保存ultrawebgridexcelexporters,并在主页中创建一个iframe标签来保存这个新页面。让iframe回发。并将您的infgristics版本升级到最新版本。
首先,您需要重新查看您如何编写的代码。您需要重构或改进代码库。
如果您想增加请求超时,那么您可以这样做您需要在web中添加更多的超时时间。配置
<system.web>
<httpruntime executionTimeout="4800"/> //or higher values
</system.web>