记录Java导出Excel,给某个单元格设置文本格式或者某一列设置文本格式,需要给用户编号列设置为文本格式

    @ApiOperation("/模版下载")
    @PostMapping("/template/download/excel")
    public Result<?> excelTemplateToExport(HttpServletResponse response) throws IOException{
//        try {
//            Map<Integer, List<String>> map = new HashMap<>() ;
//            ExcelTitleHandler excelTitleHandler = new ExcelTitleHandler(map);
//            EasyExcelHelper.writeExcelWithModel(resp, new ArrayList<>(), KoiLocalWaterReadRecordResponse.class, "水量录入", excelTitleHandler);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result<>(904, "系统异常!");
//        }
//        return Result.success();
        // 创建工作簿
        Workbook workbook = new HSSFWorkbook();
//Workbook workbook = new XSSFWorkbook();也可
//        // 创建Sheet页
        Sheet sheet = workbook.createSheet("Sheet1");
        // 创建行和单元格,并填充数据
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        Cell cell1 = row.createCell(1);
        Cell cell2 = row.createCell(2);
        Cell cell3 = row.createCell(3);
        Cell cell4 = row.createCell(4);
        cell.setCellValue("*用户编号");
        cell1.setCellValue("*用户名");
        cell2.setCellValue("*用户地址");
//        cell3.setCellValue("*起度");
        cell3.setCellValue("*止度");
//        cell5.setCellValue("*水量");
        cell4.setCellValue("备注");

        //设置文本格式
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat dataFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(dataFormat.getFormat("@"));
//      cell.setCellStyle(cellStyle);给单元格设置文本样式

        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 14);
        cellStyle.setFont(font);
        //设置默认列的单元格格式为"文本"
        for (int i = 0; i < 2; i++) {
            //columnIndex表示第几列
            sheet.setColumnWidth(0, 10*256);
            // 主要起作用的地方
            sheet.setDefaultColumnStyle(i, cellStyle);
        }

        String fileName = URLEncoder.encode("水量录入", "UTF-8");

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        workbook.write(byteArrayOutputStream);

        byte[] byte = byteArrayOutputStream.toByteArray();

        // 设置响应头,指定文件名和文件类型

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xls");

        response.setContentLength(byte.length);

        // 关闭输出流和工作簿对象

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(byte);

        outputStream.flush();

        outputStream.close();
        return null;
    }

结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值