Learning to create simple CSV files manually

25 May 2009

CSV is one of the best solutions when implementing any kind of data into an Excel-like spreadsheet. It’s hyper simplicity and its broad compatibility makes this format ideal for any kind of project.

Let’s have a look at a simple CSV file:

a1,b1,c1,d1
a2,b2,c2,d2
a3,b3,,d3

This is a 4 column x 3 row table with letters and numbers. For your reference, we have used column and row reference into every cell. Since we haven’t included any text in c3 column, it shows comma comma without anything in it.

So:

  • to change to the next cell, add a comma.
  • to change to the next row, add a line break.

But what if we want to insert a comma inside a cell? Pretty simple, just quote it:
We have replaced c1 with c,1 and this is how it looks:

a1,b1,"c,1",d1
a2,b2,c2,d2
a3,b3,,d3

But what if we want to insert a quote inside a cell? Simple too, quote it:
We have replaced c,1 with "c1" and this is how it looks:

a1,b1,"""c1""",d1
a2,b2,c2,d2
a3,b3,,d3

That’s it! Now you can convert any kind of data into a spreadsheet using simple RegEx to get lighter AJAX calls than XML or whatever use you want…

Living in United Kingdom? Read about the best mobile phone network in the UK.

               
       

Comments

3 comments to Learning to create simple CSV files manually

  • Avatar Jordi

    Hi!!

    Im trying it as is nice, but there is one thing that i can not do: write a string made of numbers, with zeros at the left side. Excel thinks it’s a number and I loose the zeros :(

    Any one can comment about this? It is useful to me, as I can export database product codes made this way into excel without too much hassle.

    Signed: sausage eater :D

    June 2nd, 2009 at 10:50 am
  • Avatar LuckyShot

    Jordi! :D

    To avoid loosing the leading zeros use the Data Import Wizard in Excel. At the last step you can set the format of the columns to text/string.

    Que tal tot? Com va per TremSpain? Jo dexamens i a punt de graduarme. Quan acabi em passo pel server!

    Un saludote i records a tots!

    June 2nd, 2009 at 12:39 pm
  • Avatar Jordi

    Thanks!, i’ll take a look at it!

    M’alegre de veure’t acabant.

    Si et passes molta gent s’alegrarà.

    Ens vem !

    (=

    June 2nd, 2009 at 7:23 pm

Comment Form