【java】excel添加颜色和注释

news/2024/7/21 6:52:50 标签: poi, excel, java

excel_0">excel添加颜色和注释

javascript">package com.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ExcelTest {
    public static void main(String[] args){
        File file = new File("D:\\test.xlsx");
        InputStream inputStream = null;
        FileOutputStream out = null;
        try {
            inputStream = new FileInputStream(file);
            Workbook workBook = new XSSFWorkbook(inputStream);
            int totalRows = 0;
            int totalCells = 0;
            Sheet sheet = workBook.getSheetAt(0);
            // 得到Excel的行数
            totalRows = sheet.getPhysicalNumberOfRows();
            // 得到Excel的列数
            if (totalRows >= 1 && sheet.getRow(0) != null) {
                totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
            }

            // 读取Excel文档
            File writefile = new File("D:\\结果.xlsx");
            Workbook workbook = new XSSFWorkbook();
            CellStyle redStyle = workbook.createCellStyle();
            redStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
            redStyle.setFillForegroundColor(IndexedColors.RED.getIndex());

            CellStyle blueStyle = workbook.createCellStyle();
            blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
            blueStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());

            Sheet writeSheet = workbook.createSheet("校验结果");
            for (int i = 0; i < totalRows; i++) {
                Row row = sheet.getRow(i);
                Row writeRow = writeSheet.createRow(i);
                if (row == null) {
                    continue;
                }
                //遍历列
                for (int j = 0; j < totalCells; j++) {
                    Cell cell = row.getCell(j);
                    Cell writeCell = writeRow.createCell(j);
                    //问题列
                    String cellValue = getCellValue(cell);
                    if(Func.isNotEmpty(cellValue)) {
                        writeCell.setCellValue(cellValue);
                        writeCell.setCellStyle(redStyle);
                        setComment(writeCell, "与数据库重复", writeSheet);
                    }
                }
            }
            out = new FileOutputStream("D:\\结果.xlsx");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 获取值
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell){
        String cellValue = "";
        if (Func.isNotEmpty(cell)) {
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                    cellValue = cell.getNumericCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_STRING: // 字符串
                    cellValue = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                    cellValue = cell.getBooleanCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA: // 公式
                    cellValue = cell.getCellFormula() + "";
                    break;
                case HSSFCell.CELL_TYPE_BLANK: // 空值
                    cellValue = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR: // 故障
                    cellValue = "";
                    break;
                default:
                    cellValue = "";
                    break;
            }
        }
        return cellValue;
    }
    /**
     * 设置注释
     * @param cell
     * @param text
     * @param sheet
     */
    public static void setComment(Cell cell, String text, Sheet sheet){
        ClientAnchor anchor = new XSSFClientAnchor();
        // 关键修改
        anchor.setDx1(0);
        anchor.setDx2(0);
        anchor.setDy1(0);
        anchor.setDy2(0);
        anchor.setCol1(cell.getColumnIndex());
        anchor.setRow1(cell.getRowIndex());
        anchor.setCol2(cell.getColumnIndex() + 5);
        anchor.setRow2(cell.getRowIndex() + 6);
        // 结束
        Drawing drawing = sheet.createDrawingPatriarch();
        Comment comment = drawing.createCellComment(anchor);
        comment.setString(new XSSFRichTextString(text));
        cell.setCellComment(comment);
    }
}

示例图片

在这里插入图片描述


http://www.niftyadmin.cn/n/903312.html

相关文章

【java】JsonFormat注解时间导致时区相差8小时问题

//默认是cst&#xff0c;需要指定为gmt JsonFormat(pattern "yyyy-MM-dd HH:mm:ss", timezone"GMT8") ApiModelProperty(value "创建时间") private Date createTime;

【docker】docker常用操作及日志清理

一.centos7.x环境 //启动docker服务 sudo systemctl start docker //重启docker服务 sudo systemctl restart docker //关闭docker服务 sudo systemctl stop docker //查看docker服务状态 sudo systemctl status docker1.1 docke服务关闭后还是启动状态问题 //sudo systemct…

【电脑系统】c盘误操作删除EFI引导分区后,开机一直checking media

问题描述 1.利用分区工具不小心将c盘中ESP分区和MSR分区合并了&#xff0c;开机后一直checking media。 2.用u盘装系统&#xff0c;进入pe后还原重装系统后&#xff0c;开机仍然是checking media。 3.boot manager中没有硬盘引导项。 4.pe模式中能看到硬盘&#xff0c;硬盘没有…

【java】jdk的安装

一.安装jdk-8u251-windows-x64 1.双击安装包&#xff0c;按引导安装&#xff0c;这里示例jdk版本为jdk-8u251-windows-x64.exe&#xff0c;点击下一步 2.可以更改jdk安装的位置&#xff0c;这里安装到默认位置C盘&#xff0c;装任意盘都可以&#xff0c;选好位置后点击下一步 …

【java】权限修饰符

1.java提供了四种访问权限&#xff0c;public公共的、protected受保护的、default&#xff08;不写&#xff09;默认的、private私有的。 1.public:公共的&#xff0c;没有限制。在同一个package同一个class&#xff0c;同一个package不同的class&#xff0c;不同package之间的…

【java】 swagger

1.代码示例 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.…

【vue】关闭vue项目启动后打开默认浏览器

1.配置 修改vue.config.js&#xff0c;open改为false module.exports {devServer: {port: 8080,open: false} }

【java】jdbc连接mysql用法示例

1.创建数据库 create database student; use student;2.sql创建表语句 //创建学生表student DROP TABLE IF EXISTS student; CREATE TABLE student (id bigint(0) NOT NULL COMMENT ID,name varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMEN…