14

I created a csv file with the following format which I'm aiming to output to the device's sd card:

Ship Name,Scientist Name,Scientist Email,Sample Volume,Sample Colour,Longitude,Latitude,Material,Date

Each of the vales in the csv will be of type string except for the last value of date. The name of the csv file is AnalysisData.csv

I've looked examples on Stackoverflow such as this, Export my data on CSV file from app android but this creates a new file which I don't want.

I have already added the opencsv jar to my project so just need a relevant example.

Does anyone have any advice on achieving this in Android?

Community
  • 1
  • 1
Brian J
  • 5,416
  • 19
  • 94
  • 189

1 Answers1

28

Try with this code snippet :

  String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
  String fileName = "AnalysisData.csv";
  String filePath = baseDir + File.separator + fileName;
  File f = new File(filePath);
  CSVWriter writer;

  // File exist
  if(f.exists()&&!f.isDirectory())
    {
      mFileWriter = new FileWriter(filePath, true);
      writer = new CSVWriter(mFileWriter);
    }
  else
    {
      writer = new CSVWriter(new FileWriter(filePath));
    }

    String[] data = {"Ship Name", "Scientist Name", "...", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").formatter.format(date)});

  writer.writeNext(data);

  writer.close();
Cool
  • 10,038
  • 13
  • 56
  • 77
  • The above code worked..but how can I format the csv so that the data is displayed underneath the data heading? At the moment data is output alongside the field name like this `"Ship Name","Maria" "Scientist Name","Joh Doe"` This is the code I'm using to add the data `data.add(new String[] {"Scientist Name", analystName});` but the format is not correct, perhaps I can use a new line format? – Brian J Jan 05 '15 at 13:52
  • use `writeNext` http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVWriter.html#writeNext(java.lang.String[]) – Cool Jan 05 '15 at 14:23
  • Can you show me an example of how I can apply `write.next` to the above code please? – Brian J Jan 05 '15 at 14:49
  • @BrianJ check my edit you must add a loop to add severl lines – Cool Jan 05 '15 at 15:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68229/discussion-between-brian-j-and-abdellah). – Brian J Jan 05 '15 at 16:16
  • It should be FileWriter mFileWriter = new FileWriter(filePath , true); and should be surronded with try catch – wwjdm Mar 08 '17 at 15:06
  • 1
    FYI, I had a Gradle warning for CSVWriter's commons-logging dependency when adding com.opencsv:opencsv:4.0. – LordParsley Aug 15 '17 at 09:15
  • 11
    compile 'com.opencsv:opencsv:4.0' – Aderbal Nunes Oct 05 '17 at 18:51
  • Hello @Abdellah , I'm not able to create csv file in android, my work is i have to create csv file by button click and insert data into it by pojo , please help me . I have added opencsv jar on my android project – Abhishek kumar Jan 31 '18 at 13:04
  • what is **CSVWriter** ?? – Hossein Kurd Mar 13 '19 at 10:56
  • @AndroSco check this : http://opencsv.sourceforge.net/apidocs/com/opencsv/CSVWriter.html – Cool Mar 13 '19 at 11:06