• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java code to merge 2 columns in a CSV file

 
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't write code to read or write CSV yourself; use one of the libraries listed in https://coderanch.com/how-to/java/AccessingFileFormats
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Fyodor Sherstobitov
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can try using poi jar from apache POI javadoc
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you can try using poi jar from apache


No. POI has no facilities for reading or writing CSV files.
 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...?
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic