C#当字符串数据超过255个字符时插入Excel出错

5

我正在尝试将一些数据导出到Excel。我正在使用OLEDB 12。连接字符串如下:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;'"

我使用的是INSERT查询。但是每当目标列中的数据超过255个字符时,就会出现异常。

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.

在SO上有一个类似的帖子:Excel unable to insert more than 255 chars?,但它不是关于c#的。

我也参考了http://support.microsoft.com/kb/213841,但没有得到任何解决方案。

请帮忙。

3个回答

1

我最初认为你可以在写入数据之前定义单元格的数据类型(如memo/text),但是根据这篇文章http://support.microsoft.com/kb/278973(在一半的位置明确指出Excel中无法定义数据类型),这似乎是不可能的。

我能提供给你的“最佳”解决方案是将你的数据切割成255个字符的片段,并将它们插入到Excel文件的相邻列中。然后,你可以使用一些Excel互操作来将它们拼接回来。


0

我最终做了什么:

由于我无法获得足够的响应并且无法解决问题,我转而使用Excel对象(Office Interop),现在没有问题了。


0

尝试使用这段代码,希望它能有所帮助。

    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)
        {                
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接