0

I'm a noob in CSV processing using Java.

How can I read/write multiple header CSV in Java. For example:

Data in hand after multiple joins (say from a table).

-------------------------------------------------------------
|student_id | name | year | subject_id | marks| total_marks |
-------------------------------------------------------------
|STD01      | ABC  |2001  | SUB01      | 80   | 155         |
|STD01      | ABC  |2001  | SUB02      | 75   | 155         |
|STD02      | XYZ  |2001  | SUB01      | 85   | 85          |
-------------------------------------------------------------

The above data to be represented in below format in CSV - they differ in the first column which identifies them either as ALL or IND

ALL, STUDENT_ID, NAME, YEAR, TOTAL_MARKS
IND, STUDRNT_ID, NAME, YEAR, SUBJECT_ID, MARKS
ALL, STD01, ABC, 2001, 155
IND, STD01, ABC, 2001, SUB01, 80
IND, STD01, ABC, 2001, SUB02, 75
ALL, STD02, XYZ, 2001, 85
IND, STD02, XYZ, 2001, SUB01, 85

Exporting them as the joined table would mean I have the ALL column in every line which is not desired.

Any clues or useful tools suggestion much appreciated!

lucniner
  • 102
  • 2
  • 2
  • 12
Jake
  • 391
  • 1
  • 4
  • 22
  • Hi Jake, why would you want a multi line header? How in the CSV do you know which line corresponds to which header line? Do you have a logic for that? And why not simply export it as the view/table from the multiple join result? – lucniner Sep 23 '20 at 06:49
  • @lucniner if you can see there is first column with entry ALL/IND (Total/Individual), this differentiates the data rows to match the header rows. If we simply export it as a view/table then each row will have total aggregations which is not desired. So individual rows are rearranged in individual/aggregate rows – Jake Sep 23 '20 at 06:53
  • Hi @Jake, Ah, I get your point - have maybe a look at [any of these tools](https://mvnrepository.com/search?q=csv) which might help you - I once used the apache commons csv for a similar issue – lucniner Sep 23 '20 at 08:37
  • Hi @lucniner thanks for advice. But there also it's processed using single header. I can't find any resource where multiple headers are handled! – Jake Sep 23 '20 at 09:01
  • Hi @Jake, then you probably need to write your own custom solution by iterating over every line of the file and handle them according. – lucniner Sep 23 '20 at 09:19

1 Answers1

0

I don't now if this will help but maybe this class of mine will help you. It takes a list of objects and returns a CSV file for you. Maybe you can go check out the code in the class just to see what I did https://github.com/ernst223/spread-sheet-exporter

SpreadSheetExporter spreadSheetExporter = new SpreadSheetExporter(List<Object>, "Filename");
File fileCSV = spreadSheetExporter.getCSV();