Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Need help for create csv ?

Avatar of Rose_Taylor
Rose_TaylorFlag for United States of America asked on
JavaJava EE
40 Comments1 Solution503 ViewsLast Modified:
I want to create csv from collections of objects ?seachbean.java is latestseach query data.This data want to create csv file using another servlet class.

Now i have to pass to values to servlet class and create csv file.

I am looking help now.

searchbean.java

public Object[] search(String str1, String str2, String str3, String str4, String pageIndex) {
		String returnValue = "";
		lastResults = null;
		
		try {
			String query = getCasesQuery();
			String where = buildWhere(str1,str2, str3);
			String orderBy = buildOrderBy(str4);


			lastResults = DBUtil.getSQL( query + where + orderBy );
			lastResults = mapCustomerDetails( lastResults );
			
			
		} catch (Exception e) {
			//returnValue = "Warning : " + e.getMessage();
			e.printStackTrace();
			returnValue = null;
		}

		return lastResults;
	}

createcsv.java(servlet class)
------------------------------

protected void doGet(HttpServletRequest request,
        HttpServletResponse response,Object lastResults) throws ServletException, IOException {
        
            //Object obj=AjaxBean.getLastSearchResultsAsCSV();
            response.setContentType("text/csv");
            response.setHeader("Content-disposition", "attachment; filename=" + filename);
	    resultSetToCsv(rst, response.getWriter());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        	 try {
 				con.close();
 			} catch (SQLException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
        }
}
        public static void resultSetToCsv(ResultSet rs, Writer out) {
        	try {
        	    final String LINEFEED = "\r\n";
        	    ResultSetMetaData metaData = rs.getMetaData();
        	    int numberOfColumns = metaData.getColumnCount();

        	    // Get the column names
        	    String sep = "";

        	    for (int column = 0; column < numberOfColumns; column++) {
        		out.write(sep);
        		out.write(metaData.getColumnLabel(column + 1));
        		sep = ",";
        	    }

        	    out.write(LINEFEED);

        	    // Get all rows.
        	    while (rs.next()) {
        		sep = "";

        		for (int i = 1; i <= numberOfColumns; i++) {
        		    out.write(sep);
        		    out.write(""+rs.getObject(i));
        		    sep = ",";
        		}

        		out.write(LINEFEED);
        	    }
        	} catch (Exception e) {
        	    e.printStackTrace();
        	}
            }
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
Commented:
This problem has been solved!
Unlock 1 Answer and 40 Comments.
See Answers