Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

608
Visualizações
.Net web api download csv file (Excel separator)

I have a controller endpoint from which I want to generate a csv file and download it.

Currently I am using nuget CsvHelper and my code is like this:

var cc = new CsvConfiguration(new System.Globalization.CultureInfo("sl-SI"));
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(stream: ms, encoding: new UTF8Encoding(true)))
{
    using (var cw = new CsvWriter(sw, cc))
    {
        cw.WriteRecords(ListOfReports);
    }// The stream gets flushed here.
    return File(ms.ToArray(), "text/csv", $"{docNumber.Trim()}_{docType}.csv");
}
}

It generated csv pretty nice, but the problem was, if I opened it in Excel, whole row was in the first column and was not splitted.

I added this part:

cw.WriteField("sep=,", false);
cw.NextRecord();

Before cw.WriteRecords(ListOfReports);, which made it work in Excel, but if I open it in Notepad, there is a sep=, in my first row.

I noticed there is a difference in CultureInfo, If I set "sl-SI" it will work properly on Slovenian windows (separator will be ;), if I set "en-US" it will work on English Windows (separator ,). But what do i need to do to work on any Culture?

Does anyone has any idea how to fix this so it will work properly in Excel and any other text editor?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is effectively the same as this SuperUser question, but it appears you want a programming-oriented solution rather than a user-oriented one.

The problem is fundamentally that Excel is really bad at dealing with CSV files, especially when taking non-US cultures into account. My suggestion would be to allow users to download a real Excel file using a library like DocumentFormat.OpenXml.

If you have two separate use cases for your downloads (i.e. some users who open the file in notepad or consume it with software that reads CSV, and others who open the file in Excel), give the users separate options to download in CSV or Excel.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda