aspose file tools
The moose likes Java in General and the fly likes Java code to merge 2 columns in a CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java code to merge 2 columns in a CSV file" Watch "Java code to merge 2 columns in a CSV file" New topic
Author

Java code to merge 2 columns in a CSV file

Ashutosh M Kulkarni
Ranch Hand

Joined: Jun 07, 2010
Posts: 41

Howdy fellas...!
First of all, please move the thread if this doesn't seem to be the appropriate place for this question, I am posting only because there is Java code involved here.

My Requirement
is that I need to programmatically print out a .csv file that has 2 rows as headers. The second row is actually Sub-headers row which has 4-5 sub-headers under each header from first row.
I need to have the headers from Row 1 combined for its sub-headers (like we can in Excel files). I hope the goal is clear.

My questions
1. Is it possible in a .csv file format to combine columns? (I've tried that in advanced text editors like Textpad, Notepad++ and it doesn't seem to work)
2. Is there a Java utility/class/package readily available to implement that?

Thanks,
Ashutosh.


SCJP 6, next stop - OCPJWCD!
Fyodor Sherstobitov
Greenhorn

Joined: Feb 21, 2011
Posts: 9
Hi!
Excel works with .csv files pretty well, so you can merge two .csv files by copying and paseting them from one file opened in Excel to another.
Ashutosh M Kulkarni
Ranch Hand

Joined: Jun 07, 2010
Posts: 41

Thanks for the reply.
But no, that's not what I am looking for.

For one thing, I need to write a program that will merge them through the PrintWriter that writes the whole file.
And second, the comma separated files (.csv) seem to lose all the column-combining kind of formatting while saving. Otherwise it needs be saved as some other Excel-featured type.

EDIT:
Oops, so rude on my part...
Welcome to Ranch, Fyodor!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35220
    
    7
Don't write code to read or write CSV yourself; use one of the libraries listed in http://www.coderanch.com/how-to/java/AccessingFileFormats


Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Welcome to the Ranch Fyodor Sherstobitov
Kowshik Nandagudi
Ranch Hand

Joined: Dec 09, 2010
Posts: 57
I think you can try using poi jar from apache POI javadoc
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35220
    
    7
I think you can try using poi jar from apache

No. POI has no facilities for reading or writing CSV files.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

It would be better if you can explain the requirement a bit more. May be graphically if possible.

What I can understand from your requirement is that , you need to produce something like (I have excluded the comma separator).


For acheiving something like this , I don't think there will be any ready made code/library available.
But you can try writing something in this direction.



For each "sub-header" of a "header" , you will have to repeate the "header" column in the first row as shown above.

Ashutosh M Kulkarni
Ranch Hand

Joined: Jun 07, 2010
Posts: 41

You got the requirement right, but you too know how bad it will look.
The requirement is of a Report in CSV which contains 4 pieces of info about 3 departments:
Gross Amount
Refunds
Agent Charges
Net Amount

I don't think repetition is an option here.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

I know that would look bad. But CSV is a plain stuff ; very limited formatting options.

You can try to code for the below formats (Only if its ok for your customer :-))
Copy the below formats to a text file , save as CSV file and open using excel and see if this would work for you

OR

Else as mentioned in the one of the messages below , try using POI if possible. You won't get a CSV but a spreadsheet with proper formatting can be created.
Curious to know why you need only CSV ...?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Java code to merge 2 columns in a CSV file
 
Similar Threads
Unique Values in ArrayCollection?
Groovy : Process CSV to SQL
2d array issues.
CSV help
Reading a CSV file and storing it into an array and then displaying it on a HTML screen