轉載自: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, "數據庫表名"); }