List <Tag> GetTagModelListByTagGroupId(ExcelTransactionExtended transaction, Dictionary <string, List <string> > tagGroupDict) { var tags = new List <Tag>(); if (tagGroupDict == null || tagGroupDict.Count == 0) { Console.WriteLine("New row's TagGroupId is set, but the dictionary contains no elements!"); } else if (!tagGroupDict.ContainsKey(transaction.TagGroupId)) { Console.WriteLine("The new row's TagGroupId was not found in the dictionary!"); } else // Has TagGroupId, create a TagModel list { var groupNames = tagGroupDict[transaction.TagGroupId]; foreach (var item in groupNames) { tags.Add(new Tag() { Id = this.newTagId--, Title = item }); } } return(tags.Count > 0 ? tags : null); }
List <Tag> GetTagModelList(ExcelTransactionExtended transaction) { var tags = new List <Tag>(); foreach (var item in transaction.TagNames) { tags.Add(new Tag() { Id = this.newTagId--, Title = item }); } return(tags.Count > 0 ? tags : null); }
Transaction ConvertToModel(ExcelTransactionExtended tr, Dictionary <string, List <string> > tagGroupDict) { List <Tag> tags; if (String.IsNullOrWhiteSpace(tr.TagGroupId)) { tags = this.GetTagModelList(tr); } else { tags = this.GetTagModelListByTagGroupId(tr, tagGroupDict); } return(new Transaction() { //Id = default(int), Account = tr.Account, AccountName = tr.AccountName, AccountingDate = tr.AccountingDate, Type = tr.Type, TransactionId = tr.TransactionId, PartnerAccount = tr.PartnerAccount, PartnerName = tr.PartnerName, Sum = (decimal?)tr.Sum, Message = tr.Message, Tags = tags, Currency = new Currency() { //Id = this.newTransactionId--, Name = tr.Currency, CreateDate = DateTime.Now, State = "T" }, CreateDate = DateTime.Now, State = "T" }); }
void ReadExcel(string path, ExcelSheet <T> excelSheet) { ISheet sheet; using (var stream = new FileStream(path, FileMode.Open)) { stream.Position = 0; if (Path.GetExtension(path) == ".xls") { HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } else { XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } if (excelSheet.IsHeaderEmpty()) { IRow headerRow = sheet.GetRow(0); this.AddHeaderColumns(excelSheet, headerRow); } int i = this.GetStartingIndexValue(sheet); while ((IsReadFromTheBeginning && i <= sheet.LastRowNum) || // from the beginning (!IsReadFromTheBeginning && i >= (sheet.FirstRowNum + 1))) // from end { IRow row = sheet.GetRow(i); if (row == null || row.Cells.All(d => d.CellType == CellType.Blank)) { i = this.GetNextIteration(i); continue; } T tr = new T(); if (row.GetCell(0) != null) { tr.AccountingDate = row.GetCell(0).DateCellValue; } if (row.GetCell(1) != null) { tr.TransactionId = row.GetCell(1).ToString().Trim(); } if (row.GetCell(2) != null) { tr.Type = row.GetCell(2).ToString().Trim(); } if (row.GetCell(3) != null) { tr.Account = row.GetCell(3).ToString().Trim(); } if (row.GetCell(4) != null) { tr.AccountName = row.GetCell(4).ToString().Trim(); } if (row.GetCell(5) != null) { tr.PartnerAccount = row.GetCell(5).ToString().Trim(); } if (row.GetCell(6) != null) { tr.PartnerName = row.GetCell(6).ToString().Trim(); } if (row.GetCell(7) != null) { tr.Sum = double.Parse(row.GetCell(7).ToString()); } if (row.GetCell(8) != null) { tr.Currency = row.GetCell(8).ToString().Trim(); } if (row.GetCell(9) != null) { tr.Message = row.GetCell(9).ToString().Trim(); } if (tr is ExcelTransactionExtended) { ExcelTransactionExtended cast = tr as ExcelTransactionExtended; if (row.GetCell(10) != null) { cast.IsOmitted = row.GetCell(10).ToString().Trim() == "1"; } if (row.GetCell(11) != null) { cast.GroupId = row.GetCell(11).ToString().Trim(); } if (row.GetCell(12) != null) { cast.TagNames = this.GetIntList(row.GetCell(12).ToString().Trim()); } if (row.GetCell(13) != null) { cast.TagGroupId = row.GetCell(13).ToString().Trim(); } } excelSheet.AddNewRow(tr); i = this.GetNextIteration(i); } } }