In many cases we need to output some data sets in some file format, where the output of CSV files is a more common way.
Here is a simple implementation of the CSV file output code, shared with you.
public static Boolean createcsv (HttpServletResponse Response, Querydata qrydata, string[][] columnName) throws Exception
{
String data = "";
SimpleDateFormat DataFormat = new SimpleDateFormat ("yyyymmddhhmm");
Date today = new Date ();
String Datetoday = Dataformat.format (today);
if (qrydata.size () = 0 | | columnname.length = 0)
return false;
Data + + "number,";
for (int i = 0; i < columnname.length; i++)
{
Data + + columnname[i][1] + ",";
}
Data + = "n";
for (int i = 0; i < qrydata.size (); i++)
{
Data + + string.valueof (i + 1) + ",";
for (int j = 0; J < Columnname.length; J + +)
{
Data + + qrydata.getrow (i). Get (Columnname[j][0]) + ",";
}
Data + = "n";
}
StringBuilder str = new StringBuilder ();
Str.append (data);
Response.setheader ("Content-disposition", "attachment; filename=/"+ Datetoday +". csv "+"/"");
Response.setcharacterencoding ("Shift-jis");
Response.setcontenttype ("Text/plain;charset=shift-jis");
PrintWriter out = Response.getwriter ();
Out.write (Str.tostring ());
Out.flush ();
Out.close ();
return true;
}