转载自:https://blog.csdn.net/mzl87/article/details/79822488sql
项目使用的mvc模式,数据库访问使用的EF处理数据增删改查。数据库
首先你要有一个最直接的封装有SqlBulkCopy的类,以下: c#
/// <summary> /// 大批量数据导入到数据库中 /// </summary> /// <param name="dataTable">数据表</param> /// <param name="tableName">表名称</param> /// <returns>成功返回true,不然false</returns> public static bool BulkCopyTable(DataTable dataTable, string tableName) { try { using (var sqlBulkCopy = new SqlBulkCopy(connectionString)) { sqlBulkCopy.DestinationTableName = tableName; if (dataTable != null && dataTable.Rows.Count != 0) { sqlBulkCopy.WriteToServer(dataTable); } sqlBulkCopy.Close(); return true; } } catch (System.Exception ex) { Console.WriteLine(ex.Message); return false; } }
/// <summary> /// DataTable扩展类 /// </summary> public static class DataTableExtensions { /// <summary> /// Convert a List{T} to a DataTable. /// </summary> public static DataTable ToDataTable<T>(List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } 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); } tb.Rows.Add(values); } return tb; } /// <summary> /// Return underlying type if type is Nullable otherwise return the type /// </summary> public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) { return t; } return Nullable.GetUnderlyingType(t); } return t; } /// <summary> /// Determine of specified type is nullable /// </summary> public static bool IsNullable(Type t) { return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } }
注:这个类主要是将导入的数据集合转换为datatable以方便做为批量插入方法的参数。数组
var data = ExcelOpr.ImportExcel(path); var lstA = new List<A>(); for (int rowIndex = 1; rowIndex < data.GetLength(0); rowIndex++) { var a = new A(); //处理具体的字段赋值 lstA.Add(a); } //导入到数据库中 if (lstA != null && lstA.Count > 0) { var dt = DataTableExtensions.ToDataTable<A>(lstA); tempService.ImportExecel(dt); }
注:一、此处A为具体须要导入的数据表在EF中对应的c#类名,请必定保证EF中对象字段的顺序和数据库中的列顺序一致,不然没法导入;mvc
二、第一句代码spa
ExcelOpr.ImportExcel(path);
path为导入文件的绝对路径,使用的是封装的导入方法,代码以下:(须要使用Aspose.Cells,在vs中自行下载、添加).net
/// <summary> /// 导入excel;返回是object的数组类型数据; /// 其中data.GetLength(0)表示数据的条数,包括标题栏; /// data.GetLength(1)表示数据的列数; /// data.GetValue(1, 1)表示拿到对应下表的数据; /// 使用形如:for (int i = 1; i 小于 data.GetLength(0); i++){}方式生成具体类型便可! /// </summary> /// <param name="strFileName">文件名(包含路径)</param> /// <returns>Object[,]数组类型</returns> public static Object[,] ImportExcel(String strFileName) { Workbook book = new Workbook(strFileName); //book.Open(strFileName); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; return cells.ExportArray(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1); }
三、最后一句excel
tempService.ImportExecel(dt);
tempService 为业务逻辑实现的对象,调用具体的导入方法:code
/// <summary> /// excel数据导入,批量导入 /// </summary> /// <param name="dt">datatable</param> /// <returns></returns> public bool ImportExecel(System.Data.DataTable dt) { return SqlHelper.BulkCopyTable(dt, "数据库表名"); }