public async Task <IActionResult> Upload(IFormFile file, Guid projectId) { var fileStream = file.OpenReadStream(); var excel = new ExcelStream(); excel.Load(fileStream); var sheet1 = excel.LoadSheetHDR(excel.WorkBook.First().Name); var slots = new List <OnCallSlot>(sheet1.Count); foreach (var x in sheet1) { var primary = await User.Manager.FindByNameAsync(x[2]); var backup = await User.Manager.FindByNameAsync(x[3]); var incidentManager = await User.Manager.FindByNameAsync(x[4]); slots.Add(new OnCallSlot { Begin = Convert.ToDateTime(x[0]), End = Convert.ToDateTime(x[1]), ProjectId = projectId, Role = SlotRole.Primary, UserId = primary.Id }); slots.Add(new OnCallSlot { Begin = Convert.ToDateTime(x[0]), End = Convert.ToDateTime(x[1]), ProjectId = projectId, Role = SlotRole.Backup, UserId = backup.Id }); slots.Add(new OnCallSlot { Begin = Convert.ToDateTime(x[0]), End = Convert.ToDateTime(x[1]), ProjectId = projectId, Role = SlotRole.IncidentManager, UserId = incidentManager.Id }); } DB.AddRange(slots); await DB.SaveChangesAsync(); return(Prompt(x => { x.Title = "导入成功"; x.Details = "On-Call日程表已经成功导入"; x.HideBack = true; x.RedirectUrl = Url.Action("Index", "OnCall", new { project = projectId }); x.RedirectText = "查看On-Call时间表"; })); }
public void Test_Load_Excel() { Test_Creat_Excel(); using (var x = _ExcelStream.Load(_ExcelPath)) { using (var sheet = x.LoadSheet(1)) { // Reading the data from sheet foreach (var a in sheet) { foreach (var b in a) { Assert.Equal(b, "Create test"); } } } } }
private async Task SaveCoupons(string fileName, string userId) { using (var x = _excelStream.Load(fileName)) { using (var sheet = x.LoadSheet(1)) { // Reading the data from sheet var index = 1; int baseUrlIndex = 0, priceIndex = 0, titleIndex = 0, shopNameIndex = 0, productNameIndex = 0, startIndex = 0, endIndex = 0, productTypeIndex = 0, pictureUrlIndex = 0, productUrlIndex = 0; IList <Coupon> coupons = new List <Coupon>(); foreach (var item in sheet) { if (index == 1) { for (var childIndex = 0; childIndex < item.Count; childIndex++) { switch (item[childIndex].Trim()) { case "商品名称": productNameIndex = childIndex; break; case "商品主图": pictureUrlIndex = childIndex; break; case "商品一级类目": productTypeIndex = childIndex; break; case "淘宝客链接": productUrlIndex = childIndex; break; case "商品价格": priceIndex = childIndex; break; case "店铺名称": shopNameIndex = childIndex; break; case "优惠券面额": titleIndex = childIndex; break; case "优惠券开始时间": startIndex = childIndex; break; case "优惠券结束时间": endIndex = childIndex; break; case "商品优惠券推广链接": baseUrlIndex = childIndex; break; } } index++; continue; } var coupon = new Coupon(); //Spider(item[10]); coupon.BaseUrl = item[baseUrlIndex]; coupon.Title = item[titleIndex]; coupon.ShopName = item[shopNameIndex]; coupon.ProductUrl = item[productUrlIndex]; coupon.PictureUrl = item[pictureUrlIndex]; coupon.ProductName = item[productNameIndex]; coupon.ProductType = item[productTypeIndex]; double price = 0; double.TryParse(item[priceIndex], out price); coupon.Price = price; DateTime startDate; DateTime.TryParse(item[startIndex], out startDate); coupon.StartDate = startDate; DateTime endDate; DateTime.TryParse(item[endIndex], out endDate); coupon.EndDate = endDate; coupon.UserId = userId; coupon.Id = Guid.NewGuid().ToString(); coupon.AddTime = DateTime.Now; coupon.Version = 0; coupons.Add(coupon); } await _service.AddAsync(coupons); } } }