private static string JsonToCsvString(string json, CsvConfiguration config, CreateOption option) { List <Dictionary <string, string> > data = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(json); using (var csvString = new StringWriter()) using (var csv = new CsvWriter(csvString, config)) { //Write the header row if (config.HasHeaderRecord && data.Any()) { foreach (var column in data.First().Keys) { csv.WriteField(column); } csv.NextRecord(); } foreach (var row in data) { foreach (var cell in row) { csv.WriteField(cell.Value ?? option.ReplaceNullsWith); } csv.NextRecord(); } return(csvString.ToString()); } }
/// <summary> /// Create a csv string from object or from a json string. See https://github.com/FrendsPlatform/Frends.Csv /// </summary> /// <returns>Object { string Csv } </returns> public static CreateResult Create([PropertyTab] CreateInput input, [PropertyTab] CreateOption option) { var config = new Configuration() { Delimiter = input.Delimiter, HasHeaderRecord = option.IncludeHeaderRow, CultureInfo = new CultureInfo(option.CultureInfo), IgnoreQuotes = option.NeverAddQuotesAroundValues }; if (option.NeverAddQuotesAroundValues) { // if IgnoreQuotes is true, seems like ShouldQuote function has to return false in all cases // if IgnoreQuotes is false ShouldQuote can't have any implementation otherwise it will overwrite IgnoreQuotes statement ( might turn it on again) config.ShouldQuote = (field, context) => (!option.NeverAddQuotesAroundValues); } var csv = string.Empty; switch (input.InputType) { case CreateInputType.List: csv = ListToCsvString(input.Data, input.Headers, config, option); break; case CreateInputType.Json: csv = JsonToCsvString(input.Json, config, option); break; } return(new CreateResult(csv)); }
/// <summary> /// Create a csv string from object or from a json string. See https://github.com/FrendsPlatform/Frends.Csv /// </summary> /// <returns>Object { string Csv } </returns> public static CreateResult Create([PropertyTab] CreateInput input, [PropertyTab] CreateOption option) { var config = new CsvConfiguration() { Delimiter = input.Delimiter, HasHeaderRecord = option.IncludeHeaderRow, CultureInfo = new CultureInfo(option.CultureInfo) }; var csv = ""; switch (input.InputType) { case CreateInputType.List: csv = ListToCsvString(input.Data, input.Headers, config); break; case CreateInputType.Json: csv = JsonToCsvString(input.Json, config); break; } return(new CreateResult(csv)); }
private static string ListToCsvString(List <List <object> > inputData, List <string> inputHeaders, CsvConfiguration config, CreateOption option) { using (var csvString = new StringWriter()) using (var csv = new CsvWriter(csvString, config)) { //Write the header row if (config.HasHeaderRecord && inputData.Any()) { foreach (var column in inputHeaders) { csv.WriteField(column); } csv.NextRecord(); } foreach (var row in inputData) { foreach (var cell in row) { csv.WriteField(cell ?? option.ReplaceNullsWith); } csv.NextRecord(); } return(csvString.ToString()); } }