CSV Exporting: Preserving leading zeros

前端 未结 4 540
后悔当初
后悔当初 2020-12-21 21:25

I\'m working on a .NET application which exports CSV files to open in Excel and I\'m having a problem with preserving leading zeros when the file is opened in Excel. I\'ve

相关标签:
4条回答
  • 2020-12-21 22:06

    Ok, new discovery.

    Using Quick Preview on Mac to view a CSV file the telephone column will display perfectly, but opening the file fully with Numbers or Excel will ruin that column.

    On some level Mac OS X is capable of handling that column correctly with no user meddling.

    I am now working on the best/easiest way to make a website output a universally accepted CSV with telephone numbers preserved.

    But maybe with that info someone else has an idea on how to make Numbers handle the file in the same way that Quick Preview does?

    0 讨论(0)
  • 2020-12-21 22:07

    I found a nice way around this, if you add a space anywhere along the phone number, the cell is then not treated as number and is treated as a text cell in both Excel and Apple's iWork Numbers.

    It's the only solution I've found so far that plays nice with Numbers.

    Yes I realise the number then has a space, but this is easy to process out of large chunks of data, you just have to select a column and remove all spaces.

    Also, if this is web related, most web type things are ok with users entering a space in the number field. E.g you can tap-to-call on mobiles.

    The challenge is to get the space in there in the first place.

    In use:

    01202123456 = 1202123456

    but

    01202 123456 = 01202 123456

    0 讨论(0)
  • 2020-12-21 22:12

    As @GSerg mentions, this is not a CSV issue.

    If your users must edit/save in Excel they need to select the entire worksheet, right-click and choose "Format Cells" and from the Category list select "Text" after opening the csv file. This will preserve the leading zeros since the numbers will be treated as simple text.

    Alternatively, you could use Open XML SDK 2.0, or some other Excel library, to create an xlsx file from your csv data and programmaticaly set the Cell type to Text in order to take the end users out of the equation...

    0 讨论(0)
  • 2020-12-21 22:26

    This is not a CSV issue.

    This is Excel loving to play with CSV files.

    Change the extension to something else.

    0 讨论(0)
提交回复
热议问题