In many cases, we need to output some data sets in a certain file format. CSV file output is a common method.
Below is a simple code to implement CSV file output, which is 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 + = "No ,";
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;
}