Exemplo n.º 1
0
        /// <summary>
        /// Finds and reads a sheet at the given zero-based index in the document.
        /// </summary>
        /// <param name="sheetIndex">The zero-based index of the sheet to read.</param>
        /// <param name="sheet">The sheet in the document at the given zero-based index.</param>
        /// <returns>True if the sheet was found, else false.</returns>
        public bool TryReadSheet(int sheetIndex, out ExcelSheet sheet)
        {
            sheet = null;

            if (sheetIndex < 0 || sheetIndex > NumberOfSheets - 1)
            {
                return(false);
            }

            ResetReader();
            for (int i = 0; i < sheetIndex; i++)
            {
                Reader.NextResult();
            }

            sheet = new ExcelSheet(Reader, sheetIndex, this);
            ResetReader();
            return(true);
        }
Exemplo n.º 2
0
        private static string GetMessage(string message, ExcelSheet sheet, int rowIndex, int columnIndex)
        {
            string position;

            if (sheet != null && sheet.HasHeading)
            {
                if (sheet.Heading == null)
                {
                    sheet.ReadHeading();
                }

                position = $"\"{sheet.Heading.GetColumnName(columnIndex)}\"";
            }
            else
            {
                position = $"in position \"{columnIndex}\"";
            }


            return($"{message} {position} on row {rowIndex} in sheet \"{sheet?.Name}\".");
        }
Exemplo n.º 3
0
        internal static object GetPropertyValue(IValuePipeline pipeline, ExcelSheet sheet, int rowIndex, IExcelDataReader reader, ReadCellValueResult readResult, MemberInfo member)
        {
            foreach (ICellValueTransformer transformer in pipeline.CellValueTransformers)
            {
                readResult = new ReadCellValueResult(readResult.ColumnIndex, transformer.TransformStringValue(sheet, rowIndex, readResult));
            }

            if (string.IsNullOrEmpty(readResult.StringValue) && pipeline.EmptyFallback != null)
            {
                return(pipeline.EmptyFallback.PerformFallback(sheet, rowIndex, readResult, member));
            }

            PropertyMapperResultType resultType = PropertyMapperResultType.Success;
            object value = null;

            foreach (ICellValueMapper mappingItem in pipeline.CellValueMappers)
            {
                PropertyMapperResultType newResultType = mappingItem.MapCellValue(readResult, ref value);
                if (newResultType == PropertyMapperResultType.Success)
                {
                    return(value);
                }

                if (newResultType != PropertyMapperResultType.Continue)
                {
                    resultType = newResultType;
                }
            }

            if (resultType != PropertyMapperResultType.Success && resultType != PropertyMapperResultType.SuccessIfNoOtherSuccess && pipeline.InvalidFallback != null)
            {
                return(pipeline.InvalidFallback.PerformFallback(sheet, rowIndex, readResult, member));
            }

            return(value);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates an ExcelMappingException throw trying to map a cell value to a property or field.
 /// </summary>
 /// <param name="message">The base error message of the exception.</param>
 /// <param name="sheet">The sheet that is currently being read.</param>
 /// <param name="rowIndex">The zero-based index of the row in the sheet that is currently being read.</param>
 /// <param name="columnIndex">The zero-based index of the column in the row that is currently being read.</param>
 public ExcelMappingException(string message, ExcelSheet sheet, int rowIndex, int columnIndex) : base(GetMessage(message, sheet, rowIndex, columnIndex))
 {
 }
Exemplo n.º 5
0
 /// <summary>
 /// Maps a row of a sheet to an object. The return value will be assigned to the property or field.
 /// </summary>
 /// <param name="sheet">The sheet containing the row that is being read.</param>
 /// <param name="rowIndex">The index of the row that is being read.</param>
 /// <param name="reader">The reader that allows access to the data of the document.</param>
 /// <returns>An object created from one or more cells in the row.</returns>
 public abstract object GetPropertyValue(ExcelSheet sheet, int rowIndex, IExcelDataReader reader);