生成csv文件中并将文件添加到zip压缩包中

pom.xml配置文件中添加依赖

        
        
            org.json
            json
            20090211
        

请求json数据转成JSONArray,通过CDL.toString(jsonArray);转成string类型的数据,数据如下:

year,subject,subjectType,isPassVerifty,vcode,isOpenEnrollquery,isOpenTickeprint,adjname,genre,isPhotoVerifty,company,id,levelname,cityname,mobile,operateType,photo,thirdcompany,subjectid,adjcode,month,secondcompany,idcard,name,isOpenVerifty
2019,测试,1101,2401,null,1802,1802,34,"理论,操作",1802,社会,1181478206816792577,职业资格五级,无锡市,13921599129,null,1162248159623225348,运营部,1173436904938393602,34,9,其他单位,321183199109193815,元依珊,1801
2019,嗡嗡嗡,1100,2402,null,1802,1802,电力通信运维员,"理论,操作",1801,中国电信股份有限公司江苏分公司,1181380065429966850,职业资格一级,无锡市,13505231957,null,1182180660650848257,121212,1181379827273191425,1002,10,连云港电信,320623198002140014,全球,1801
2019,【研发用】--【研高】互联网技术【胡】,1103,2402,null,1802,1802,互联网技术,高工评审,1802,江苏省通信服务有限公司,1182299830264762369,研高,苏州市,13770686113,null,1162248159623225365,南京华苏科技有限公司,1182297578514911233,223,10,江苏中博通信设备分公司,320121199011030014,李骏奡,1801
2019,【研发用】--【副高】设备环境【胡】,1103,2402,null,1802,1802,设备环境,高工评审,1802,社会,1182301574638047251,副高,常州市,18661201239,null,null,南京嘉环科技有限公司,1182296047853998082,222,10,其他单位,341103199001081215,李堃,1801
2019,【研发用】--【副高】设备环境【胡】,1103,2402,null,1802,1802,设备环境,高工评审,1802,社会,1182301574638047252,副高,常州市,18796978207,null,null,常州招投标代理中心,1182296047853998082,222,10,其他单位,320483198607155839,高晓珑,1801

最后将数据导出

        //方法三:输出数据到文件中并将文件添加到zip压缩包中
        byte[] bytes = csv.getBytes();
        // 编码类型
        response.setCharacterEncoding("utf-8");
        // 请求头
        response.setContentType("text/html; charset=utf-8");
        InputStream fis = null;
        ZipOutputStream zipOut = null;
        try {
            fis = new ByteArrayInputStream(bytes);
            zipOut = new ZipOutputStream(response.getOutputStream());
            FileUtils.zipFile(fis, zipOut, "ab.csv");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                zipOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

完整实现:

    /**
     * 测试
     */
    @RequestMapping(value = "/authEnroll/test", method = RequestMethod.POST)
    public void test(@RequestBody Map map, HttpServletResponse response) {

        List list = (List) map.get("ids");
        List records = null;
        //将过滤条件转成对象
        SubjectEnrollList model = (SubjectEnrollList) MapBeanUtil.mapToBean(map, SubjectEnrollList.class);
        if (list != null && list.size() > 0) {
            records = listService.selectList(new EntityWrapper<>(model).in("id", list));
        } else {
            records = listService.selectList(new EntityWrapper<>(model));
        }

        //使用jackson转换
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(records);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        String json1 = "[{\"id\":1151386828971962369,\"year\":2019,\"month\":2,\"subjectType\":1102," +
                "\"subjectid\":1142974581648003074,\"subject\":\"G:2019年2月初级考试科目\",\"levelname\":\"初级\"," +
                "\"adjcode\":\"110\",\"adjname\":\"初级\",\"genre\":\"综合能力,专业实务\",\"company\":\"中国移动通信集团江苏有限公司\"," +
                "\"secondcompany\":\"无锡移动\",\"thirdcompany\":\"123\",\"name\":\"敖哲瀚\"," +
                "\"idcard\":\"110101197505052519\",\"photo\":1153117791993069570,\"cityname\":\"淮安市\"," +
                "\"isOpenVerifty\":1802,\"isPassVerifty\":2402,\"isOpenEnrollquery\":1801,\"isOpenTickeprint\":1801," +
                "\"isPhotoVerifty\":1802,\"mobile\":\"13965455421\",\"vcode\":null},{\"id\":1141634592641380354," +
                "\"year\":2019,\"month\":10,\"subjectType\":1101,\"subjectid\":1140892928876474370," +
                "\"subject\":\"2019年10月客户工程师技能鉴定考试\",\"levelname\":\"职业资格一级\",\"adjcode\":\"720\"," +
                "\"adjname\":\"客户工程师\",\"genre\":\"理论\",\"company\":\"中移铁通有限公司江苏分公司\",\"secondcompany\":\"南京铁通\"," +
                "\"thirdcompany\":\"运营部\",\"name\":\"施海兰\",\"idcard\":\"320922198012205723\"," +
                "\"photo\":1141638294626365442,\"cityname\":\"南京市\",\"isOpenVerifty\":1801,\"isPassVerifty\":2403," +
                "\"isOpenEnrollquery\":1802,\"isOpenTickeprint\":1801,\"isPhotoVerifty\":1802," +
                "\"mobile\":\"13778956459\",\"vcode\":null}]";

        String csv = null;
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(json);
            csv = CDL.toString(jsonArray);
            System.out.println(csv);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //方法三:输出数据到文件中并将文件添加到zip压缩包中
        byte[] bytes = csv.getBytes();
        // 编码类型
        response.setCharacterEncoding("utf-8");
        // 请求头
        response.setContentType("text/html; charset=utf-8");
        InputStream fis = null;
        ZipOutputStream zipOut = null;
        try {
            fis = new ByteArrayInputStream(bytes);
            zipOut = new ZipOutputStream(response.getOutputStream());
            FileUtils.zipFile(fis, zipOut, "ab.csv");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                zipOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
/**
 * 根据输入的文件与输出流对文件进行打包
 */
public static void zipFile(InputStream inputStream, ZipOutputStream ouputStream, String fileName) {
    BufferedInputStream bins = null;
    try {
        bins = new BufferedInputStream(inputStream, 512);
        ZipEntry entry = new ZipEntry(fileName);
        ouputStream.putNextEntry(entry);
        // 向压缩文件中输出数据
        int length;
        byte[] buffer = new byte[512];
        while ((length = bins.read(buffer)) != -1) {
            ouputStream.write(buffer, 0, length);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        // 关闭创建的流对象
        try {
            bins.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(生成csv文件中并将文件添加到zip压缩包中)