Skip to content
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

Closed
RizwanAhmedJutt opened this issue Nov 16, 2017 · 7 comments
Closed

HasExcelSeparator is missing #847

RizwanAhmedJutt opened this issue Nov 16, 2017 · 7 comments

Comments

@RizwanAhmedJutt
Copy link

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.

@JoshClose
Copy link
Owner

All the Excel specific stuff was removed, so HasExcelSeparator doesn't exist anymore. Excel stuff will need to come from a contrib library or another place.

TrimFields is now done using TrimOptions.

@steven-dawkins
Copy link

Does this mean that you can no longer read csv files with an excel seperator line?

@helmerm
Copy link

helmerm commented Feb 19, 2020

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?

@JoshClose
Copy link
Owner

sep=, would imply Excel wrote the file.

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; }
}

@helmerm
Copy link

helmerm commented Feb 19, 2020

Thank you!

@Meligy
Copy link

Meligy commented Apr 16, 2024

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.

@JoshClose
Copy link
Owner

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants