예제 #1
0
        /// <summary>
        /// 处理一列信息
        /// </summary>
        private ProcessCellDataException ProcessCellData(T item, DataRowWrapper dataWrapper, ReadExcelColumn <T> column)
        {
            ProcessCellDataException error = null;

            if (column.CustomDelegate != null)
            {//自定义获取信息,优先级最高
                error = WrapperException(String.Format("读取第 [{0}] 行信息错误。", dataWrapper.RowIndex + 1), () => column.CustomDelegate(item, dataWrapper));
            }
            else
            {
                String data = null;
                if (!String.IsNullOrEmpty(column.ColumnName))
                {//信息列名称获取信息优先级大于信息索引
                    error = WrapperException(String.Format("读取第 [{0}] 行,[{1}] 列单元格信息错误。", dataWrapper.RowIndex + 1, column.ColumnName),
                                             () => data = dataWrapper[column.ColumnName]);
                    if (null == error)
                    {//转换信息
                        error = WrapperException(String.Format("读取第 [{0}] 行,[{1}] 列单元格信息错误。", dataWrapper.RowIndex + 1, column.ColumnName),
                                                 () => column.CustomEvaluater(item, data));
                    }
                }
                else
                {
                    error = WrapperException(String.Format("读取第 [{0}] 行,第 [{1}] 列单元格信息错误。", dataWrapper.RowIndex + 1, column.ColumnIndex + 1),
                                             () => data = dataWrapper[column.ColumnIndex]);
                    if (null == error)
                    {//转换信息
                        error = WrapperException(String.Format("读取第 [{0}] 行,第 [{1}] 列单元格信息错误。", dataWrapper.RowIndex + 1, column.ColumnIndex + 1),
                                                 () => column.CustomEvaluater(item, data));
                    }
                }
            }

            return(error);
        }
예제 #2
0
        public List <T> ReadDataTable(DataTable sheetTable, ReadExcelColumnBuilder <T> columns, Func <T> createInstance)
        {
            if (sheetTable == null)
            {
                throw new SheetTableNullException("工作表中不存在数据");
            }

            //校验信息列
            this.ValidateColumns(columns, sheetTable, "");
            List <T> items = new List <T>(sheetTable.Rows.Count);

            //循环读取每行Excel信息
            for (Int32 rowIndex = 0; rowIndex < sheetTable.Rows.Count; rowIndex++)
            {
                DataRow row  = sheetTable.Rows[rowIndex];
                T       item = this.CreateInstance(createInstance);

                //该行数据为空
                if (row.ItemArray.Where(e => null == e).Count() == row.ItemArray.Length)
                {
                    continue;
                }

                using (DataRowWrapper dataWrapper = new DataRowWrapper(row, rowIndex))
                {
                    foreach (var column in columns)
                    {
                        ProcessCellDataException error = this.ProcessCellData(item, dataWrapper, column);
                        if (null != error)
                        {//保存错误信息
                            throw error;
                        }
                    }
                }

                items.Add(item);
            }
            return(items);
        }