在新行中写日期

时间:2014-02-20 18:06:35

标签: java servlets cvs

TableName我正在尝试将一组行写入csv文件,每个ow应该以新行开头,我使用println和\ n什么都没有在下面的新行中打印是我正在尝试的代码。

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);
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

最好的方法是使用获得的Line seperator System.getProperty("line.separator")

如果这个文件下载的话应该没有问题,但如果这个csv在浏览器的chrome上显示,那么最好将内容类型更改为 response.setContentType(text/csv;charset=UTF-8")

如果座右铭只是在浏览器上显示,那么请按照上面的建议使用<BR>

对我来说line.seperator做了神奇的事。只需添加此行

即可

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

并使用此lineSep代替'\ n'。

希望这有帮助。

答案 1 :(得分:0)

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

您将内容类型设置为html, 因此要打破新行,必须使用<br/>代替\n

将代码out.println('\n');更改为out.println('<br/>');