/// <summary> /// Converts the headers and values into a dynamic object. /// </summary> /// <param name="headers">The headers.</param> /// <param name="values">The values.</param> /// <param name="rowIndex">The rowIndex.</param> /// <returns>dynamic.</returns> /// <exception cref="System.ArgumentNullException"> /// headers /// or /// values /// </exception> public static dynamic RowToExpando(IDictionary <string, string> headers, IDictionary <string, string> values, int rowIndex = -1) { if (headers == null) { throw new ArgumentNullException("headers"); } if (values == null) { throw new ArgumentNullException("values"); } //As not every excel cell may have a value, we have to emit only those cells whose key match up to the headers CustomExpandoObject expandoObject = new CustomExpandoObject(true); foreach (string key in headers.Keys) { if (values.ContainsKey(key)) { if (!expandoObject.ContainsKey(headers[key])) { expandoObject.Add(headers[key], values[key]); } else { expandoObject.Add(key + "_" + headers[key], values[key]); } } else { if (expandoObject.ContainsKey(headers[key])) { throw new DuplicateColumnNameException(string.Format("Column '{0}' found multiple times in the excel file.", headers[key])); } expandoObject.Add(headers[key], String.Empty); } } if (rowIndex >= 0) { expandoObject.Add("__RowIndex", rowIndex); } return(expandoObject); }