Question

TableNameI am trying to write set of row in to csv file and each ow should start with new line, I used println and \n nothing is printing in new line below is the code what i am trying to.

private void createBlankEmplDocForSubReports(HttpServletResponse response,
        List<HashMap<String, String>> results, String date, HttpServletRequest request )
        throws ServletException, IOException {

    SXSSFRow row = null;
    SXSSFCell cell = null;
    Connection conn = null;
    ResultSet result = null;
    PreparedStatement selectStmt = null;
    PrintWriter out = null;
    try {
        conn = DBUtil.getConnection();
        if(request.getParameter("param1").equals("RED9")){
            selectStmt = conn.prepareStatement("select * TableName");
        }else if(request.getParameter("param1").equals("RED921")){
            selectStmt = conn.prepareStatement("select * TableName");
        }

        result = selectStmt.executeQuery();
    } catch(Exception e) {
        System.out.println("Exception occured in executing query during createBlankEmplDoc Report ::: " +e.getMessage());
    }

    try{
        response.reset();
        response.setHeader("Content-disposition:","filename=mycsvfilename.csv;");
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        int rownum = 1;
        while(result.next()){
            out.println(result.getString("SSN"));
            out.println(',');
            out.println(result.getString("_NAME"));
            out.println(',');
            out.println(result.getString("SIDESRESPONSE"));
            out.println(',');
            out.println(result.getString("LAPSEDDAY"));
            out.println('\n');
            out.flush();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                    out = null;
                }
                if (result != null) {
                    result.close();
                    result = null;
                }
                if (selectStmt != null) {
                    selectStmt.close();
                    selectStmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }

            } catch (SQLException e) {
                DBUtil
                    .log("Exception from ExcelReportServlet class, Method -createNoEcmatsRegDoc "
                            + e);
            }
        }
    }

No correct solution

OTHER TIPS

The best approach will be to Use Line seperator obtained by using System.getProperty("line.separator").

If this file will get downloaded than there should be no problem but if this csv is shown on the browser's chrome it self then it is better to change the Content type to response.setContentType(text/csv;charset=UTF-8")

And if the motto is just to show this on browser then user <BR> as suggested above.

For me line.seperator did the magic. Just add this line

String lineSep = System.getProperty("line.separator");

And use this lineSep in place of '\n'.

Hope this helps.

response.setContentType("text/html;charset=UTF-8");

You are setting the content type as html, so to break in a new line, it is necessary to use <br/> instead of \n

change the code out.println('\n'); to out.println('<br/>');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top