C#实现从EXCEL文件读取数据到SqlServer数据库

用第三方组件:NPOI组件实现

先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用。使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。

先创建一个实体类:

 [Table("Customer") ]
    public class Customer
    {
        [Key]
        public int Id { get; set; }
        public string FirstName { get; set; }

        public string LastName { get; set; }
       
        public int Age { get; set; }

        public int Gender { get; set; }

    }
新建一个实体类Customer

创建一个类去实现读取Excel文件的数据到List<Customer>。

 public class ImportExcel
    {
        public IList<Customer> ImportExeclToCustomer(string filePath)
        {
            var customerList = new List<Customer>();
            Customer customer;
            if(string.IsNullOrEmpty(filePath))
            {
                return null;
            }

            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            XSSFWorkbook workBook = new XSSFWorkbook(fileStream);

            int sheetCount = workBook.Count;
            if(sheetCount >0)
            {
                var sheet = workBook.GetSheetAt(0);
                
                //从第二行开始导入,第一行是列名
                for(int i=1; i<= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    
                    //获得该行每一列的值
                    string tmpFirstName = GetCellValueStringFromISheet(row,0);
                    string tmpLastName = GetCellValueStringFromISheet(row,1);
                    string tmpAge = GetCellValueStringFromISheet(row,2);
                    string tmpGender = GetCellValueStringFromISheet(row,3);

                    customer = new Customer()
                    {
                        FirstName = tmpFirstName,
                        LastName = tmpLastName,
                        Age = Convert.ToInt32(tmpAge),
                        Gender = Convert.ToInt32(tmpGender)
                    };

                    customerList.Add(customer);
                }
            }

            return customerList;
        }

        private string GetCellValueStringFromISheet(IRow row, int colIndex)
        {
            if (row != null)
            {
                ICell cell = row.GetCell(colIndex);
                if (cell != null)
                {
                    if (cell.CellType == CellType.String)
                    {
                        return cell.StringCellValue.Trim();
                    }
                    if (cell.CellType == CellType.Numeric)
                    {
                        return cell.NumericCellValue.ToString().Trim();
                    }
                    return cell.StringCellValue.Trim();
                }
            }
            return string.Empty;
        }
    }
实现读取EXCEL中的数据到List中

在Main函数中方法,实现批量插入数据到SQL Server数据库表中

class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\Customer_Test.xlsx";

            ImportExcel importExcel = new ImportExcel();
            var customerList = importExcel.ImportExeclToCustomer(filePath);

            #region 添加数据到数据库
            using (CodeFirstDBContext context = new CodeFirstDBContext())
            {
                //EF大数据批量处理
                context.BulkInsert(customerList);
                context.SaveChanges();

            };
            #endregion

            Console.ReadKey();
        }
    }
Main函数中实现插入数据到数据库中

代码中应用到了EF创建实体,批量插入数据的方法,后续文章中会详细列出

posted @ 2016-06-03 18:26  only one minmin  阅读(1257)  评论(0编辑  收藏  举报