我使用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();
}
谢谢!
答案 0 :(得分:3)
您使用的是Excel 2003吗?如果是这样,Excel工作表的大小有限制。它是65,536行乘256列。有关详细信息,请参阅以下link。
列宽也有限制。这是255个字符。尝试在将其导出到Excel之前修剪C#中的所有字段。您还可以做的是当工作簿达到最大行长度时,创建另一个工作表。