首页 > 编程语言 >Java Excel导出动态自定义单元格样式

Java Excel导出动态自定义单元格样式

时间:2022-11-27 21:33:46浏览次数:40  
标签:Java colorIndex 自定义 样式 单元格 param cell font aDouble

根据表格内容定义单元格样式

效果图:

文章描述两种,一种创建生成时定义样式,另一种在excel在写入文件前修改样式

关键代码一

    /**
     * 数据动态设置样式
     *
     * @param cell  单元格
     * @param value 单元格数据
     */
    private void getStyleColor(Cell cell, Object value) {
        if (!ObjectUtils.isEmpty(value)) {
            Double aDouble;
            try {
                aDouble = Double.valueOf(value.toString());
            } catch (Exception e) {
                return;
            }
            short colorIndex;  // 颜色下标
            // 判断分数大小 动态赋值样式
            if (aDouble < 60) {
                colorIndex = IndexedColors.RED.getIndex();
            } else if (aDouble >= 60 && aDouble < 80) {
                colorIndex = IndexedColors.GREEN.getIndex();
            } else {
                colorIndex = IndexedColors.BLACK.getIndex();
            }
            CellStyle cellStyle1 = wb.createCellStyle();  // 创建样式
            cellStyle1.cloneStyleFrom(cell.getCellStyle()); // 克隆原先样式
            Font font = wb.createFont();  // 创建字体
            font.setColor(colorIndex);
            cellStyle1.setFont(font);
            // 设置样式
            cell.setCellStyle(cellStyle1);
        }
    }

关键代码二

    /**
     * 表格创建后未写入前,修改样式
     */
    private void styleColorCustom() {
        // 取出生成好的表格页
        Sheet sheetAt = wb.getSheetAt(0);
        // 开始遍历数据
        for (Row cells : sheetAt) {
            for (Cell cell : cells) {
                // 创建样式
                CellStyle newCellStyle = wb.createCellStyle();
                newCellStyle.cloneStyleFrom(cell.getCellStyle()); // 克隆原先样式

                String stringCellValue = cell.getStringCellValue(); // 获取单元格数据
                Double aDouble;
                try {
                    aDouble = Double.parseDouble(stringCellValue);
                } catch (Exception e) {
                    continue;
                }
                short colorIndex;
                // 根据数据判断赋值
                if (aDouble < 60) {
                    colorIndex = IndexedColors.RED.getIndex();
                } else if (aDouble >= 60 && aDouble < 80) {
                    colorIndex = IndexedColors.GREEN.getIndex();
                } else {
                    colorIndex = IndexedColors.BLACK.getIndex();
                }
                newCellStyle.setFont(getFont(wb, (short) 10, false, colorIndex));
                cell.setCellStyle(newCellStyle); // 设置新样式
            }
        }
    }

    /**
     * 获取字体样式
     * @param workbook
     * @param size
     * @param isBold
     * @param colorIndex
     * @return
     */
    private Font getFont(Workbook workbook, short size, boolean isBold, short colorIndex) {
        Font font = workbook.createFont();
        font.setFontName("Arial"); // 字体样式
        font.setBold(isBold);    // 是否加粗
        font.setFontHeightInPoints(size);   // 字体大小
        font.setColor(colorIndex);
        return font;
    }

使用的  apachePoi,文章只展示关键修改方式代码

颜色、编码对照 (http://www.ibloger.net/article/3391.html)

地址:http://www.ibloger.net/article/3391.html

 

标签:Java,colorIndex,自定义,样式,单元格,param,cell,font,aDouble
From: https://www.cnblogs.com/2979100039-qq-con/p/16930585.html

相关文章

  • 用Java打印一个9层空心菱形
    publicclassRhombus{publicstaticvoidmain(Stringargs[]){      for(inti=1;i<=5;i++){  //i表示层数      //空格个数    ......
  • Java: Exceptions - Try...Catch
    tryandcatch  Usetryandcatch:publicclassMain{publicstaticvoidmain(String[]args){try{int[]myNumbers={1,2,3};Syst......
  • Java: Wrapper Classes
    Wrapperclassesprovideawaytouseprimitivedatatypes(int, boolean,etc..)asobjects.PrimitiveDataTypeWrapperClassbyteByteshortShortint......
  • Java: Iterator
    An Iterator isanobjectthatcanbeusedtoloopthroughcollections,like ArrayList and HashSet.//ImporttheArrayListclassandtheIteratorclassi......
  • 线程总述(Java版)
    一、线程创建1、继承Thread类首先,自定义线程类继承THread类;其次,重写run方法,编写线程执行体;最后,创建线程对象并调用start()方法启动线程。但值得注意的是,线程并不一定......
  • 自用_Minecraft Java Server配置、指令等等提示
    尚未写完gamemode=(模式)//adventure冒险模式、creative创造模式、survival生存模式、spectator旁观者模式。online-mode=//用于验证玩家是否“在线”,也就是是否是正版......
  • 又一巨头从 Java 迁移到 Kotlin,简直很无语。。
    出品|OSC开源社区(ID:oschina2013)Meta发布了一篇博客表示,正在将其Android应用的Java代码迁移到Kotlin,并分享了这一过程中的一些经验。该公司认为,Kotlin是一种流行的......
  • jquery011-自定义函数-执行
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><bodystyle="width:980px;margin:0auto"><h1>例子1</h1><......
  • jquery010-自定义-自执行-封闭
    一,jQuery扩展:-$.extend({函数名:function(){}})$.方法-$.fn.extend({函数名:function(){}})......
  • java中时间表达
    初始化Datedate=newDate();输出时间字符串System.out.println(date.toString());字母           描述          ......