Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating headers for CSV file

I'm writing a CSV file using the Opencsv library and need to add headers to my file. The file is created and the headers are inserted, but all the headers in same cell.

csvFile.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(csvFile));
String heading = "eid,name,vid,emp_id,balance_amt,handover_to,entry_date \n";
csvWrite.writeNext(new String[]{heading});
like image 954
iam user Avatar asked Jul 26 '26 23:07

iam user


2 Answers

Here is your solution :

 CSVWriter csvWrite = new CSVWriter(new FileWriter(csvFile));
 String[] entries = {"eid","name","vid","emp_id","balance_amt","handover_to","entry_date"};
 csvWrite.writeNext(entries);

thats working fine here ! try

like image 92
Deepak Kumar Avatar answered Jul 29 '26 13:07

Deepak Kumar


If you want to use FileWriter you can make use of:

    FileWriter writer = new FileWriter(CSV_LOCATION); 
    writer.append("Name,Age,Company,Salary");
    writer.append("\n");
like image 27
Keegs Avatar answered Jul 29 '26 13:07

Keegs