简体   繁体   English

在MVC 5中使用Epplus和Sqlbulkcopy将Excel文件导入数据库

[英]Import Excel file into database using Epplus and Sqlbulkcopy in MVC 5

I am importing a large data file from excel into my database table using Epplus library in MVC5 and Entity Framework 6. It is taking alot of time to copy that file to the database. 我正在使用MVC5和Entity Framework 6中的Epplus库将excel中的大数据文件导入到我的数据库表中。这需要大量时间才能将该文件复制到数据库中。 I am looking to use sqlbulkcopy to insert the data quickly but I am not really sure how to do that inside my controller. 我希望使用sqlbulkcopy快速插入数据,但我不确定如何在控制器中执行此操作。 The code is working now but it takes alot of time for all the data to be imported into the database. 该代码现在正在运行,但是将所有数据导入数据库都需要花费大量时间。

  public ActionResult Structure(FormCollection formCollection)
    {
        var usersList = new List<bomStructuredImportTgt>();
        if (Request != null)
        {
            HttpPostedFileBase file = Request.Files["UploadedFile"];
            if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
            {
                string fileName = file.FileName;
                string fileContentType = file.ContentType;
                byte[] fileBytes = new byte[file.ContentLength];
                var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                using (var package = new ExcelPackage(file.InputStream))
                {
                    var currentSheet = package.Workbook.Worksheets;
                    var workSheet = currentSheet.First();
                    var noOfCol = workSheet.Dimension.End.Column;
                    var noOfRow = workSheet.Dimension.End.Row;
                    for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                    {
                        var user = new bomStructuredImportTgt();
                        user.ActualDate = Convert.ToDateTime(workSheet.Cells[rowIterator, 1].Value);
                        user.Description = workSheet.Cells[rowIterator, 2].Value?.ToString();
                        user.Level = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
                        user.ParentPartNumber = workSheet.Cells[rowIterator, 4].Value?.ToString();
                        user.PartNumber = workSheet.Cells[rowIterator, 5].Value?.ToString();
                        user.PartName = workSheet.Cells[rowIterator, 6].Value?.ToString();
                        user.HNS = workSheet.Cells[rowIterator, 7].Value?.ToString();
                        user.DWGSZ = workSheet.Cells[rowIterator, 8].Value?.ToString();
                        user.Part = workSheet.Cells[rowIterator, 9].Value?.ToString();
                        user.L1Quantity = Convert.ToInt32(workSheet.Cells[rowIterator, 10].Value);
                        user.ColorM = workSheet.Cells[rowIterator, 11].Value?.ToString();
                        user.ATTCD = workSheet.Cells[rowIterator, 12].Value?.ToString();
                        user.KD = workSheet.Cells[rowIterator, 13].Value?.ToString();
                        user.Sell = workSheet.Cells[rowIterator, 14].Value?.ToString();
                        user.PlGroup = workSheet.Cells[rowIterator, 15].Value?.ToString();
                        user.PL1 = workSheet.Cells[rowIterator, 16].Value?.ToString();
                        user.AT1 = workSheet.Cells[rowIterator, 17].Value?.ToString();
                        user.PL2 = workSheet.Cells[rowIterator, 18].Value?.ToString();
                        user.AT2 = workSheet.Cells[rowIterator, 19].Value?.ToString();
                        user.PL3 = workSheet.Cells[rowIterator, 20].Value?.ToString();
                        user.Plant = workSheet.Cells[rowIterator, 21].Value?.ToString();
                        user.SHRPCMINMAX = workSheet.Cells[rowIterator, 22].Value?.ToString();
                        usersList.Add(user);
                    }
                }
            }
        }
        using (Dev_Purchasing_New_ModelEntities excelImportDBEntities = new Dev_Purchasing_New_ModelEntities())
        {
            foreach (var item in usersList)
            {
                excelImportDBEntities.bomStructuredImportTgts.Add(item);
            }
            excelImportDBEntities.SaveChanges();
        }
        return View("Structure");
    }

IMHO , it's not a good idea to load huge excels via web site directly because: 恕我直言 ,直接通过网站加载巨大的Excel并不是一个好主意,因为:

  1. It is not so easy to use BULK insert or BCP from ASP. 使用ASP的BULK插入或BCP并不是那么容易。
  2. You'll make a user waits until process is finished. 您将使用户等待过程完成。

What about another approaches: 那另一种方法呢:

  1. use ETL (SSIS) to perform all loads to database and just copy user's excel to some share folder where SSIS package can read this file. 使用ETL (SSIS)来执行对数据库的所有加载,只需将用户的excel复制到某个共享文件夹中,SSIS包就可以读取此文件。 After data is loaded you can notify user with mail and link to his/her data or use a web-site notification. 加载数据后,您可以通过邮件通知用户并链接到其数据或使用网站通知。
  2. The same as the first but without a SSIS. 与第一个相同,但没有SSIS。 Just copy file to some share that can be read by sql service and run BULK Insert with SQL command. 只需将文件复制到一些可由sql服务读取的共享,然后使用SQL命令运行BULK Insert。 But keep in mind that BULK insert works with csv files. 但是请记住,批量插入可用于csv文件。
  3. The same as the first two but instead bulk insert or bulk copy you can try OPENROWSET command to read excel but you'll require some drivers to be installed on SQL server side. 与前两个相同,但是可以批量插入或批量复制,您可以尝试使用OPENROWSET命令读取excel,但是您需要在SQL Server端安装一些驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM