SqlBulkCopy批量将Excel(Aspose)数据导入至SQL Server

转载自:https://blog.csdn.net/mzl87/article/details/79822488

项目使用的mvc模式,数据库访问使用的EF处理数据增删改查。

首先你要有一个最直接的封装有SqlBulkCopy的类,如下: 

        /// <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;
            }
        }

  

  • 其次,项目中使用到了一个DataTable的扩展类,具体如下:
    /// <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);
   }

  

注:1、此处A为具体需要导入的数据表在EF中对应的c#类名,请一定保证EF中对象字段的顺序和数据库中的列顺序一致,否则无法导入;

    2、第一句代码

ExcelOpr.ImportExcel(path);

  path为导入文件的绝对路径,使用的是封装的导入方法,代码如下:(需要使用Aspose.Cells,在vs中自行下载、添加)

        /// <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);
        }

    3、最后一句

tempService.ImportExecel(dt);

tempService 为业务逻辑实现的对象,调用具体的导入方法:

        /// <summary>
        /// excel数据导入,批量导入
        /// </summary>
        /// <param name="dt">datatable</param>
        /// <returns></returns>
        public bool ImportExecel(System.Data.DataTable dt)
        {
            return SqlHelper.BulkCopyTable(dt, "数据库表名");
        }
    

猜你喜欢

转载自www.cnblogs.com/Denghejing/p/9831480.html
今日推荐