/// <summary> /// Convert <paramref name="dataTable"/> to <see cref="JArray"/> using <paramref name="mappingTemplate"/>. /// </summary> /// <returns>The jarray.</returns> /// <param name="dataTable">Data table.</param> /// <param name="mappingTemplate">Mapping template.</param> public static JArray ToJarray(this DataTable dataTable, IMappingTemplate mappingTemplate) { if (dataTable is null) { throw new NullReferenceException(nameof(dataTable)); } if (mappingTemplate is null) { throw new ArgumentNullException(nameof(mappingTemplate)); } var jArray = new JArray(); foreach (DataRow row in dataTable.Rows) { var jObject = new JObject(); int colIndex = 0; foreach (DataColumn column in dataTable.Columns) { IColumnMap columnMap = null; if (mappingTemplate.UseColumnIndex) { columnMap = mappingTemplate.GetMap(columnIndex: colIndex); } else { columnMap = mappingTemplate.GetMap(columnName: column.ColumnName); } if (columnMap is null) { continue; } var value = string.Empty; if (columnMap.NormalizeValue) { value = row[column].ToString().Trim(); } else { value = row[column].ToString(); } jObject.Add(new JProperty(columnMap.PropertyName, row[column].ToString())); colIndex++; } colIndex = 0; jArray.Add(jObject); } return(jArray); }
public IExcelMapper AddTemplate(IMappingTemplate template) { if (template is null) { throw new ArgumentNullException(nameof(template)); } if (!_mappingTemplates.Contains(template)) { _mappingTemplates.Add(template); } return(this); }