我在ASP.NET页面中有一个Grid,我有一个Export按钮。单击“导出”按钮时,它将执行下面的方法,并使用网格中的数据填充Excel文档。但它只会填充第1页上的数据。如果我有100个项目和10个页面,它只会执行第1页。我认为它会做所有而不只是显示的那些。知道为什么会这样做吗?
由于
private void fillDocument()
{
string lFilename = Leads.xls";
string lDistributorFolder = Server.MapPath(".") + "\\Portals\\0\\Distributors\\" + _currentUser.UserID.ToString() + "\\";
string lTemplateFolder = System.Configuration.ConfigurationManager.AppSettings["Templates"];
System.IO.Directory.CreateDirectory(lDistributorFolder);
File.Copy(lTemplateFolder + lFilename, lDistributorFolder + lFilename, true);
string lConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lDistributorFolder + "\\" + lFilename + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
DbProviderFactory lFactory = DbProviderFactories.GetFactory("System.Data.OleDb");
int lSequence = 0;
using (DbConnection lConnection = lFactory.CreateConnection())
{
lConnection.ConnectionString = lConnectionString;
lConnection.Open();
foreach (GridDataItem lItem in grdLeadList.Items)
{
lSequence++;
using (DbCommand lCommand = lConnection.CreateCommand())
{
lCommand.CommandText = "INSERT INTO [Leads$] ";
lCommand.CommandText += "(F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21) ";
lCommand.CommandText += "VALUES(";
lCommand.CommandText += "\"" + lSequence.ToString() + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gLeadNumber].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gSource].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gAccountName].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gCreatedOn].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gContactFullName].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gPriority].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gStreet1].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gStreet2].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gZIP].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gCity].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += "\"" + lItem.Cells[_gState].Text.Replace(" ", " ") + "\",";
lCommand.CommandText += ")";
lCommand.ExecuteNonQuery();
}
}
lConnection.Close();
}
}
答案 0 :(得分:2)
网格是否用于以任何方式编辑或操作数据?通常,最好从填充网格的数据中导出值,而不是从网格本身导出。
毕竟,如果网格有分页,那么显示的实际网格只包含单页数据。但是填充它的数据源包含所有内容。
从数据源导出,而不是从UI控件导出。
答案 1 :(得分:1)
绑定网格时,只有可见的项目实际上绑定到网格。当您使用分页时,通常会对基础数据源进行另一次调用(您通常必须自己处理此分页)
如果要显示所有数据,当他们单击“导出”按钮时,您应该重新加载数据源并将导出从整个数据源中取消