从 .xls / .xlsx 文件中读取记录
从 .csv 文件中读取记录
Excel Helper
using ExcelDataReader; using OfficeOpenXml; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; internal class ExcelHelper { internal static DataTable GetDataTableFromCsv(string path) { var stream = File.Open(path, FileMode.Open, FileAccess.Read); var ds = ExcelReaderFactory.CreateCsvReader(stream).AsDataSet(new ExcelDataSetConfiguration() { ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration() { UseHeaderRow = true } }); if (ds.Tables.Count == 0) return null; var dt = ds.Tables[0]; foreach (DataColumn col in dt.Columns) { if (string.IsNullOrEmpty(col.ColumnName)) continue; col.ColumnName = col.ColumnName.Replace(" ", ""); } return dt; } internal static DataTable GetDataTableFromExcel(string path, bool hasHeader = true) { using (var pck = new ExcelPackage()) { using (var stream = File.OpenRead(path)) { pck.Load(stream); } var ws = pck.Workbook.Worksheets.First(); DataTable tbl = new DataTable(); foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column]) { var columnName = string.Empty; if (hasHeader) { columnName = string.IsNullOrEmpty(firstRowCell.Text) ? string.Empty : firstRowCell.Text.Replace(" ", ""); } tbl.Columns.Add(hasHeader ? columnName : string.Format("Column {0}", firstRowCell.Start.Column)); } var startRow = hasHeader ? 2 : 1; for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++) { var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column]; DataRow row = tbl.Rows.Add(); foreach (var cell in wsRow) { row[cell.Start.Column - 1] = cell.Text; } } return tbl; } } internal static DataTable ToDataTable<T>(List<T> items) { DataTable dataTable = new DataTable(typeof(T).Name); PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) { var type = prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType; dataTable.Columns.Add(prop.Name, type); } foreach (T item in items) { var values = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { values[i] = Props[i].GetValue(item, null); } dataTable.Rows.Add(values); } return dataTable; } internal static List<T> ConvertDataTable<T>(DataTable dt) { List<T> data = new List<T>(); foreach (DataRow row in dt.Rows) { T item = GetItem<T>(row); data.Add(item); } return data; } internal static T GetItem<T>(DataRow dr) { Type temp = typeof(T); T obj = Activator.CreateInstance<T>(); foreach (DataColumn column in dr.Table.Columns) { foreach (PropertyInfo pro in temp.GetProperties()) { if (pro.Name == column.ColumnName) pro.SetValue(obj, dr[column.ColumnName], null); else continue; } } return obj; } }
参考资料
https://www.nuget.org/packages/ExcelDataReader
https://www.nuget.org/packages/ExcelDataReader.DataSet
https://www.nuget.org/packages/EPPlus
https://stackoverflow.com/a/13396787
https://www.cnblogs.com/holyson/p/3690831.html
https://stackoverflow.com/a/55024803
https://stackoverflow.com/a/13698809
https://forums.asp.net/post/6134316.aspx