C# WebApplication - System.Data.OleDb 已找到但几乎为空
C# WebApplication - System.Data.OleDb found but nearly empty
我目前正在尝试使用 OleDB 从 Excel 文件中读取。
这是代码:
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;
namespace WebApplication1.My_Logic.Worker
{
public class ExcelLoader
{
private string GetConnectionString()
{
Dictionary<string, string> props = new Dictionary<string, string>();
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "Excel 12.0 XML";
props["Data Source"] = "C:\MyExcel.xlsx";
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
return sb.ToString();
}
private DataSet ReadExcelFile()
{
DataSet ds = new DataSet();
string connectionString = GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// Get all Sheets in Excel File
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// Loop through all Sheets to get data
foreach (DataRow dr in dtSheet.Rows)
{
string sheetName = dr["TABLE_NAME"].ToString();
if (!sheetName.EndsWith("$"))
continue;
// Get all rows from the Sheet
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
DataTable dt = new DataTable();
dt.TableName = sheetName;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
ds.Tables.Add(dt);
}
cmd = null;
conn.Close();
}
return ds;
}
}
}
System.Data.OleDb
显示为灰色(未使用)并且无法找到以下 类:
OleDbCommand
OleDbConnection
OleDbSchemaGuid
OleDbDataAdapter
当我查看 System.Data.OleDb
时,只有 2 类 显示为 Visual Studio:
System.Data.OleDb.OleDbPermission
System.Data.OleDb.OleDbPermissionAttribute
我在互联网上到处阅读,唯一的答案是添加 using System.Data.OleDb
,但我确实有它,而且 Visual Studio 似乎知道它。
它是 ASP.NET 核心 2.1 API 项目,来自 Visual Studio 2017。
OleDb 在 .NET Core 中不可用,因为它不是为其他平台实现的,而不是 windows。
见下文githubthread.
如果您想从 xlsx 中读取,只需使用 OpenXmlSdk,它是跨平台的并且受 .NET 核心支持。
在下面的 thread.
中查看如何使用 OpenXmlSdk 读取 excel 文件
我目前正在尝试使用 OleDB 从 Excel 文件中读取。
这是代码:
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;
namespace WebApplication1.My_Logic.Worker
{
public class ExcelLoader
{
private string GetConnectionString()
{
Dictionary<string, string> props = new Dictionary<string, string>();
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "Excel 12.0 XML";
props["Data Source"] = "C:\MyExcel.xlsx";
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
return sb.ToString();
}
private DataSet ReadExcelFile()
{
DataSet ds = new DataSet();
string connectionString = GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// Get all Sheets in Excel File
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// Loop through all Sheets to get data
foreach (DataRow dr in dtSheet.Rows)
{
string sheetName = dr["TABLE_NAME"].ToString();
if (!sheetName.EndsWith("$"))
continue;
// Get all rows from the Sheet
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
DataTable dt = new DataTable();
dt.TableName = sheetName;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
ds.Tables.Add(dt);
}
cmd = null;
conn.Close();
}
return ds;
}
}
}
System.Data.OleDb
显示为灰色(未使用)并且无法找到以下 类:
OleDbCommand
OleDbConnection
OleDbSchemaGuid
OleDbDataAdapter
当我查看 System.Data.OleDb
时,只有 2 类 显示为 Visual Studio:
System.Data.OleDb.OleDbPermission
System.Data.OleDb.OleDbPermissionAttribute
我在互联网上到处阅读,唯一的答案是添加 using System.Data.OleDb
,但我确实有它,而且 Visual Studio 似乎知道它。
它是 ASP.NET 核心 2.1 API 项目,来自 Visual Studio 2017。
OleDb 在 .NET Core 中不可用,因为它不是为其他平台实现的,而不是 windows。
见下文githubthread.
如果您想从 xlsx 中读取,只需使用 OpenXmlSdk,它是跨平台的并且受 .NET 核心支持。
在下面的 thread.
中查看如何使用 OpenXmlSdk 读取 excel 文件