hutool工具ExcelWriter设置单个单元格样式

49 篇文章 2 订阅
36 篇文章 1 订阅

官网自定义样式:

ExcelWriter writer = ...;

// 定义单元格背景色
StyleSet style = writer.getStyleSet();
// 第二个参数表示是否也设置头部单元格背景
style.setBackgroundColor(IndexedColors.RED, false);

//设置内容字体
Font font = writer.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED); 
font.setItalic(true); 
//第二个参数表示是否忽略头部样式
writer.getStyleSet().setFont(font, true);

以上自定义样式是针对单元格集合的,划分为:

  • 头部样式 headCellStyle

  • 普通单元格样式 cellStyle

  • 数字单元格样式 cellStyleForNumber

  • 日期单元格样式 cellStyleForDate

但是,有时候我们只需要修改单个单元格样式,其他单元格样式采用默认样式,参考如下:

    @GetMapping("downloadExampleExcel")
    public Response downloadExampleExcel(HttpServletResponse response) {
        logger.info("downloadExampleExcel response start。。。");
        List<Title> titles = titleService.selectTitles();
        ExcelWriter writer = ExcelUtil.getWriter();
        for (int i = 0, j = 0; i < titles.size(); i++) {
            Title title = titles.get(i);
            writeCell(writer, j ++, title.getFieldDescC(), title.getRequiredFlag());
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + "文件名" + ".xls");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (Exception e) {
            log.error("downloadExampleExcel response exception", e);
            return ResponseHelper.buildFail(e.getMessage());
        } finally {
            writer.close();
            if (out != null) {
                IoUtil.close(out);
            }
            logger.info("downloadExampleExcel response end。。。");
        }
        return ResponseHelper.buildOk();
    }
 
    /**
     * 输入标题到excel
     * @param writer excel对象
     * @param column 当前列位置
     * @param cellValue 标题内容
     * @param requiredFlag 是否标红
     */
    private void writeCell(ExcelWriter writer, int column, String cellValue, String requiredFlag){
        // 根据x,y轴设置单元格内容
        writer.writeCellValue(column , 0, cellValue);
        Font font = writer.createFont();
        font.setColor(Font.COLOR_RED);
        if (Constants.NUMBER_TWO.equals(requiredFlag)){
            // 根据x,y轴获取当前单元格样式
            CellStyle cellStyle = writer.createCellStyle(column, 0);
            // 内容水平居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            // 内容垂直居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 设置边框
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            // 字体颜色标红
            cellStyle.setFont(font);
        }
    }

 单个单元格字体标红,效果:

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
设置单个单元格部分区域有颜色,可以使用 Apache POI 库来实现。 首先,你需要创建一个工作簿对象并创建一个工作表对象。然后,创建一个单元格对象并设置它的值。接下来,你可以使用 Conditional Formatting 对象来设置单元格的颜色。在这个对象中,你可以定义颜色的条件,并将其应用于单元格的特定区域。 以下是一个示例代码片段,展示如何设置单个单元格部分区域有颜色: ``` // 创建工作簿和工作表对象 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建单元格对象并设置值 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello World"); // 创建条件格式对象 ConditionalFormattingRule rule = sheet.getConditionalFormattingRules().createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"Good\""); PatternFormatting fill = rule.createPatternFormatting(); fill.setFillBackgroundColor(IndexedColors.GREEN.index); fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND); // 设置条件格式对象的区域 CellRangeAddress[] range = { CellRangeAddress.valueOf("A1:A1") }; sheet.getConditionalFormattingRules().addRule(rule); sheet.setActiveConditionalFormatting(range); // 导出工作簿 FileOutputStream out = new FileOutputStream(new File("example.xlsx")); workbook.write(out); out.close(); ``` 在这个示例中,我们创建了一个单元格,其值为 "Hello World"。我们定义了一个条件格式,如果单元格的值等于 "Good",则将单元格的背景颜色设置为绿色。我们将条件格式应用于单元格的 A1 区域。最后,我们将工作簿导出到一个名为 "example.xlsx" 的文件中。 你可以根据需要修改示例代码来设置单个单元格的颜色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wzq_55552

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值