Esempio n. 1
0
        private static List <T> GetWorksheetData <T>(string fileName,
                                                     IEnumerable <ImportPropertyDescriptor <T> > mappedColumns) where T : new()
        {
            //Check.Argument.IsNotEmpty(fileName, "fileName");

            var items = new List <T>();

            using (var excelHandler = ExcelHandlerFactory.Instance.Create(fileName))
            {
                IExcelSheet sheet   = excelHandler.GetSheet(1);
                var         headers = ExcelUtility.GetWorksheetColumns(sheet);

                int       colCount          = headers.Count();
                int       rowCount          = 1000 * 1000; // TODO: !!! ws.Cells.UsedRange.RowCount;
                const int DataRowStartIndex = 2;           // TODO: constant!!
                for (int rowIndex = DataRowStartIndex; rowIndex <= rowCount; rowIndex++)
                {
                    var  item = new T();
                    bool hasAnyNonEmptyValue = false;

                    foreach (var mappedColumn in mappedColumns)
                    {
                        string value = sheet.GetCellValue(rowIndex, ExcelUtility.GetColNameFromIndex(mappedColumn.ExcelFileColumnIndex)).ToString().Trim();
                        if (value.Length > 0)
                        {
                            hasAnyNonEmptyValue = true;
                            mappedColumn.SetValue(item, value);
                        }
                    }

                    if (!hasAnyNonEmptyValue)
                    {
                        break;
                    }

                    items.Add(item);
                }
            }

            return(items);
        }