简体   繁体   中英

Generate CSV file in JSF 2.0

i am using apache poi and JSF 2.0 in my application to generate report in excel format.

Code Snipped to generate Excel.

final FacesContext facesContext = FacesContext.getCurrentInstance();
        final ExternalContext externalContext = facesContext
                .getExternalContext();
        externalContext.setResponseContentType("application/vnd.ms-excel");
            externalContext.setResponseHeader("Content-Disposition",
            "attachment; filename=\"Excel Sample");
        workbook.write(externalContext.getResponseOutputStream());
        facesContext.responseComplete();

As You can see i am adding "application/vnd.ms-excel"- MIME type for the server. I am just playing around and wondering if we can change the MIME type to CSV. I am no more generating Excel file for report.

There is another function where i am fetching the data from the database and writing it to cells using HSSF object. and I call this method (code snippet) after writing data to each cell.

What I am supposed to write instead

externalContext.setResponseContentType("application/vnd.ms-excel");

So that it generates file with csv extension.

response.setContentType("text/comma-separated-values; charset=utf8");

The normal mimetype for a CSV file is text/csv

When generating your file, be sure to quote values correctly. It may be worth using an existing library to ensure that's done, especially if you have complicated values

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