批量导出为csv文件

 /**
     *
     * 功能描述:

     * 批量导出
     *
     * @param request
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("sgbatchOutput")
    public void batchOutput(HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        logger.info("type:" + type, type);
        int typevalue = Integer.parseInt(type);
        List> list = stService.batchOutput(typevalue);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");// 设置日期格式
        String outputfilename = df.format(new Date());// new Date()为获取当前系统时间
        response.setCharacterEncoding("GBK");
        response.addHeader("Content-Disposition", "attachment;filename=" + outputfilename + ".csv");
        Writer writer = null;
        try {
            writer = response.getWriter();
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            stService.writeCsv(bufferedWriter, list, typevalue);
            bufferedWriter.close();
            writer.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }


-----------------------------------------------------------------------------------------------------------------
 /**
     *
     * 功能描述:

     * 写CSV文件
     * 
     */
    public void writeCsv(BufferedWriter bufferedWriter, List> list, int typevalue)
            throws IOException {

                    bufferedWriter.write("常用\n");
            bufferedWriter.write("关键词,类型,目录,名称,图片url,url链接,值,\n");
            for (Map map : list) {
                bufferedWriter.write(map.get("KEYWORD").toString() + "," + map.get("TYPE").toString() + ","
                        + map.get("DIR_ID").toString() + "," + map.get("DIR_NAME").toString() + ","
                        + map.get("DIR_IMAGE").toString() + "," + map.get("URL").toString() + ","
                        + map.get("SORT").toString() + "\n");
                bufferedWriter.flush();
            }
        
        }

    }

------------------------------------------------------------------------------------------------------------
service

 /**
     * 功能描述:

     * 批量导出     *
     * @param type
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List> batchOutput(int typevalue) {
        List> results = new ArrayList>();
        if (typevalue == 4) {
            results = sdDao.batchOutputDir();
        }
        if (typevalue == 5) {
            results = sbDao.batchOutputBrand();
        }
        if (typevalue == 8) {
            results = sgDao.batchOutputGoods();
        }
        if (typevalue == 10) {
            results = pgDao.batchOutputPro();
        }
        return results;
    }
---------------------------------------------------------------------------------------------------------
dao

  /**
     *
     * 功能描述:

     * 批量导出常用分类
     *
     * @param type
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List> batchOutputDir() {

        // List paramList=new ArrayList();
        String sqlStr = "SELECT A.KEYWORD,B.TYPE,A.DIR_ID,A.DIR_NAME,"
                + "A.DIR_IMAGE,A.URL,A.SORT FROM DIRECTORY A "
                + "LEFT JOIN TF_SUGGESTION_TYPE B ON A.SUG_TYPE_ID=B.ID  WHERE  TYPE='04' ";
        return jdbcTemplate.queryForList(sqlStr);
    }

你可能感兴趣的:(java)