使用 OLEDB 将数据表导出到 Excel 会引发太多字段定义错误
我们有一个包含 2500 列和 2000 行的数据表。当我们尝试使用 OLEDB 导出时,我收到错误“定义的字段太多”。我无法使用 Excel Inter op,因为它会消耗更多时间。有没有其他方法可以解决这个问题
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();
}
}
We have a data table with 2500 columns and 2000 rows. When we try to export using OLEDB i am getting an error "too many fields defined". I cant use Excel Inter op, since it consumes more time. Is there any other way to fix this issue
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现使用 oledb 的限制是 255 列。因此,我使用下面的代码将数据表转换为 csv 并将其写入 http 页面的响应中
,现在我能够解决导出大量数据的问题
I found out that the limitation of using oledb is 255 columns. Hence i am converting the datatable into csv using the below code and write it into response of the http page
now i am able to fix the issue of exporting huge data
Excel(您也正在连接)是否通过“表”定义支持列数限制?这可能是你的瓶颈。
我建议,在构建表格时,对所有列进行此操作。我建议以批处理周期运行它。调用该函数并一次执行 100 列并仅导出 2 或 3 行。因此,可能需要 20 个循环周期才能构建所有 2000 个列,但您可能会在那里找到突破点。如果您确实达到了完整的 2000 列,则展开正在导出的行并找出失败的插入行。可能有一些 NULL 值在杀死您的进程。
Does Excel (which you are connecting too) have a limit of columns it supports via a "table" definition? that might be your bottleneck.
What I would suggest, is when building your table out, you do it for all the columns. I would suggest running it in batch cycles. Call the function and do 100 columns at a time and export only 2 or 3 rows. So, it may take 20 loop cycles to build out all 2000 columns, but you might find your break there. If you DO get to the full 2000 columns, then expand the rows being exported and find out at what ROW inserted it fails. It could be there are some NULL values floating around killing your process.