Exporting to CSV/Excel in Java

26,302

Solution 1

I would recommend using a framework like opencsv for that. It also does escaping and quoting for you.

Solution 2

If you're not getting errors, check the directory where your code is. Without a specific path, your file is being saved there.

EDIT: Since the file is being saved and you want it to open automatically, use

Runtime.getRuntime().exec("results.csv"); 

(For Windows - opens the csv file in the default application for csv files)

Runtime.getRuntime().exec("open results.csv"); 

(For Mac - opens the csv file in the default application for csv files)

Share:
26,302
WIOijwww
Author by

WIOijwww

Updated on June 20, 2020

Comments

  • WIOijwww
    WIOijwww almost 4 years

    I'm trying to export data into a CSV file through Java and I've got some code to do it but it doesn't seem to be outputting the CSV file. Could someone tell me what's wrong? What I would like to do is rather than saving the file somewhere, I would like it to be directly exported to the user.

    EDIT: Just in case it's not clear, I don't want the file to be saved anywhere but would like it to be outputted automatically to the user i.e. they click export and get the "Run/Save results.csv" window and they open the file. Currently the file is getting saved so I know that the method seems to work, just in the opposite way that I want it to.

    public static void writeToCSV(List<Map> objectList) {
        String CSV_SEPARATOR = ",";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("results.csv"), "UTF-8"));
            for (Map objectDetails : objectList) {
                StringBuffer oneLine = new StringBuffer();
                Iterator it = objectDetails.values().iterator();
    
                while (it.hasNext()) {
                    Object value = it.next();
    
                    if(value !=null){
                        oneLine.append(value.toString());
                        }
    
                    if (it.hasNext()) {
                        oneLine.append(CSV_SEPARATOR);
                    }
                }
                bw.write(oneLine.toString());
                bw.newLine();
            }
            bw.flush();
            bw.close();
        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
    
  • WIOijwww
    WIOijwww over 12 years
    Hmm, after further investigation I found that no exceptions are being thrown, but the file is getting saved to the harddrive. I would actually rather like it to open up automatically so that upon hitting the "Export" button, the file opens directly, if you know what I mean? I'm sure this is possible as I've done it before but can't for the life of me remember how I did it.
  • Eric Hydrick
    Eric Hydrick over 12 years
    @WIOijwww Please edit the question to reflect that you need the file to open automatically.
  • Eric Hydrick
    Eric Hydrick over 12 years
    @WIOijwww Your original post made it sound like the file wasn't getting written at all, not that it wasn't opening automatically.
  • WIOijwww
    WIOijwww over 12 years
    Is this really the best way to do it? I thought Java was platform independent.
  • Eric Hydrick
    Eric Hydrick over 12 years
    The string in the exec() method is the system command to be run, i.e. what you'd run from the command-line of that particular OS to open the file.