[英]How to read .xlsx and .xls files using C# and OleDbConnection?
Previously I was using ExcelPackage
to read data out of a .xlsx file.以前我使用
ExcelPackage
从 .xlsx 文件中读取数据。 That was working fine but then I realized that ExcelPackage
doesn't work with the old .xls format.这工作正常,但后来我意识到
ExcelPackage
不适用于旧的 .xls 格式。 So I upgraded to using OleDbConnection
instead of ExcelPackage
like this:所以我升级到使用
OleDbConnection
而不是ExcelPackage
像这样:
var file = HttpContext.Current.Request.Files[0]; DataTable sheetData = new DataTable(); string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file.FileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""; using (OleDbConnection conn = new OleDbConnection(connStr)) { conn.Open(); DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); string sheetName = dtSchema.Rows[0].Field("TABLE_NAME"); OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); }
Basically just trying to read the first spreadsheet there.基本上只是试图阅读那里的第一个电子表格。 But I get this error in the exception:
但我在异常中收到此错误:
Cannot update. Database or object is read-only.
What am I doing wrong?我究竟做错了什么? Is there some type of update operation hidden in there?
那里是否隐藏了某种类型的更新操作?
Try this:试试这个:
OleDbConnection connection;
OleDbCommand command;
OleDbDataReader dr;
string commandText = "SELECT * FROM [Sheet1$]";
string oledbConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=" + filename + ";" +
"Extended Properties=\"Excel 12.0;HDR=YES\";";
connection = new OleDbConnection(oledbConnectString);
command = new OleDbCommand(commandText, connection);
try
{
connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
count++;
for (int i = 1; i < dr.VisibleFieldCount; i++)
{
Console.Writeln(""+dr[i].ToString());
}
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("" + ex.Message);
connection.Close();
}
Below is an example method that returns a DataSet
from the given excel file path.下面是一个示例方法,它从给定的 excel 文件路径返回一个
DataSet
。 The returned DataSet
should have each Excel worksheet in the workbook as a DataTable
in the DataSet
.返回的
DataSet
应将工作簿中的每个 Excel 工作表作为DataSet
的DataTable
。 This appears to work correctly, hope it may help.这似乎工作正常,希望它可以帮助。
private DataSet GetExcelDataSet(string path) {
string sheetName;
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
"; Jet OLEDB:Engine Type = 5; Extended Properties =\"Excel 8.0;\"";
DataSet ds = new DataSet();
using (OleDbConnection con = new OleDbConnection(ConnectionString)) {
using (OleDbCommand cmd = new OleDbCommand()) {
using (OleDbDataAdapter oda = new OleDbDataAdapter()) {
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < dtExcelSchema.Rows.Count; i++) {
sheetName = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
DataTable dt = new DataTable(sheetName);
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
oda.SelectCommand = cmd;
oda.Fill(dt);
dt.TableName = sheetName;
ds.Tables.Add(dt);
}
}
}
}
return ds;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.