问题
I am trying to export some data to Excel. I am using OLEDB 12. The connectio string looks like:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;'"
And I use an INSERT query. But whenever the data in a target column exceeds 255 chars, I get an exception.
Exception Details: System.Data.OleDb.OleDbException: The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data.
There is a similar post on SO: Excel unable to insert more than 255 chars? but it is not into c#,
I also referred to http://support.microsoft.com/kb/213841 and not getting any solution.
Please help.
回答1:
I was initially thinking that you might be able to define the data type (to memo/text) of the cells before writing the data but this does not appear possible from this article http://support.microsoft.com/kb/278973 (About half way down it explictly states defining the data type in excel is not possible.)
The "best" solution I could offer you is to chop your data into 255 char pieces and insert them into the excel file into nieghbouring columns. from there you could use some excel interop to splice them back together.
回答2:
What I did finally:
As I could no amass enough responses and was not able to solve the issue, I switched to Excel object (Office Interop) and there is no issue now.
回答3:
Try to use this code, may it ll help
public static void DataSetsToExcel(DataSet dataSet, string filepath)
{
try
{
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;";
string tablename = "";
DataTable dt = new DataTable();
foreach (System.Data.DataTable dataTable in dataSet.Tables)
{
dt = dataTable;
tablename = dataTable.TableName;
using (OleDbConnection con = new OleDbConnection(connString))
{
con.Open();
StringBuilder strSQL = new StringBuilder();
strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]");
strSQL.Append("(");
for (int i = 0; i < dt.Columns.Count; i++)
{
strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,");
}
strSQL = strSQL.Remove(strSQL.Length - 1, 1);
strSQL.Append(")");
OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
cmd.ExecuteNonQuery();
for (int i = 0; i < dt.Rows.Count; i++)
{
strSQL.Clear();
StringBuilder strfield = new StringBuilder();
StringBuilder strvalue = new StringBuilder();
for (int j = 0; j < dt.Columns.Count; j++)
{
strfield.Append("[" + dt.Columns[j].ColumnName + "]");
strvalue.Append("'" + dt.Rows[i][j].ToString().Replace("'", "''") + "'");
if (j != dt.Columns.Count - 1)
{
strfield.Append(",");
strvalue.Append(",");
}
else
{
}
}
if (strvalue.ToString().Contains("<br/>"))
{
strvalue = strvalue.Replace("<br/>", Environment.NewLine);
}
cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ")
.Append(strfield.ToString())
.Append(") values (").Append(strvalue).Append(")").ToString();
cmd.ExecuteNonQuery();
}
con.Close();
}
}
}
catch (Exception ex)
{
}
}
来源:https://stackoverflow.com/questions/8655000/c-sharp-error-inserting-to-excel-when-string-data-is-more-than-255-chars