Text qualifier for csv export

Hi,
How do we add a text qualifier for csv exports? For example we have an address field that contains a comma which we want to wrap with quotes:
123, any street should be "123, any street"

You can add ExportController to your application ignore list and apply your modifications.

1 Like

Hi,
Is it likely that the ability to change text qualifiers and seprators could be released in Radzen without having to manually update the controller?

Hi @simon,

At the moment the best way will be to manually modify the controller depending on your needs. We intentionally generate this class to be part of the application (not included in Radzen.Blazor for example) in order to be easily customizable.

For anyone else that comes across this issue, here is how I modified ExportController.cs. (change is within the comments).

        public FileStreamResult ToCSV(IQueryable query, string fileName = null)
        {
            var columns = GetProperties(query.ElementType);

            var sb = new StringBuilder();

            foreach (var item in query)
            {
                var row = new List<string>();

                foreach (var column in columns)
                {
                    // ******************************************************
                    // CHECK IF COLUMN TYPE IS STRING. WRAP IN QUOTES IF TRUE

                    if(column.Value == typeof(string))
                    {
                        row.Add($"\"{GetValue(item, column.Key)}\"".Trim());
                    }
                    else
                    {
                        row.Add($"{GetValue(item, column.Key)}".Trim());
                    }

                    // ******************************************************
                }

                sb.AppendLine(string.Join(",", row.ToArray()));
            }


            var result = new FileStreamResult(new MemoryStream(UTF8Encoding.Default.GetBytes($"{string.Join(",", columns.Select(c => c.Key))}{System.Environment.NewLine}{sb.ToString()}")), "text/csv");
            result.FileDownloadName = (!string.IsNullOrEmpty(fileName) ? fileName : "Export") + ".csv";

            return result;
        }
1 Like