示例#1
0
        public bool ImportLoctionExcel(ExcelWorksheet locationSheet)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
                                                                 new System.TimeSpan(0, 60, 0)))
            {
                try
                {
                    var colCount = locationSheet.Dimension.End.Column;
                    var rowCount = locationSheet.Dimension.End.Row;
                    for (int i = 2; i <= rowCount; i++)
                    {
                        Location location;
                        string   name = locationSheet.Cells[i, 1].Value.ToString();

                        if (_locationService.GetLocationByName(name) != null)
                        {
                            location              = _locationService.GetLocationByName(name);
                            location.Name         = locationSheet.Cells[i, 1].Value.ToString();
                            location.LocationCode = locationSheet.Cells[i, 2].Value.ToString();
                            location.Description  = locationSheet.Cells[i, 3].Value.ToString();
                            string campusName = locationSheet.Cells[i, 4].Value.ToString();
                            location.CampusID = _campusService.GetCampusCode(campusName).ID;
                            location.Active   = true;
                            _locationService.Update(location);
                            _locationService.SaveChanges();
                        }
                        else
                        {
                            location              = new Location();
                            location.Name         = locationSheet.Cells[i, 1].Value.ToString();
                            location.LocationCode = locationSheet.Cells[i, 2].Value.ToString();
                            location.Description  = locationSheet.Cells[i, 3].Value.ToString();
                            string campusName = locationSheet.Cells[i, 4].Value.ToString();
                            location.CampusID = _campusService.GetCampusCode(campusName).ID;
                            location.Active   = true;
                            _locationService.Add(location);
                            _locationService.SaveChanges();
                        }
                    }
                    scope.Complete();
                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
        }