有一个具有20,000行的excel表。 我想将数据从Excel电子表格添加到数据库中。 使用以下代码,我可以在2分钟内成功将20,000行保存到数据库中,但这对我来说还不够快。 如何更快地将数据添加到数据库? 如果在读取后将其转换为对象,可以更快地读取Excel电子表格吗? 代码运行速度不够快;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using OfficeOpenXml;
using Proje1_1.Models;
namespace Proje1_1.Controllers
{
public class ExcelToDbController : Controller
{
DenemeEntities db = new DenemeEntities();
// GET: ExcelToDb
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Get(HttpPostedFileBase excelFile)
{
if (excelFile == null || excelFile.ContentLength == 0)
{
ViewBag.Error = "Dosya Seçimi Yapın";
return View();
}
else
{
if (excelFile.FileName.EndsWith("xls") || excelFile.FileName.EndsWith("xlsx"))
{
string path = Server.MapPath("~/Assets/PlateImage/" + excelFile.FileName);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
excelFile.SaveAs(path);
var package = new ExcelPackage(new System.IO.FileInfo(path));
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
var rowCnt = worksheet.Dimension.End.Row;
List<plate_logs> localPlate = new List<plate_logs>();
for (int i = 2; i <= rowCnt; i++)
{
plate_logs pl = new plate_logs();
pl.Plate = Convert.ToString(worksheet.Cells[i, 3].Value);
if ((pl.Plate != "") && (pl.Plate.Length <= 10))
{
pl.Guid = Convert.ToInt64(worksheet.Cells[i, 1].Value);
pl.UserId = 0;
pl.Date = Convert.ToDateTime(worksheet.Cells[i,2].Value);
db.plate_logs.Add(pl);
}
}
db.SaveChanges();
return View("Listele");
}
else
{
ViewBag.Error = "Dosya Tipi Yanlış";
return View();
}
}
}}}