private IXPSimpleObject GetExistingOrCreateNewObject(XPObjectSpace objectSpace, string keyPropertyName,
     Row excelRow, Type type){
     Mapping idMapping = ImportMap.Mappings.SingleOrDefault(p => p.MapedTo == keyPropertyName);
     IXPSimpleObject newObj = null;
     if (idMapping != null && ImportUtils.GetQString(excelRow[idMapping.Column].Value) != string.Empty){
         try{
             //find existing object
             Cell val = excelRow[idMapping.Column];
             var gwid = new Guid(ImportUtils.GetQString(val.Value));
             newObj =
                 objectSpace.FindObject(type, new BinaryOperator(keyPropertyName, gwid), true) as IXPSimpleObject;
         }
         catch{
         }
     }
     return newObj ?? (objectSpace.CreateObject(type) as IXPSimpleObject);
 }
        private void ProcessSingleRow(XPObjectSpace objectSpace, DoWorkEventArgs e, Type type, string keyPropertyName,
            Row excelRow, List<XPMemberInfo> props, int i, out string message){
            IXPSimpleObject newObj = GetExistingOrCreateNewObject(objectSpace, keyPropertyName, excelRow, type);

            if (newObj == null){
                message = string.Format(Resources.newObjectError, i);
                return;
            }
            foreach (Mapping mapping in ImportMap.Mappings){
                if (_bgWorker.CancellationPending){
                    e.Cancel = true;
                    break;
                }
                Application.DoEvents();

                XPMemberInfo prop = props.Single(p => p.Name == mapping.MapedTo);

                try{
                    Cell val = excelRow[mapping.Column];

                    if (val != null)
                        _propertyValueMapper(objectSpace, prop, val.Value, ref newObj);
                }

                catch (Exception ee){
                    message = string.Format(Resources.ErrorProcessingRecord,
                        i - 1, ee);
                    _bgWorker.ReportProgress(0, message);
                }

                if (CurrentCollectionSource != null)
                    AddNewObjectToCollectionSource(CurrentCollectionSource, newObj, ObjectSpace);
            }

            objectSpace.Session.Save(newObj);
            message = string.Format(Resources.SuccessProcessingRecord, i - 1);
        }
Example #3
0
        private static Cell GetCell(Row row, DocumentFormat.OpenXml.Spreadsheet.Cell p){
            return new Cell {
                OXmlCell = p,
                Row = row,
                Column = row.Sheet.Columns().First(t 
                    => t.ColumnIndex ==ColumnAddressToIndex(SplitAddress(p.CellReference)[0])),
                Value = p.CellValue != null ? p.CellValue.Text : p.InnerText

            };
        }