【发布时间】:2012-01-09 19:56:39
【问题描述】:
我正在使用 C# 导出到 Excel,但在某些记录上出现此错误。
The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data
通过谷歌搜索,我发现它与大小限制有关,但我找不到解决方法。有什么想法吗?
请求的代码:
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 [ColderLeads$] ";
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();
}
谢谢!
【问题讨论】:
-
你能展示你用来导出的代码,以及它失败的数据元素吗?
-
I am exporting to Excel using C#- 您忘记在问题中显示 C#。 -
@M.Babcock,无法解释的否决票?前两个 cmets 有什么不清楚的地方吗?你还需要什么解释?附:我没有投反对票,但完全同意投反对票的人。这就像对机械师说:我的车坏了,问他为什么不带车。更糟糕的是:你让他修理你的车而不带坏车(我想 OP 是在要求我们修复他的代码)。
-
@M.Babcock:由于前两个 cmets 中所述的原因,我投了反对票;因此,为什么我赞成第一条评论。一旦贾斯汀添加了必要的信息,我肯定会删除我的反对票。
-
@DarinDimitrov - Justin 没有要求我们修复他的代码(对于大多数值得被否决的人来说,这比我能说的要多),他询问如何解决一个常见错误。他最初的问题更类似于,我的车不会启动,因为发动机不会转动。有足够的信息来理论化解决方案。
标签: c# export-to-excel excel-2003