vertx框架 Excel导出(工具类)

 /**
     * 设置excel的字段
     * @param context  上下文
     * @param classmateList  传入的beanlist对象
     * @Param sheetName      一个sheet的表名
     * @param fileName       导出excel的文件名
     * @param head           excel第一行的头
     * @return
     */
    public static void excelUtil(RoutingContext context,
                                 JSONArray jsonArray,
                                 LinkedHashMap<String,String> map,
                                 String sheetName,
                                 String fileName){
        JsonResult jsonResult = new JsonResult();
        //如果上下文为空则返回
        if(context==null){
            System.out.println("传入的RoutingContext为空");
            return;
        }
        //如果传入的json数组为空则返回空
        if(jsonArray == null||jsonArray.size()<=0){
            System.out.println("传入的json数组为空");
            context.response().end("");
            return;
        }
        //如果传入sheetName的参数为空或者“”
        if(sheetName == null||sheetName.equals(""))
        {
            //设置sheet名称
            sheetName="sheet1";
        }
        //如果传入fileName的参数为空或者“”
        if(fileName == null||fileName.equals(""))
        {
            //指定默认的文件名为时间
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            fileName=format.format(date);
        }

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet表名
        HSSFSheet sheet = workbook.createSheet(sheetName);
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

        // 设置边框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        //创建首行
        HSSFRow tableHead = sheet.createRow(0);

        //获取第一个json数组中的第一个json对象
        JSONObject object = jsonArray.getJSONObject(0);

        //创键excel表头中列数
        int cell = 0;
        //设置表头
        for(String value:map.values()){
            //设置excel头部的每一列
            tableHead.createCell(cell).setCellValue(value);
            //将json中键的集合放入list中
            //cell自增
            cell++;
        }

        for(int i=0;i<jsonArray.size();i++){
            //获取body JSON数组中的json
            JSONObject body = jsonArray.getJSONObject(i);
            //创建excel中的每一个行
            HSSFRow row = sheet.createRow(i+1);
            int cell2 = 0;
            //将对应键的值插入到每一列的excel中
            for(String key:map.keySet())
            {
                Object obj = body.get(key);
                //如果obj为空则赋值空字符串
                if(obj==null)
                    obj = "";
                row.createCell(cell2).setCellValue(obj.toString());
                cell2++;
            }
        }
//        创建字节输入流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try{
            //将excel表的数据写入到字节流中
            workbook.write(bos);
        }catch(IOException e){
            throw new RuntimeException("export excel error");
        }
        //设置缓冲区
        Buffer buffer = Buffer.buffer();
        //将流中的内容写入到缓冲区中
        buffer.appendBytes(bos.toByteArray());
        context = setContext(context,fileName);
        context.response().end(buffer);
    }

    /**
     * //设置传输的格式
     * @param context 上下文
     * @param buffer    传输的buffer
     * @param fileName  文件名
     * @return
     */
    public static RoutingContext setContext(RoutingContext context,String fileName){
        //设置传输的格式
        context.response().putHeader("content-type", "application/octet-stream;charset=UTF-8");
        context.response().putHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
        context.response().putHeader("Pargam", "no-cache");
        context.response().putHeader("Cache-Control", "no-cache");
        return context;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值