[英]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并不是一个好主意,因为:
What about another approaches: 那另一种方法呢:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.