示例#1
0
        /// <summary>
        /// Conversion from Entry to string seq seq
        /// Contrary function of LoadContentToFile()
        /// </summary>
        /// <returns></returns>
        private IEnumerable <IEnumerable <string> > SaveFileToContent()
        {
            var titles = Titles;

            if (_titleRules != null)
            {
                _titleRules(ref titles);
            }

            var content = new List <List <string> > {
                titles
            };

            foreach (var aEntry in Content)
            {
                var newEntry = aEntry;
                if (_entryRules != null)
                {
                    _entryRules(ref newEntry);
                }
                var newContentLine = new List <string>();
                foreach (var propName in Titles.Select(
                             title => (from prop in EntryType.GetProperties()
                                       from attr in prop.GetCustomAttributes(typeof(TitleName), false)
                                       where ((TitleName)attr).Name == title
                                       select prop.Name).FirstOrDefault()))
                {
                    if (propName != null)
                    {
                        var contentField = EntryType.GetProperty(propName).GetValue(newEntry, null);
                        newContentLine.Add(contentField.ToString());
                    }
                    else
                    {
                        newContentLine.Add("");
                    }
                }
                content.Add(newContentLine);
            }
            return(content);
        }
示例#2
0
        public IEnumerable <T> ToList()
        {
            _viewConfigure   = ServiceLocator.GetViewConfigure(EntryType);
            _entryProperites = EntryType.GetProperties();
            SpreadsheetDocument doc     = null;
            List <T>            results = new List <T>();

            try
            {
                doc = SpreadsheetDocument.Open(_excelStream, false);
            }
            catch
            {
                ErrorMessages.Add("Only support .xlsx");
            }
            if (doc != null)
            {
                using (doc)
                {
                    _workBookPart = doc.WorkbookPart;
                    Sheet     mysheet   = (Sheet)doc.WorkbookPart.Workbook.Sheets.ChildElements.FirstOrDefault();
                    Worksheet worksheet = ((WorksheetPart)_workBookPart.GetPartById(mysheet.Id)).Worksheet;
                    SheetData sheetData = null;
                    foreach (var item in worksheet.ChildElements)
                    {
                        if (item is SheetData)
                        {
                            sheetData = (SheetData)item;
                            break;
                        }
                    }
                    if (sheetData != null && sheetData.ChildElements.Any())
                    {
                        List <string> header    = new List <string>();
                        var           headerRow = sheetData.ChildElements.First() as Row;
                        foreach (var cell in headerRow.ChildElements)
                        {
                            var cellItem = cell as Cell;
                            header.Add(ReadCellValue(cellItem));
                        }

                        for (int i = 1; i < sheetData.ChildElements.Count; i++)
                        {
                            T item = new T();
                            try
                            {
                                for (int j = 0; j < sheetData.ChildElements[i].ChildElements.Count; j++)
                                {
                                    if (j < header.Count)
                                    {
                                        CellConvert(item, header[j], sheetData.ChildElements[i].ChildElements[j] as Cell);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                ErrorMessages.Add(ex.Message);
                                break;
                            }
                            yield return(item);
                        }
                    }
                }
            }
        }