Это код:
Код: Выделить всё
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 props = new Dictionary();
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 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.OleDbPermission
System.Data.OleDb.OleDbPermissionAttribute
Это недавно созданный проект API ASP.NET Core 2.1 из Visual Studio 2017.
Подробнее здесь: https://stackoverflow.com/questions/527 ... arly-empty
Мобильная версия