C# 导出到Excel

C# 导出到Excel,c#,export-to-excel,excel-2003,C#,Export To Excel,Excel 2003,我正在使用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.Map

我正在使用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();
  }

谢谢

您正在使用Excel 2003吗?如果是,则Excel工作表的大小有限制。它是65536行乘256列。有关更多信息,请参见以下内容


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

是否使用Excel 2003?如果是,则Excel工作表的大小有限制。它是65536行乘256列。有关更多信息,请参见以下内容


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

您可以显示用于导出的代码以及失败的数据元素吗?
我正在使用C#
导出到Excel-您忘了在问题中显示C。@M.Babcock,无法解释的向下投票?前两条评论有什么不清楚的地方吗?你还需要什么解释?我没有投反对票,但完全同意投反对票的人。这就像对一个机械师说:我的车坏了,问他为什么不把车带来。更糟糕的是:你要求他修理你的车而不带坏了的车(我想OP是要求我们修理他的代码)。@M.Babcock:我否决了前两条评论中所述的理由;因此,我对第一条评论投了赞成票。一旦贾斯汀添加了必要的信息,我肯定会取消我的否决票。@DarinDimitrov-贾斯汀没有要求我们修改他的代码(对于大多数应该被否决的人来说,这比我能说的更多),他问如何解决一个常见错误。他最初的问题更类似于,我的车不会启动,因为发动机不会翻转。有足够的信息来推理一个解决方案。你能显示你用来导出的代码,以及它失败的数据元素吗?
我正在用C#
导出到Excel-你忘了在你的问题中显示C。@M.Babcock,原因不明的否决票?前两条评论有什么不清楚的地方吗?你还需要什么解释?我没有投反对票,但完全同意投反对票的人。这就像对一个机械师说:我的车坏了,问他为什么不把车带来。更糟糕的是:你要求他修理你的车而不带坏了的车(我想OP是要求我们修理他的代码)。@M.Babcock:我否决了前两条评论中所述的理由;因此,我对第一条评论投了赞成票。一旦贾斯汀添加了必要的信息,我肯定会取消我的否决票。@DarinDimitrov-贾斯汀没有要求我们修改他的代码(对于大多数应该被否决的人来说,这比我能说的更多),他问如何解决一个常见错误。他最初的问题更类似于,我的车不会启动,因为发动机不会翻转。有足够的信息来推理解决方案。谢谢,我没有太多的列或行。只有一列可以有一段文字,这似乎太大了。@Justin该段文字中有回车符吗?尝试将数据剥离为原始字符串而不进行任何格式化。谢谢,我没有太多的列或行。只有一列可以有一段文字,这似乎太大了。@Justin该段文字中有回车符吗?尝试将数据剥离为原始字符串,而不使用任何格式。