Beispiel #1
0
        /// <summary>
        /// Read Excel document by SheetIndex
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <param name="sheetIndex">Zero based index sheet</param>
        /// <returns></returns>
        public IEnumerable <TObject> ReadExcelDocument <TObject>(int sheetIndex, bool usingPropertyAttribute = false) where TObject : class
        {
            List <string[]> propValue  = null;
            var             listReturn = new List <TObject>();

            var sheet            = Sheets.Take(sheetIndex + 1).Last();
            var openXmlExcelData = OpenXmlReader.ReadExcel(sheet);

            foreach (var row in openXmlExcelData.DataRows)
            {
                propValue = new List <string[]>();

                for (int i = 0; i < row.Count; i++)
                {
                    if (i < openXmlExcelData.Headers.Count)
                    {
                        propValue.Add(new[] { openXmlExcelData.Headers[i].Replace(" ", ""), row[i] });
                    }
                }

                if (row.Count() > 0 && !row.All(r => string.IsNullOrEmpty(r)))
                {
                    var TObjectInstance = Activator.CreateInstance <TObject>();
                    ObjectParser.FillInstance(TObjectInstance, propValue);
                    listReturn.Add(TObjectInstance);
                }
            }
            return(listReturn);
        }
Beispiel #2
0
        public IEnumerable <TObject> ReadExcelDocument <TObject>(string sheetName) where TObject : class
        {
            List <string[]> propValue  = null;
            var             listReturn = new List <TObject>();

            var sheet            = Sheets.FirstOrDefault(c => c.Name == sheetName);
            var openXmlExcelData = OpenXmlReader.ReadExcel(sheet);

            foreach (var row in openXmlExcelData.DataRows)
            {
                propValue = new List <string[]>();

                for (int i = 0; i < row.Count; i++)
                {
                    if (i < openXmlExcelData.Headers.Count)
                    {
                        propValue.Add(new[] { openXmlExcelData.Headers[i].Replace(" ", ""), row[i] });
                    }
                }

                if (row.Count() > 0)
                {
                    var TObjectInstance = Activator.CreateInstance <TObject>();
                    ObjectParser.FillInstance(TObjectInstance, propValue);
                    listReturn.Add(TObjectInstance);
                }
            }
            return(listReturn);
        }