提供部分代码
String sql="*****";
List list = commonDao.getAllRecord(sql); GetProperties gp = new GetProperties("dictionary"); String spath = ServletActionContext.getServletContext().getRealPath("/")+ gp.getProp("uploadpath"); String name = "planbuycar"+ StrUtil.dateFormatstr(new Date(), "yyyyMMddHHmmss")+ ".csv";
if (CsvPlanBuyCarFile(spath + name, list)) { message = "导出成功"; } else { message = "导出失败"; }
private static String userxlsinfo = "序号,用户ID,姓名,手机,留言信息,注册时间,"; public static boolean CsvPlanBuyCarFile(String path, List body) { try { File file = new File(path); if (file.isFile()) { file.mkdir(); } FileOutputStream out = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(out, "GB2312"); BufferedWriter bw = new BufferedWriter(osw); // 创建表头 String sheader = userxlsinfo; sheader += "\r\n"; bw.write(sheader); if (body != null) { for (int i = 0; i < body.size(); i++) { CamPlanBuyCar buycar = (CamPlanBuyCar) body.get(i); StringBuffer mess = new StringBuffer(); // 用户信息 mess.append((i + 1) + ","); mess.append(buycar.getId() + ","); mess.append(buycar.getName().trim() + ","); mess.append(buycar.getPhone().trim() + ","); mess.append(buycar.getMessage().trim() + ","); mess.append(StrUtil.dateFormatstr(buycar.getDate(),"yyyy-MM-dd HH:mm:ss")); mess.append("\r\n"); System.out.println(i); bw.write(mess.toString()); } } bw.close(); osw.close(); out.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }