将数据从 Excel Sheet 加载到 DataTable 时遇到错误
Facing error while loading data from Excel Sheet to DataTable
using (OleDbConnection connection = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\""))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
以上是我正在使用的代码并得到以下错误:
Cannot update. Database or object is read-only.
有人遇到过同样的问题吗?
您的连接字符串包含 "Readonly=1;"。尝试更改 "Readonly=0;"。并尝试删除 "imex=1"。所以你的代码应该是这样的:
using (OleDbConnection connection = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";Readonly=0;Extended Properties=Excel 8.0;\""))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
尝试使用以下语法(从扩展属性中删除 Text
,因为它用于导入 csv 文件):
using (OleDbConnection connection = new
OleDbConnection(String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"HDR={1};IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\"",pathOnly ,header )))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
如果您安装了 Office 2007 或更高版本的提供程序,请尝试使用 Microsoft.ACE.OLEDB.12.0
提供程序,因为它还支持读取旧的 excel 格式。
如果您尝试导入文本文件 (csv),那么最好使用文本解析库,例如:
using (OleDbConnection connection = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\""))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
以上是我正在使用的代码并得到以下错误:
Cannot update. Database or object is read-only.
有人遇到过同样的问题吗?
您的连接字符串包含 "Readonly=1;"。尝试更改 "Readonly=0;"。并尝试删除 "imex=1"。所以你的代码应该是这样的:
using (OleDbConnection connection = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";Readonly=0;Extended Properties=Excel 8.0;\""))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
尝试使用以下语法(从扩展属性中删除 Text
,因为它用于导入 csv 文件):
using (OleDbConnection connection = new
OleDbConnection(String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"HDR={1};IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\"",pathOnly ,header )))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
adapter.Fill(dataTable);
}
}
}
如果您安装了 Office 2007 或更高版本的提供程序,请尝试使用 Microsoft.ACE.OLEDB.12.0
提供程序,因为它还支持读取旧的 excel 格式。
如果您尝试导入文本文件 (csv),那么最好使用文本解析库,例如: