java excel 设置sheet页标题、自定义设置cell(单元格样式)

    @ApiLog("报告导出")
    @ApiOperation(value = "报告导出")
    @GetMapping("exportReport")
    public Map<String,Object> exportReport(String id, HttpServletResponse response) {
        Map<String,Object> resultMp = new HashMap<>();
        FileInputStream is = null;
        Workbook wb = null;
        try {
               // 文件地址  在 Resource 目录下
            Resource resource = new ClassPathResource("template/xxx.xlsx");
            boolean isFile = resource.isFile();
            //如果不存在返回
            if(!isFile){
                resultMp.put("code",1);
                resultMp.put("msg","文件不存在");
                return resultMp;
            }

            String path = resource.getFile().getPath();     //获取文件路径
            File fi = new File(path);

            is = new FileInputStream(fi);
            wb = WorkbookFactory.create(is);
               // 设置 sheet 名称
            wb.setSheetName(0,"实验报告");
            Sheet sheet1 = wb.getSheetAt(0);
                 // 遍历设置列的的宽度 
            for (int i = 0; i < 8; i++) {
                sheet1.setColumnWidth(i, 18*256);//设置当前sheet页第一列宽度
            }
//
//            sheet1.setColumnWidth(0, 15*256);//设置当前sheet页第一列宽度
//            sheet1.setColumnWidth(1, 15*256);//第二列宽度
//            sheet1.setColumnWidth(1, 15*256);//第二列宽度
//            sheet1.setColumnWidth(1, 15*256);//第二列宽度
//            sheet1.setColumnWidth(1, 15*256);//第二列宽度
//            sheet1.setColumnWidth(1, 15*256);//第二列宽度




            CellStyle cellStyle = wb.createCellStyle();
            //下边框
            cellStyle.setBorderBottom(BorderStyle.THIN);
            //左边框
            cellStyle.setBorderLeft(BorderStyle.THIN);
            //上边框
            cellStyle.setBorderTop(BorderStyle.THIN);
            //右边框
            cellStyle.setBorderRight(BorderStyle.THIN);
            //增加水平居中样式
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            //增加垂直居中样式
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //自动换行样式增加
            cellStyle.setWrapText(true);
             
// 创建行
Row dataRow = sheet1.createRow( startRow + 4 +j);

// 行上创建cell 

dataRow.createCell(0).setCellStyle(cellStyle);
dataRow.createCell(1).setCellStyle(cellStyle);
dataRow.createCell(2).setCellStyle(cellStyle);
dataRow.createCell(3).setCellStyle(cellStyle);
dataRow.createCell(4).setCellStyle(cellStyle);
dataRow.createCell(5).setCellStyle(cellStyle);
// 获取行数并赋值
dataRow.getCell(0).setCellValue(dataList.get(j).get("xxxx").toString().trim());
dataRow.getCell(1).setCellValue(dataList.get(j).get("xxxx").toString().trim());
dataRow.getCell(2).setCellValue(dataList.get(j).get("xxxx").toString().trim());
dataRow.getCell(3).setCellValue(dataList.get(j).get("xxxx").toString().trim());
dataRow.getCell(4).setCellValue(dataList.get(j).get("xxxx").toString().trim());
dataRow.getCell(5).setCellValue(dataList.get(j).get("xxxx").toString().trim());




//            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(pName+"xxx"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            //设置编码
            response.setCharacterEncoding("utf-8");
            wb.write(response.getOutputStream());
            wb.close();
            is.close();
            resultMp.put("code",0);
            resultMp.put("msg","下载xx成功");

        } catch (Exception e) {
            e.printStackTrace();
            resultMp.put("code",1);
            resultMp.put("msg","下载xxx失败");
        }finally {
            try {
                if (wb != null) {
                    wb.close();
                }

                if (is != null) {
                    is.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();

            }
        }

        return resultMp;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值