Formatting a comma-delimited CSV to force Excel to interpret value as a string












50















I've been searching around for a while trying to figure out how to output a CSV file in such a way to force Excel to interpret the values as a string and not try to convert them to numbers or dates.



e.g.:



"141", "10/11/2002", "350.00", "1311742251"


Excel tries to "intelligently" convert all these to its native date/number formats. Is there a way around that?





EDIT: Clarified the intent of my question, sorry for confusion.










share|improve this question





























    50















    I've been searching around for a while trying to figure out how to output a CSV file in such a way to force Excel to interpret the values as a string and not try to convert them to numbers or dates.



    e.g.:



    "141", "10/11/2002", "350.00", "1311742251"


    Excel tries to "intelligently" convert all these to its native date/number formats. Is there a way around that?





    EDIT: Clarified the intent of my question, sorry for confusion.










    share|improve this question



























      50












      50








      50


      3






      I've been searching around for a while trying to figure out how to output a CSV file in such a way to force Excel to interpret the values as a string and not try to convert them to numbers or dates.



      e.g.:



      "141", "10/11/2002", "350.00", "1311742251"


      Excel tries to "intelligently" convert all these to its native date/number formats. Is there a way around that?





      EDIT: Clarified the intent of my question, sorry for confusion.










      share|improve this question
















      I've been searching around for a while trying to figure out how to output a CSV file in such a way to force Excel to interpret the values as a string and not try to convert them to numbers or dates.



      e.g.:



      "141", "10/11/2002", "350.00", "1311742251"


      Excel tries to "intelligently" convert all these to its native date/number formats. Is there a way around that?





      EDIT: Clarified the intent of my question, sorry for confusion.







      microsoft-excel formatting csv string text-formatting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 8 '17 at 2:35







      Simon East

















      asked Aug 3 '11 at 8:39









      Simon EastSimon East

      1,70532226




      1,70532226






















          5 Answers
          5






          active

          oldest

          votes


















          52














          For those that have control over the source data, apparently Excel will auto-detect the format of a CSV field unless the CSV column is in this format:



          "=""Data Here"""



          eg...



          20,       5.5%,      "0404 123 351", "3-6",  "=""123"""
          [number] [percent] [number] [date] [string] <-- how Excel interprets


          It also works in Google Spreadsheet, but not sure if other spreadsheet apps support this notation.



          If you suspect any the data may contain quotes itself, you need to double-escape them, like this...



          "=""She said """"Hello"""" to him"""





          (EDIT: Updated with corrections, thanks DMA57361!)






          share|improve this answer


























          • Awesome, we just need to change the data.. sigh

            – PriceChild
            Aug 3 '11 at 9:05






          • 4





            That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

            – DMA57361
            Aug 3 '11 at 9:18













          • @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

            – Breakthrough
            Aug 3 '11 at 10:43








          • 1





            @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

            – DMA57361
            Aug 3 '11 at 10:47








          • 2





            @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

            – Simon East
            Aug 8 '11 at 11:28





















          29














          Like many, I have been struggling with the same decisions that Microsoft makes and tried various suggested solutions.



          For Excel 2007 the following goes:




          • Putting all values in double quotes does NOT help

          • Putting an = before all values after putting them in double quutes DOES help, BUT makes the csv file
            useless for most other applications

          • Putting parentheses around the double quotes around all values is
            rubbish

          • Putting a space before all values before putting double quotes around
            them DOES prevent conversions to dates, but DOES NOT prevent trimming
            of leading or trailing zeroes.

          • Putting a single quote in front of a value only works when entering
            data within Excel.


          However:



          Putting a tab before all values before putting double quotes around them DOES prevent conversions to dates AND DOES prevent trimming of leading or trailing zeroes and the sheet does not even show nasty warning markers in the upper left corner of each cell.



          E.g.:



          "<tab character><some value>","<tab character><some other value>"


          Note that the tab character has to be within the double quotes.
          Edit: it turns out that the double quotes are not even necessary.



          Double clicking the csv file can open the file as a spreadsheet in Excel showing all values that are treated as just above, like text data.
          Make sure to set Excel to use the '.' as the decimal point and not the ',' or every line of the csv file will end up as one text in the first cell of each row.
          Apparently Microsoft thinks that CSV means "Not the decimal point" Separated Value.






          share|improve this answer


























          • This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

            – Markus Yrjölä
            Feb 3 '17 at 8:37



















          19














          Using Excel's import functionality allows you to specify the format (auto, text or date) each column should be interpreted as and does not require any modification to the data files.



          You can find it as DataGet External DataFrom Text in Excel 2007/2010.

          Or DataImport External DataImport Data in Excel 2003.



          Here's an image of the Excel 2003 Text Import Wizard in action on the example data given, showing me importing the latter two columns as text:



          Excel 2003: Text Import Wizard on Step 3 - data types






          share|improve this answer


























          • Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

            – Simon East
            Aug 8 '11 at 11:26











          • @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

            – DMA57361
            Aug 8 '11 at 11:27













          • it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

            – Simon East
            Aug 8 '11 at 11:34








          • 1





            There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

            – DMA57361
            Aug 8 '11 at 11:38













          • thats very helpful, after reading quite a few ansers

            – greg121
            Sep 5 '14 at 20:32



















          1














          The example from Simon did not work for me, and I suspect it is a language difference. In C# here is what my working format string looks like:



          var linebreak = (i++ == list.Count) ? "" : "rn";

          csv += String.Format("="{0}",{1},{2},{3},="{4}"{5}",
          item.Value, item.Status, item.NewStatus, item.Carrier, c.Status, linebreak);


          and this is what the output file looks like:



          ="abababababab",INVALID,INVALID,USPS,="",
          ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
          ="9400110200793000216184",UNKNOWNSTATUS,INVALID,USPS,=""


          As can be seen, the format in the output file is ="VALUE", not "=""VALUE""", which I believe may be a Visual Basic convention.



          I am using Excel 2010. Incidentally, Google Sheets will not open/convert a file formatted this way. It will work if you remove the equal sign thus "VALUE", - Excel will still open the file but ignore the fact that you want your columns to be strings.






          share|improve this answer

































            -2














            A simple way to force Excel to interpret the date as text is to put a single quote in front of the date, instead of using full quotes, as in:




            '10/11/2002




            If you can import the CSV instead of opening it, you can tell Excel what format each column should be. Have a look at this question I asked.






            share|improve this answer





















            • 4





              Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

              – psynnott
              Apr 14 '14 at 15:33











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f318420%2fformatting-a-comma-delimited-csv-to-force-excel-to-interpret-value-as-a-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            52














            For those that have control over the source data, apparently Excel will auto-detect the format of a CSV field unless the CSV column is in this format:



            "=""Data Here"""



            eg...



            20,       5.5%,      "0404 123 351", "3-6",  "=""123"""
            [number] [percent] [number] [date] [string] <-- how Excel interprets


            It also works in Google Spreadsheet, but not sure if other spreadsheet apps support this notation.



            If you suspect any the data may contain quotes itself, you need to double-escape them, like this...



            "=""She said """"Hello"""" to him"""





            (EDIT: Updated with corrections, thanks DMA57361!)






            share|improve this answer


























            • Awesome, we just need to change the data.. sigh

              – PriceChild
              Aug 3 '11 at 9:05






            • 4





              That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

              – DMA57361
              Aug 3 '11 at 9:18













            • @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

              – Breakthrough
              Aug 3 '11 at 10:43








            • 1





              @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

              – DMA57361
              Aug 3 '11 at 10:47








            • 2





              @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

              – Simon East
              Aug 8 '11 at 11:28


















            52














            For those that have control over the source data, apparently Excel will auto-detect the format of a CSV field unless the CSV column is in this format:



            "=""Data Here"""



            eg...



            20,       5.5%,      "0404 123 351", "3-6",  "=""123"""
            [number] [percent] [number] [date] [string] <-- how Excel interprets


            It also works in Google Spreadsheet, but not sure if other spreadsheet apps support this notation.



            If you suspect any the data may contain quotes itself, you need to double-escape them, like this...



            "=""She said """"Hello"""" to him"""





            (EDIT: Updated with corrections, thanks DMA57361!)






            share|improve this answer


























            • Awesome, we just need to change the data.. sigh

              – PriceChild
              Aug 3 '11 at 9:05






            • 4





              That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

              – DMA57361
              Aug 3 '11 at 9:18













            • @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

              – Breakthrough
              Aug 3 '11 at 10:43








            • 1





              @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

              – DMA57361
              Aug 3 '11 at 10:47








            • 2





              @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

              – Simon East
              Aug 8 '11 at 11:28
















            52












            52








            52







            For those that have control over the source data, apparently Excel will auto-detect the format of a CSV field unless the CSV column is in this format:



            "=""Data Here"""



            eg...



            20,       5.5%,      "0404 123 351", "3-6",  "=""123"""
            [number] [percent] [number] [date] [string] <-- how Excel interprets


            It also works in Google Spreadsheet, but not sure if other spreadsheet apps support this notation.



            If you suspect any the data may contain quotes itself, you need to double-escape them, like this...



            "=""She said """"Hello"""" to him"""





            (EDIT: Updated with corrections, thanks DMA57361!)






            share|improve this answer















            For those that have control over the source data, apparently Excel will auto-detect the format of a CSV field unless the CSV column is in this format:



            "=""Data Here"""



            eg...



            20,       5.5%,      "0404 123 351", "3-6",  "=""123"""
            [number] [percent] [number] [date] [string] <-- how Excel interprets


            It also works in Google Spreadsheet, but not sure if other spreadsheet apps support this notation.



            If you suspect any the data may contain quotes itself, you need to double-escape them, like this...



            "=""She said """"Hello"""" to him"""





            (EDIT: Updated with corrections, thanks DMA57361!)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '14 at 0:20

























            answered Aug 3 '11 at 8:39









            Simon EastSimon East

            1,70532226




            1,70532226













            • Awesome, we just need to change the data.. sigh

              – PriceChild
              Aug 3 '11 at 9:05






            • 4





              That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

              – DMA57361
              Aug 3 '11 at 9:18













            • @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

              – Breakthrough
              Aug 3 '11 at 10:43








            • 1





              @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

              – DMA57361
              Aug 3 '11 at 10:47








            • 2





              @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

              – Simon East
              Aug 8 '11 at 11:28





















            • Awesome, we just need to change the data.. sigh

              – PriceChild
              Aug 3 '11 at 9:05






            • 4





              That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

              – DMA57361
              Aug 3 '11 at 9:18













            • @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

              – Breakthrough
              Aug 3 '11 at 10:43








            • 1





              @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

              – DMA57361
              Aug 3 '11 at 10:47








            • 2





              @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

              – Simon East
              Aug 8 '11 at 11:28



















            Awesome, we just need to change the data.. sigh

            – PriceChild
            Aug 3 '11 at 9:05





            Awesome, we just need to change the data.. sigh

            – PriceChild
            Aug 3 '11 at 9:05




            4




            4





            That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

            – DMA57361
            Aug 3 '11 at 9:18







            That last column should be "=""123""" otherwise it's badly formed. Fields containing a " must be delimited and the "s in the field escaped with other "s.

            – DMA57361
            Aug 3 '11 at 9:18















            @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

            – Breakthrough
            Aug 3 '11 at 10:43







            @DMA57361 actually the way he has it is fine, it's the other two fields beside it that are missing the prepended equal sign. What he put there is setting that cell's formula to return a string. To also avoid this, you can set the cell's data type to "Text".

            – Breakthrough
            Aug 3 '11 at 10:43






            1




            1





            @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

            – DMA57361
            Aug 3 '11 at 10:47







            @Breakthrough that table there represents a CSV file, not Excel fields. The last value ="123" is not a valid CSV field because it contains the field delimiter character " without correctly delimiting it or the field. The fact Excel happens to read it as a formula is purely up to Excel and nothing to do with the CSV file.

            – DMA57361
            Aug 3 '11 at 10:47






            2




            2





            @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

            – Simon East
            Aug 8 '11 at 11:28







            @PriceChild, the point of my original question (that I didn't really explain very well) was actually how to format the CSV to make it as easy as possible for users. And so this is the answer I found myself and wanted to post. DMA57361 actually brought a helpful correction too, thanks!

            – Simon East
            Aug 8 '11 at 11:28















            29














            Like many, I have been struggling with the same decisions that Microsoft makes and tried various suggested solutions.



            For Excel 2007 the following goes:




            • Putting all values in double quotes does NOT help

            • Putting an = before all values after putting them in double quutes DOES help, BUT makes the csv file
              useless for most other applications

            • Putting parentheses around the double quotes around all values is
              rubbish

            • Putting a space before all values before putting double quotes around
              them DOES prevent conversions to dates, but DOES NOT prevent trimming
              of leading or trailing zeroes.

            • Putting a single quote in front of a value only works when entering
              data within Excel.


            However:



            Putting a tab before all values before putting double quotes around them DOES prevent conversions to dates AND DOES prevent trimming of leading or trailing zeroes and the sheet does not even show nasty warning markers in the upper left corner of each cell.



            E.g.:



            "<tab character><some value>","<tab character><some other value>"


            Note that the tab character has to be within the double quotes.
            Edit: it turns out that the double quotes are not even necessary.



            Double clicking the csv file can open the file as a spreadsheet in Excel showing all values that are treated as just above, like text data.
            Make sure to set Excel to use the '.' as the decimal point and not the ',' or every line of the csv file will end up as one text in the first cell of each row.
            Apparently Microsoft thinks that CSV means "Not the decimal point" Separated Value.






            share|improve this answer


























            • This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

              – Markus Yrjölä
              Feb 3 '17 at 8:37
















            29














            Like many, I have been struggling with the same decisions that Microsoft makes and tried various suggested solutions.



            For Excel 2007 the following goes:




            • Putting all values in double quotes does NOT help

            • Putting an = before all values after putting them in double quutes DOES help, BUT makes the csv file
              useless for most other applications

            • Putting parentheses around the double quotes around all values is
              rubbish

            • Putting a space before all values before putting double quotes around
              them DOES prevent conversions to dates, but DOES NOT prevent trimming
              of leading or trailing zeroes.

            • Putting a single quote in front of a value only works when entering
              data within Excel.


            However:



            Putting a tab before all values before putting double quotes around them DOES prevent conversions to dates AND DOES prevent trimming of leading or trailing zeroes and the sheet does not even show nasty warning markers in the upper left corner of each cell.



            E.g.:



            "<tab character><some value>","<tab character><some other value>"


            Note that the tab character has to be within the double quotes.
            Edit: it turns out that the double quotes are not even necessary.



            Double clicking the csv file can open the file as a spreadsheet in Excel showing all values that are treated as just above, like text data.
            Make sure to set Excel to use the '.' as the decimal point and not the ',' or every line of the csv file will end up as one text in the first cell of each row.
            Apparently Microsoft thinks that CSV means "Not the decimal point" Separated Value.






            share|improve this answer


























            • This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

              – Markus Yrjölä
              Feb 3 '17 at 8:37














            29












            29








            29







            Like many, I have been struggling with the same decisions that Microsoft makes and tried various suggested solutions.



            For Excel 2007 the following goes:




            • Putting all values in double quotes does NOT help

            • Putting an = before all values after putting them in double quutes DOES help, BUT makes the csv file
              useless for most other applications

            • Putting parentheses around the double quotes around all values is
              rubbish

            • Putting a space before all values before putting double quotes around
              them DOES prevent conversions to dates, but DOES NOT prevent trimming
              of leading or trailing zeroes.

            • Putting a single quote in front of a value only works when entering
              data within Excel.


            However:



            Putting a tab before all values before putting double quotes around them DOES prevent conversions to dates AND DOES prevent trimming of leading or trailing zeroes and the sheet does not even show nasty warning markers in the upper left corner of each cell.



            E.g.:



            "<tab character><some value>","<tab character><some other value>"


            Note that the tab character has to be within the double quotes.
            Edit: it turns out that the double quotes are not even necessary.



            Double clicking the csv file can open the file as a spreadsheet in Excel showing all values that are treated as just above, like text data.
            Make sure to set Excel to use the '.' as the decimal point and not the ',' or every line of the csv file will end up as one text in the first cell of each row.
            Apparently Microsoft thinks that CSV means "Not the decimal point" Separated Value.






            share|improve this answer















            Like many, I have been struggling with the same decisions that Microsoft makes and tried various suggested solutions.



            For Excel 2007 the following goes:




            • Putting all values in double quotes does NOT help

            • Putting an = before all values after putting them in double quutes DOES help, BUT makes the csv file
              useless for most other applications

            • Putting parentheses around the double quotes around all values is
              rubbish

            • Putting a space before all values before putting double quotes around
              them DOES prevent conversions to dates, but DOES NOT prevent trimming
              of leading or trailing zeroes.

            • Putting a single quote in front of a value only works when entering
              data within Excel.


            However:



            Putting a tab before all values before putting double quotes around them DOES prevent conversions to dates AND DOES prevent trimming of leading or trailing zeroes and the sheet does not even show nasty warning markers in the upper left corner of each cell.



            E.g.:



            "<tab character><some value>","<tab character><some other value>"


            Note that the tab character has to be within the double quotes.
            Edit: it turns out that the double quotes are not even necessary.



            Double clicking the csv file can open the file as a spreadsheet in Excel showing all values that are treated as just above, like text data.
            Make sure to set Excel to use the '.' as the decimal point and not the ',' or every line of the csv file will end up as one text in the first cell of each row.
            Apparently Microsoft thinks that CSV means "Not the decimal point" Separated Value.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 20 '14 at 15:21

























            answered Jan 20 '14 at 13:02









            ajaboajabo

            39134




            39134













            • This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

              – Markus Yrjölä
              Feb 3 '17 at 8:37



















            • This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

              – Markus Yrjölä
              Feb 3 '17 at 8:37

















            This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

            – Markus Yrjölä
            Feb 3 '17 at 8:37





            This tab trick saved my day, Excel was converting really long numeric values into numbers, and lost all digits after the 15th. Managed to get Excel to treat them as text with the tab prefix. So works for numbers as well, not just dates.

            – Markus Yrjölä
            Feb 3 '17 at 8:37











            19














            Using Excel's import functionality allows you to specify the format (auto, text or date) each column should be interpreted as and does not require any modification to the data files.



            You can find it as DataGet External DataFrom Text in Excel 2007/2010.

            Or DataImport External DataImport Data in Excel 2003.



            Here's an image of the Excel 2003 Text Import Wizard in action on the example data given, showing me importing the latter two columns as text:



            Excel 2003: Text Import Wizard on Step 3 - data types






            share|improve this answer


























            • Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

              – Simon East
              Aug 8 '11 at 11:26











            • @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

              – DMA57361
              Aug 8 '11 at 11:27













            • it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

              – Simon East
              Aug 8 '11 at 11:34








            • 1





              There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

              – DMA57361
              Aug 8 '11 at 11:38













            • thats very helpful, after reading quite a few ansers

              – greg121
              Sep 5 '14 at 20:32
















            19














            Using Excel's import functionality allows you to specify the format (auto, text or date) each column should be interpreted as and does not require any modification to the data files.



            You can find it as DataGet External DataFrom Text in Excel 2007/2010.

            Or DataImport External DataImport Data in Excel 2003.



            Here's an image of the Excel 2003 Text Import Wizard in action on the example data given, showing me importing the latter two columns as text:



            Excel 2003: Text Import Wizard on Step 3 - data types






            share|improve this answer


























            • Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

              – Simon East
              Aug 8 '11 at 11:26











            • @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

              – DMA57361
              Aug 8 '11 at 11:27













            • it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

              – Simon East
              Aug 8 '11 at 11:34








            • 1





              There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

              – DMA57361
              Aug 8 '11 at 11:38













            • thats very helpful, after reading quite a few ansers

              – greg121
              Sep 5 '14 at 20:32














            19












            19








            19







            Using Excel's import functionality allows you to specify the format (auto, text or date) each column should be interpreted as and does not require any modification to the data files.



            You can find it as DataGet External DataFrom Text in Excel 2007/2010.

            Or DataImport External DataImport Data in Excel 2003.



            Here's an image of the Excel 2003 Text Import Wizard in action on the example data given, showing me importing the latter two columns as text:



            Excel 2003: Text Import Wizard on Step 3 - data types






            share|improve this answer















            Using Excel's import functionality allows you to specify the format (auto, text or date) each column should be interpreted as and does not require any modification to the data files.



            You can find it as DataGet External DataFrom Text in Excel 2007/2010.

            Or DataImport External DataImport Data in Excel 2003.



            Here's an image of the Excel 2003 Text Import Wizard in action on the example data given, showing me importing the latter two columns as text:



            Excel 2003: Text Import Wizard on Step 3 - data types







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 3 '11 at 9:20

























            answered Aug 3 '11 at 9:13









            DMA57361DMA57361

            16.9k66195




            16.9k66195













            • Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

              – Simon East
              Aug 8 '11 at 11:26











            • @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

              – DMA57361
              Aug 8 '11 at 11:27













            • it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

              – Simon East
              Aug 8 '11 at 11:34








            • 1





              There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

              – DMA57361
              Aug 8 '11 at 11:38













            • thats very helpful, after reading quite a few ansers

              – greg121
              Sep 5 '14 at 20:32



















            • Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

              – Simon East
              Aug 8 '11 at 11:26











            • @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

              – DMA57361
              Aug 8 '11 at 11:27













            • it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

              – Simon East
              Aug 8 '11 at 11:34








            • 1





              There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

              – DMA57361
              Aug 8 '11 at 11:38













            • thats very helpful, after reading quite a few ansers

              – greg121
              Sep 5 '14 at 20:32

















            Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

            – Simon East
            Aug 8 '11 at 11:26





            Excellent answer DMA57361, thanks for all the detail. What I didn't really mention in my question was that I'm writing a script that exports data to Excel, so I was trying to prevent users from having to jump through confusing options like this. But voted you up anyway. :-)

            – Simon East
            Aug 8 '11 at 11:26













            @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

            – DMA57361
            Aug 8 '11 at 11:27







            @Simon, what're you writing the script in? Any way you can get it to produce actual Excel files directly, instead of going via an intermediate format?

            – DMA57361
            Aug 8 '11 at 11:27















            it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

            – Simon East
            Aug 8 '11 at 11:34







            it's a PHP script that exports a database table. CSV is probably the easiest to work with, but you're correct, I could probably produce an XLS with the help of some open-source code, or even just an HTML table which I think from past experience produces reasonable results in Excel (allows colours & formatting etc., but not sure about data-types).

            – Simon East
            Aug 8 '11 at 11:34






            1




            1





            There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

            – DMA57361
            Aug 8 '11 at 11:38







            There's a few questions over on SO about PHP→Excel, the first few I've tried all have an answer pointing to PHP Excel, so that might be worth a look.

            – DMA57361
            Aug 8 '11 at 11:38















            thats very helpful, after reading quite a few ansers

            – greg121
            Sep 5 '14 at 20:32





            thats very helpful, after reading quite a few ansers

            – greg121
            Sep 5 '14 at 20:32











            1














            The example from Simon did not work for me, and I suspect it is a language difference. In C# here is what my working format string looks like:



            var linebreak = (i++ == list.Count) ? "" : "rn";

            csv += String.Format("="{0}",{1},{2},{3},="{4}"{5}",
            item.Value, item.Status, item.NewStatus, item.Carrier, c.Status, linebreak);


            and this is what the output file looks like:



            ="abababababab",INVALID,INVALID,USPS,="",
            ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
            ="9400110200793000216184",UNKNOWNSTATUS,INVALID,USPS,=""


            As can be seen, the format in the output file is ="VALUE", not "=""VALUE""", which I believe may be a Visual Basic convention.



            I am using Excel 2010. Incidentally, Google Sheets will not open/convert a file formatted this way. It will work if you remove the equal sign thus "VALUE", - Excel will still open the file but ignore the fact that you want your columns to be strings.






            share|improve this answer






























              1














              The example from Simon did not work for me, and I suspect it is a language difference. In C# here is what my working format string looks like:



              var linebreak = (i++ == list.Count) ? "" : "rn";

              csv += String.Format("="{0}",{1},{2},{3},="{4}"{5}",
              item.Value, item.Status, item.NewStatus, item.Carrier, c.Status, linebreak);


              and this is what the output file looks like:



              ="abababababab",INVALID,INVALID,USPS,="",
              ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
              ="9400110200793000216184",UNKNOWNSTATUS,INVALID,USPS,=""


              As can be seen, the format in the output file is ="VALUE", not "=""VALUE""", which I believe may be a Visual Basic convention.



              I am using Excel 2010. Incidentally, Google Sheets will not open/convert a file formatted this way. It will work if you remove the equal sign thus "VALUE", - Excel will still open the file but ignore the fact that you want your columns to be strings.






              share|improve this answer




























                1












                1








                1







                The example from Simon did not work for me, and I suspect it is a language difference. In C# here is what my working format string looks like:



                var linebreak = (i++ == list.Count) ? "" : "rn";

                csv += String.Format("="{0}",{1},{2},{3},="{4}"{5}",
                item.Value, item.Status, item.NewStatus, item.Carrier, c.Status, linebreak);


                and this is what the output file looks like:



                ="abababababab",INVALID,INVALID,USPS,="",
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793000216184",UNKNOWNSTATUS,INVALID,USPS,=""


                As can be seen, the format in the output file is ="VALUE", not "=""VALUE""", which I believe may be a Visual Basic convention.



                I am using Excel 2010. Incidentally, Google Sheets will not open/convert a file formatted this way. It will work if you remove the equal sign thus "VALUE", - Excel will still open the file but ignore the fact that you want your columns to be strings.






                share|improve this answer















                The example from Simon did not work for me, and I suspect it is a language difference. In C# here is what my working format string looks like:



                var linebreak = (i++ == list.Count) ? "" : "rn";

                csv += String.Format("="{0}",{1},{2},{3},="{4}"{5}",
                item.Value, item.Status, item.NewStatus, item.Carrier, c.Status, linebreak);


                and this is what the output file looks like:



                ="abababababab",INVALID,INVALID,USPS,="",
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9500100030492359000149",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793482982812",UNKNOWNSTATUS,DELIVERED,USPS,="3"
                ="9400110200793000216184",UNKNOWNSTATUS,INVALID,USPS,=""


                As can be seen, the format in the output file is ="VALUE", not "=""VALUE""", which I believe may be a Visual Basic convention.



                I am using Excel 2010. Incidentally, Google Sheets will not open/convert a file formatted this way. It will work if you remove the equal sign thus "VALUE", - Excel will still open the file but ignore the fact that you want your columns to be strings.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 27 '14 at 18:37









                Zarepheth

                10817




                10817










                answered Jan 13 '13 at 22:37









                ShaneShane

                1114




                1114























                    -2














                    A simple way to force Excel to interpret the date as text is to put a single quote in front of the date, instead of using full quotes, as in:




                    '10/11/2002




                    If you can import the CSV instead of opening it, you can tell Excel what format each column should be. Have a look at this question I asked.






                    share|improve this answer





















                    • 4





                      Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                      – psynnott
                      Apr 14 '14 at 15:33
















                    -2














                    A simple way to force Excel to interpret the date as text is to put a single quote in front of the date, instead of using full quotes, as in:




                    '10/11/2002




                    If you can import the CSV instead of opening it, you can tell Excel what format each column should be. Have a look at this question I asked.






                    share|improve this answer





















                    • 4





                      Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                      – psynnott
                      Apr 14 '14 at 15:33














                    -2












                    -2








                    -2







                    A simple way to force Excel to interpret the date as text is to put a single quote in front of the date, instead of using full quotes, as in:




                    '10/11/2002




                    If you can import the CSV instead of opening it, you can tell Excel what format each column should be. Have a look at this question I asked.






                    share|improve this answer















                    A simple way to force Excel to interpret the date as text is to put a single quote in front of the date, instead of using full quotes, as in:




                    '10/11/2002




                    If you can import the CSV instead of opening it, you can tell Excel what format each column should be. Have a look at this question I asked.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 20 '17 at 10:17









                    Community

                    1




                    1










                    answered Jan 13 '13 at 23:23









                    hdhondthdhondt

                    2,7652910




                    2,7652910








                    • 4





                      Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                      – psynnott
                      Apr 14 '14 at 15:33














                    • 4





                      Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                      – psynnott
                      Apr 14 '14 at 15:33








                    4




                    4





                    Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                    – psynnott
                    Apr 14 '14 at 15:33





                    Downvoted due to this NOT working in CSV files being imported into Excel. Excel interprets the quote as a literal quote and therefore shows it in the cell.

                    – psynnott
                    Apr 14 '14 at 15:33


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f318420%2fformatting-a-comma-delimited-csv-to-force-excel-to-interpret-value-as-a-string%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







                    Popular posts from this blog

                    Сан-Квентин

                    Алькесар

                    8-я гвардейская общевойсковая армия