【问题标题】:Exporting to Excel导出到 Excel
【发布时间】: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


【解决方案1】:

您使用的是 Excel 2003 吗?如果是这样,则 Excel 工作表的大小有限制。它是 65,536 行 x 256 列。请参阅以下link 了解更多信息。

列宽也有限制。这是 255 个字符。在将其导出到 Excel 之前,请尝试在 C# 中修剪所有字段。您还可以做的是当工作簿达到最大行长度时,创建另一个工作表。

【讨论】:

  • 谢谢,我没有很多列或行。一栏可以有一段文字,这似乎太大了。
  • @Justin 段落中有回车吗?尝试将数据剥离为不带任何格式的原始字符串。
猜你喜欢
  • 1970-01-01
  • 2010-11-05
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多