简体   繁体   中英

How to export CSV file in Struts2

I'm exporting .xlsx file and Now I want to export .csv file.

I tried to find solution for this but I think none of those are fit for my situation.

Below is my codes

action mapping:

<action name="export" class="com.xxx.xxx.xxx.action.myAction" method="excel">
    <result type="excel">
        <param name="template">/xlsTemplate/excel_temaplate.xls</param>
        <param name="beans">gridModel</param>
        <param name="filenameKey">filename</param>
    </result>
</action>

action:

public String excel() {
        ArrayList<MyVo> resultList = myService.myFunction();
        setGridModel(resultList);
        return SUCCESS;
    }catch(Exception e){
        return ERROR;
    }
}

These works well and I'm trying to export .csv file instead of .xlsx file for web(not saving in local) so that users can download it.

Where should I start ?

Any comment would be appreciated. Thanks.

Here is a util class to manipulate CSV:


import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class CSVUtils {

    private static final char DEFAULT_SEPARATOR = ',';

    public static void writeLine(Writer w, List<String> values) throws IOException {
        writeLine(w, values, DEFAULT_SEPARATOR, ' ');
    }

    public static void writeLine(Writer w, List<String> values, char separators) throws IOException {
        writeLine(w, values, separators, ' ');
    }

    //https://tools.ietf.org/html/rfc4180
    private static String followCVSformat(String value) {

        String result = value;
        if (result.contains("\"")) {
            result = result.replace("\"", "\"\"");
        }
        return result;

    }

    public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException {

        boolean first = true;

        //default customQuote is empty

        if (separators == ' ') {
            separators = DEFAULT_SEPARATOR;
        }

        StringBuilder sb = new StringBuilder();
        for (String value : values) {
            if (!first) {
                sb.append(separators);
            }
            if (customQuote == ' ') {
                sb.append(followCVSformat(value));
            } else {
                sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
            }

            first = false;
        }
        sb.append("\n");
        w.append(sb.toString());


    }

}

So You clous use it in your code like :

    public String excel() throws Exception{
        String csvFile = "/Users/mkyong/csv/abc.csv";
        FileWriter writer = new FileWriter(csvFile);
        ArrayList<MyVo> resultList = myService.myFunction();
        List<String> strings = resultList.stream()
       .map(obj -> obj.getYourAttr())
       .collect(Collectors.toList());
        CSVUtils.writeLine(writer, strings);
       writer.flush();
        writer.close();

        return SUCCESS;
        }catch(Exception e){
            return ERROR;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM