-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HasExcelSeparator is missing #847
Comments
All the Excel specific stuff was removed, so
|
Does this mean that you can no longer read csv files with an excel seperator line? |
So, if this is not supported anymore, is there anywhere an example how to achieve compatibility with the most used CSV viewer on this world? |
You can add the functionality back in with 1 line of code. csv.Configuration.Delimiter = Regex.Match(reader.ReadLine(), "sep=(.*)").Groups[1].Value; Full example: void Main()
{
var s = new StringBuilder();
s.AppendLine("sep=,");
s.AppendLine("Id,Name");
s.AppendLine("1,one");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.Delimiter = Regex.Match(reader.ReadLine(), "sep=(.*)").Groups[1].Value;
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
} |
Thank you! |
Sorry for jumping in quite late, but, how does this work for a writer instead of a reader? I'm migrating some old code that has: public class CsvResult : IActionResult
{
private static readonly CsvConfiguration CsvConfiguration;
private readonly string _fileName;
private readonly Array _data;
static CsvResult()
{
CsvConfiguration = new CsvConfiguration()
{
HasExcelSeparator = true,
HasHeaderRecord = true
};
CsvConfiguration.RegisterClassMap<SomethingCsvMapper>();
}
public CsvResult(string fileName, Array data)
{
_fileName = fileName;
_data = data;
}
public Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
response.Headers.Add("Content-Disposition", $"attachment; filename={_fileName}");
response.ContentType = "text/csv";
using (var writer = new CsvWriter(new StreamWriter(response.Body), CsvConfiguration))
{
writer.WriteRecords(_data);
}
return Task.CompletedTask;
}
} Thanks. |
You can just write that line to the stream directly. using var writer = new StreamWriter(response.body);
using var csvWriter = new CsvWriter(writer, CsvConfiguration);
writer.WriteLine($"sep={config.Delimiter}");
csvWriter.WriteRecords(data); |
i am using csvHelper version 6.0.0 , i could not found 'HasExcelSeparator','TrimFields' properties which are accessable in 'configuration' Class.how can i access these above mentioned properties .Got Stuck..Thanks in Advance.
The text was updated successfully, but these errors were encountered: