51Testing软件测试论坛

标题: Dataset如何写成csv文件 [打印本页]

作者: fcui    时间: 2008-1-11 10:50
标题: Dataset如何写成csv文件
想请教一下,如何将RFT中连接数据库获得的Dataset写成csv文件
作者: lovetest6    时间: 2008-1-11 17:47
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;
}
作者: wonew1228    时间: 2008-5-20 11:06
好人,谢谢




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2