EasyExcel 设置字体样式(字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线)

1 Maven配置 

        <!--hutool工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.1</version>
        </dependency>
        <!-- EasyExcel文档处理工具 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2 调试代码

    /**
     * 导出(设置字体样式,支持设置字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线)
     *
     * @param response
     */
    @GetMapping("/exportFontStyle")
    public void exportFontStyle(HttpServletResponse response) {
        try {
            //生成表格数据
            List<List<Object>> dataList = new ArrayList<>();
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头1", "表头2", "表头3", "表头4"})));
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
            //导出文件
            String fileName = new String("文件名称.xlsx".getBytes(), "UTF-8");

            List<CellStyleModel> cellStyleList = new ArrayList<>();
            //第一种一次性设置字体样式
            cellStyleList.add(CellStyleModel.createFontCellStyleModel("模板", 2, 2, "微软雅黑", 20D, IndexedColors.BLUE
                    , true, true, Font.U_SINGLE, Font.SS_NONE, true));

            //第二种分别设置字体样式
            //设置单元格字体(黑体)
            cellStyleList.add(CellStyleModel.createFontNameCellStyleModel("模板", 0, 0, "黑体"));
            //设置单元格大小 18号
            cellStyleList.add(CellStyleModel.createFontHeightCellStyleModel("模板", 0, 1, 18D));
            //设置单元格字体颜色
            cellStyleList.add(CellStyleModel.createFontColorCellStyleModel("模板", 0, 2, IndexedColors.RED));
            //设置单元格字体加粗
            cellStyleList.add(CellStyleModel.createFontBoldCellStyleModel("模板", 0, 3, true));
            //设置单元格字体斜体
            cellStyleList.add(CellStyleModel.createFontItalicCellStyleModel("模板", 1, 0, true));
            //设置单元格字体单下划线
            cellStyleList.add(CellStyleModel.createFontUnderLineCellStyleModel("模板", 1, 1, Font.U_SINGLE));
            //设置单元格字体双下划线
            cellStyleList.add(CellStyleModel.createFontUnderLineCellStyleModel("模板", 1, 2, Font.U_DOUBLE));
            //设置单元格字体上标
            cellStyleList.add(CellStyleModel.createFontTypeOffsetCellStyleModel("模板", 1, 3, Font.SS_SUPER));
            //设置单元格字体下标
            cellStyleList.add(CellStyleModel.createFontTypeOffsetCellStyleModel("模板", 2, 0, Font.SS_SUB));
            //设置单元格字体删除线
            cellStyleList.add(CellStyleModel.createFontStrikeoutCellStyleModel("模板", 2, 1, true));

            response.addHeader("Content-Disposition", "filename=" + fileName);
            //设置类型,扩展名为.xls
            response.setContentType("application/vnd.ms-excel");
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new CustomCellStyleHandler(cellStyleList)).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(dataList, writeSheet);
            //千万别忘记finish 会帮忙关闭流
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3 调试结果

注:

(1)有关CellStyleModel和CustomCellStyleHandler的源码请查看以下博客。

EasyExcel 批量设置单元格样式(字体样式、背景颜色、边框样式、对齐方式、自动换行)icon-default.png?t=M85Bhttps://blog.csdn.net/qq_38974638/article/details/114841208

(2)有关自定义RGB颜色的使用请查看以下博客。

 EasyExcel 单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现icon-default.png?t=M85Bhttps://blog.csdn.net/qq_38974638/article/details/117395831

旭东怪的个人空间_哔哩哔哩_Bilibili旭东怪,人生低谷不可怕,可怕的是坚持不到人生转折点的那一天;旭东怪的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。https://space.bilibili.com/484264966?spm_id_from=333.337.search-card.all.click

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值