我們有一個包含2500列和2000行的數據表。當我們嘗試使用OLEDB導出時,出現「定義的字段太多」的錯誤。我不能使用Excel Inter操作,因爲它消耗更多時間。有沒有其他辦法可以解決這個問題使用OLEDB將數據表導出到Excel會拋出太多定義的字段錯誤
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended Properties=" + "\"" + "Excel 8.0;HDR=NO;" + "\""))
{
con.Open();
OleDbCommand cmdInsert;
if (createTable || !File.Exists(fileName))
{
sql = "CREATE TABLE " + tableName + " (";
for (int i = 0; i < tbl.Columns.Count; i++)
{
sql += "[" + tbl.Columns[i].ColumnName + "]";
if (i + 1 == tbl.Columns.Count) //Here we decide should we close insert command or appebd another create column command
sql += " " + GetColumnType(tbl.Columns[i]) + ")"; //Close insert
else
sql += " " + GetColumnType(tbl.Columns[i]) + ","; //there is more columns to add
}
}
if (!String.IsNullOrEmpty(sql))
{
cmdInsert = new OleDbCommand(sql, con);
cmdInsert.ExecuteNonQuery();
}
foreach (DataRow row in tbl.Rows)
{
//Dodati parametre na comandu
string values = "(";
for (int i = 0; i < tbl.Columns.Count; i++)
{
if (i + 1 == tbl.Columns.Count)
{
if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Int64") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Double"))
values += String.IsNullOrEmpty(row[i].ToString()) ? "0)" : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ")";
else
values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "')";
//values += "'" + "test" + "')";
}
else
{
if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Int64") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Double"))
values += String.IsNullOrEmpty(row[i].ToString()) ? "0," : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ",";
else
values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "',";
//values += "'" + "test" + "',";
}
}
string sqlInsert = String.Format("Insert into [{0}$] VALUES {1}", tableName, values);
cmdInsert = new OleDbCommand(sqlInsert, con);
cmdInsert.ExecuteNonQuery();
}
}
你知道問題在那裏記錄的響應OleDb的最大列數是255?我遇到了一個類似的問題,從Excel工作表中填充DataTable,該Excel工作表中有一堆空的但已初始化的列。 – Jim 2012-07-26 17:32:55