JAVA 使用apache poi实现EXCEL文件的输出;apache poi实现标题行的第一个字符为红色;EXCEL设置某几个字符为别的颜色

设置输出文件的列宽,防止文件过于丑陋

复制代码
Sheet sheet = workbook.createSheet(FileConstants.ERROR_FILE_SHEET_NAME);
sheet.setColumnWidth(0, 40 * 256);
sheet.setColumnWidth(1, 20 * 256);
sheet.setColumnWidth(2, 20 * 256);
sheet.setColumnWidth(3, 20 * 256);

实现标题行的第一个字符为红色,效果如图

//这边的0,1 就是设置第一个字符

Font font = workbook.createFont(); font.setColor(IndexedColors.RED.getIndex());

RichTextString store = workbook.getCreationHelper().createRichTextString(FileConstants.UPDATE_TEMPLATE_NAMES.get(Constants.ZERO)); store.applyFont(0, 1, font);

titleRow.createCell(Constants.ONE_INT).setCellValue(store);

判断如果是第一列的话 设置字体为红色

复制代码
if (Constants.ZERO.equals(columnIndex)) {

    cellStyle.setFont(font);
    cell.setCellStyle(cellStyle);
}

将文件上传oss保存,可以忽略

复制代码
try (InputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
    aliyunOssUtil.uploadFile(ossFilePath, inputStream);
}

完整代码如下:

java 复制代码
    private void uploadErrorFile(List<BatchUpdateStatusFileDTO> list, String fileBatchId) {

        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
             Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet(FileConstants.ERROR_FILE_SHEET_NAME);
            sheet.setColumnWidth(0, 40 * 256);
            sheet.setColumnWidth(1, 20 * 256);
            sheet.setColumnWidth(2, 20 * 256);
            sheet.setColumnWidth(3, 20 * 256);
            // 创建标题行
            Row titleRow = sheet.createRow(0);
            titleRow.createCell(Constants.ZERO).setCellValue(StringPool.EMPTY);
            Font font = workbook.createFont();
            font.setColor(IndexedColors.RED.getIndex());
            RichTextString store = workbook.getCreationHelper().createRichTextString(FileConstants.UPDATE_TEMPLATE_NAMES.get(Constants.ZERO));
            store.applyFont(0, 1, font);
            titleRow.createCell(Constants.ONE_INT).setCellValue(store);
            RichTextString number = workbook.getCreationHelper().createRichTextString(FileConstants.UPDATE_TEMPLATE_NAMES.get(Constants.ONE_INT));
            number.applyFont(0, 1, font);
            titleRow.createCell(Constants.TWO_INT).setCellValue(number);
            RichTextString status = workbook.getCreationHelper().createRichTextString(FileConstants.UPDATE_TEMPLATE_NAMES.get(Constants.TWO_INT));
            status.applyFont(0, 1, font);
            titleRow.createCell(Constants.THREE_INT).setCellValue(status);
            // 创建数据行
            for (int i = 0; i < theResList.size(); i++) {
                BatchUpdateStatusFileDTO data = theResList.get(i);
                Row row = sheet.createRow(i + 1);
                createCell(row, Constants.ZERO, data.getCheckResult(), workbook, font);
                createCell(row, Constants.ONE_INT, data.getSalesStore(), workbook, font);
                createCell(row, Constants.TWO_INT, data.getSalesOrder(), workbook, font);
                createCell(row, Constants.THREE_INT, data.getSalesStatus(), workbook, font);
            }
            String ossFilePath = String.format(FileConstants.BATCH_UPDATE_ERROR_FILE_PATH, fileBatchId);
            // 将Excel输出为InputStream
            workbook.write(out);
            try (InputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
                aliyunOssUtil.uploadFile(ossFilePath, inputStream);
            }
        } catch (Exception e) {
            log.error("---Modify the order status in batches, failed to upload an error file---", e);
        }
    }


    private void createCell(Row row, int columnIndex, Object value, Workbook workbook, Font font) {
        Cell cell = row.createCell(columnIndex);
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Integer) {
            cell.setCellValue((Integer) value);
        } else {
            cell.setCellValue(value.toString());
        }
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setWrapText(Boolean.TRUE);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);
        if (Constants.ZERO.equals(columnIndex)) {

            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
        }
    }
相关推荐
Z_W_H_1 分钟前
【Springboot】Bean解释
java·开发语言
Otaku love travel1 小时前
老系统改造增加初始化,自动化数据源配置(tomcat+jsp+springmvc)
java·tomcat·初始化·动态数据源
DKPT1 小时前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star1 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
晨岳2 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆2 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
YuTaoShao2 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
Apache IoTDB2 小时前
Apache IoTDB V2.0.4 发布|新增用户自定义表函数及多种内置表函数功能
apache·iotdb
程序员的世界你不懂2 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
AI360labs_atyun2 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai