Write quotes round all fields except first (heading) row





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















CsvHelper has this very handy flag to set "" round all fields (a common requirement in CSV files):



csv.Configuration.QuoteAllFields = true;


However, this also places " round the first row, which contains field names.



How can "" be put round all fields except the first row containing field names?



I can find nothing in Google










share|improve this question

























  • Why do you want to?

    – Caius Jard
    Nov 24 '18 at 17:34











  • Client requirement

    – niico
    Nov 24 '18 at 17:40











  • Show the entire code you use to write the file

    – Caius Jard
    Nov 24 '18 at 20:24


















0















CsvHelper has this very handy flag to set "" round all fields (a common requirement in CSV files):



csv.Configuration.QuoteAllFields = true;


However, this also places " round the first row, which contains field names.



How can "" be put round all fields except the first row containing field names?



I can find nothing in Google










share|improve this question

























  • Why do you want to?

    – Caius Jard
    Nov 24 '18 at 17:34











  • Client requirement

    – niico
    Nov 24 '18 at 17:40











  • Show the entire code you use to write the file

    – Caius Jard
    Nov 24 '18 at 20:24














0












0








0


0






CsvHelper has this very handy flag to set "" round all fields (a common requirement in CSV files):



csv.Configuration.QuoteAllFields = true;


However, this also places " round the first row, which contains field names.



How can "" be put round all fields except the first row containing field names?



I can find nothing in Google










share|improve this question
















CsvHelper has this very handy flag to set "" round all fields (a common requirement in CSV files):



csv.Configuration.QuoteAllFields = true;


However, this also places " round the first row, which contains field names.



How can "" be put round all fields except the first row containing field names?



I can find nothing in Google







c# csvhelper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:44









Nkosi

121k17143207




121k17143207










asked Nov 24 '18 at 17:32









niiconiico

3,934105397




3,934105397













  • Why do you want to?

    – Caius Jard
    Nov 24 '18 at 17:34











  • Client requirement

    – niico
    Nov 24 '18 at 17:40











  • Show the entire code you use to write the file

    – Caius Jard
    Nov 24 '18 at 20:24



















  • Why do you want to?

    – Caius Jard
    Nov 24 '18 at 17:34











  • Client requirement

    – niico
    Nov 24 '18 at 17:40











  • Show the entire code you use to write the file

    – Caius Jard
    Nov 24 '18 at 20:24

















Why do you want to?

– Caius Jard
Nov 24 '18 at 17:34





Why do you want to?

– Caius Jard
Nov 24 '18 at 17:34













Client requirement

– niico
Nov 24 '18 at 17:40





Client requirement

– niico
Nov 24 '18 at 17:40













Show the entire code you use to write the file

– Caius Jard
Nov 24 '18 at 20:24





Show the entire code you use to write the file

– Caius Jard
Nov 24 '18 at 20:24












2 Answers
2






active

oldest

votes


















1














Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time



As such I recommend you do something like this:



var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);


Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.



The important part I'm aiming to outline is that you should




  • turn quotes off in the config,

  • write the headers,

  • turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)

  • write the data records






share|improve this answer


























  • QuoteAllFields appears to have stopped working in version 2? It's no longer available.

    – niico
    Dec 20 '18 at 19:09






  • 1





    Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

    – Caius Jard
    Dec 20 '18 at 19:28






  • 1





    A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

    – Caius Jard
    Dec 20 '18 at 19:32













  • This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

    – niico
    Jan 20 at 16:49





















0














You could use something like this



cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;


or



cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;


But you'll need to test and adjust as needed.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460714%2fwrite-quotes-round-all-fields-except-first-heading-row%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time



    As such I recommend you do something like this:



    var cw = new CsvWriter(yourTextWriterOrWhatever);
    cw.Configuration.QuoteNoFields = true;
    cw.WriteHeader<YourClassNameHere>();
    cw.NextRecord(); // without this first row is on same line as header
    cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
    cw.WriteRecords(yourCollectionOfYourClass);


    Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.



    The important part I'm aiming to outline is that you should




    • turn quotes off in the config,

    • write the headers,

    • turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)

    • write the data records






    share|improve this answer


























    • QuoteAllFields appears to have stopped working in version 2? It's no longer available.

      – niico
      Dec 20 '18 at 19:09






    • 1





      Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

      – Caius Jard
      Dec 20 '18 at 19:28






    • 1





      A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

      – Caius Jard
      Dec 20 '18 at 19:32













    • This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

      – niico
      Jan 20 at 16:49


















    1














    Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time



    As such I recommend you do something like this:



    var cw = new CsvWriter(yourTextWriterOrWhatever);
    cw.Configuration.QuoteNoFields = true;
    cw.WriteHeader<YourClassNameHere>();
    cw.NextRecord(); // without this first row is on same line as header
    cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
    cw.WriteRecords(yourCollectionOfYourClass);


    Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.



    The important part I'm aiming to outline is that you should




    • turn quotes off in the config,

    • write the headers,

    • turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)

    • write the data records






    share|improve this answer


























    • QuoteAllFields appears to have stopped working in version 2? It's no longer available.

      – niico
      Dec 20 '18 at 19:09






    • 1





      Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

      – Caius Jard
      Dec 20 '18 at 19:28






    • 1





      A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

      – Caius Jard
      Dec 20 '18 at 19:32













    • This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

      – niico
      Jan 20 at 16:49
















    1












    1








    1







    Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time



    As such I recommend you do something like this:



    var cw = new CsvWriter(yourTextWriterOrWhatever);
    cw.Configuration.QuoteNoFields = true;
    cw.WriteHeader<YourClassNameHere>();
    cw.NextRecord(); // without this first row is on same line as header
    cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
    cw.WriteRecords(yourCollectionOfYourClass);


    Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.



    The important part I'm aiming to outline is that you should




    • turn quotes off in the config,

    • write the headers,

    • turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)

    • write the data records






    share|improve this answer















    Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time



    As such I recommend you do something like this:



    var cw = new CsvWriter(yourTextWriterOrWhatever);
    cw.Configuration.QuoteNoFields = true;
    cw.WriteHeader<YourClassNameHere>();
    cw.NextRecord(); // without this first row is on same line as header
    cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
    cw.WriteRecords(yourCollectionOfYourClass);


    Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.



    The important part I'm aiming to outline is that you should




    • turn quotes off in the config,

    • write the headers,

    • turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)

    • write the data records







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 20 at 16:54









    niico

    3,934105397




    3,934105397










    answered Nov 24 '18 at 20:54









    Caius JardCaius Jard

    12.7k21440




    12.7k21440













    • QuoteAllFields appears to have stopped working in version 2? It's no longer available.

      – niico
      Dec 20 '18 at 19:09






    • 1





      Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

      – Caius Jard
      Dec 20 '18 at 19:28






    • 1





      A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

      – Caius Jard
      Dec 20 '18 at 19:32













    • This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

      – niico
      Jan 20 at 16:49





















    • QuoteAllFields appears to have stopped working in version 2? It's no longer available.

      – niico
      Dec 20 '18 at 19:09






    • 1





      Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

      – Caius Jard
      Dec 20 '18 at 19:28






    • 1





      A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

      – Caius Jard
      Dec 20 '18 at 19:32













    • This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

      – niico
      Jan 20 at 16:49



















    QuoteAllFields appears to have stopped working in version 2? It's no longer available.

    – niico
    Dec 20 '18 at 19:09





    QuoteAllFields appears to have stopped working in version 2? It's no longer available.

    – niico
    Dec 20 '18 at 19:09




    1




    1





    Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

    – Caius Jard
    Dec 20 '18 at 19:28





    Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?

    – Caius Jard
    Dec 20 '18 at 19:28




    1




    1





    A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

    – Caius Jard
    Dec 20 '18 at 19:32







    A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…

    – Caius Jard
    Dec 20 '18 at 19:32















    This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

    – niico
    Jan 20 at 16:49







    This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.

    – niico
    Jan 20 at 16:49















    0














    You could use something like this



    cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;


    or



    cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;


    But you'll need to test and adjust as needed.






    share|improve this answer




























      0














      You could use something like this



      cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;


      or



      cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;


      But you'll need to test and adjust as needed.






      share|improve this answer


























        0












        0








        0







        You could use something like this



        cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;


        or



        cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;


        But you'll need to test and adjust as needed.






        share|improve this answer













        You could use something like this



        cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;


        or



        cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;


        But you'll need to test and adjust as needed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 20:51









        camilohecamilohe

        441512




        441512






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460714%2fwrite-quotes-round-all-fields-except-first-heading-row%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()