Click here to Skip to main content
14,828,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! i am looking for adding header to my existing csv file , there is almost 16 colums of header already i want to add mor header from database , after those header

What I have tried:

I tried many ways from google but i don;t
Updated 26-Feb-21 1:09am
Maciej Los 26-Feb-21 6:39am
This sounds like: I want to bake a cake. I've seen and tried tons of recipes, but that bake won't bake.
This description causes many questions, such as:
1) what cake?
2) what recipe?
3) what tools you have used?

Note: we can't read in your mind or direct from your screen...
Please, clarify your question and add more description (what exactly have you tried, what is the structure of csv, what is the method you access data from database, etc.).
Gerry Schmitz 26-Feb-21 15:17pm
Use the same "delimiter" as the data. Unless the column names keep changing, add your own string.

C01, C02, C03 ................. C16
aaa, bbb, ccc, .....

1 solution

CSV Format is really simple - or at least the basics are.
The first row can be a header.
All other rows are data.

Pretty much all systems expect as many columns of data as there are columns of header, so just adding columns to a header probably isn't going to work, unless you also add the relevant columns of data.

But doing it is pretty simple: read your CSV file line by line.
Add your new headers to the first line, and write it to a new file.
If you have extra data, add each row to the end of all subsequent lines.
Either way, write each line to your new file.
When you are finished, rename the original CSV file to a backup name (just in case you made a mistake) and rename the new file to the original CSV file name.
Member 14976405 26-Feb-21 7:17am
yes ! but how can i add a new colum as a header in first row? actually i want to insert a text from database to that csv as header
OriginalGriff 26-Feb-21 8:07am
Same process, but you add your new header - and column data - before the existing stuff in your CSV file.

Remember, CSV is just a text file, so you can't "insert" or "delete" without rewriting the whole file.
Dave Kreskowiak 26-Feb-21 10:04am
Let me guess, you're using code you found on the web to write the CSV file for you and you have no idea how it works?

Without seeing your code, it's impossible to tell you what to do with any accuracy.
OriginalGriff 26-Feb-21 10:21am
But ... but ... but that's how coding works!
You find random lines of code on t'interwebz, paste it into your app and it sorts itself out, even if it's in a different language!

I've been coding like that for minutes and it hasn't failed yet.
Mind you, I can't get it to compile, but that's an implementation detail ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900