|
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 += "编号,";
for (int i = 0; i < columnName.length; i++)
{
data += columnName[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;
} |
|