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);
        }
    }
相关推荐
都叫我大帅哥14 分钟前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥14 分钟前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
程序猿阿越33 分钟前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
想要入门的程序猿36 分钟前
Qt写入excel
数据库·qt·excel
程序无bug38 分钟前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro41 分钟前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_970956391 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724091 小时前
Maven
java·数据库·maven
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站