当前位置: 首页 > news >正文

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);
}

 完整代码如下:

    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输出为InputStreamworkbook.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);}}

http://www.lryc.cn/news/519948.html

相关文章:

  • 通过vba实现在PPT中添加计时器功能
  • 检验统计量与p值笔记
  • 【集成学习】Bagging、Boosting、Stacking算法详解
  • Rabbit Rocket kafka 怎么实现消息有序消费和延迟消费的
  • 【Ubuntu与Linux操作系统:五、文件与目录管理】
  • 32_Redis分片集群原理
  • 微信小程序mp3音频播放组件,仅需传入url即可
  • Sql 创建用户
  • 数据结构:LinkedList与链表—面试题(三)
  • 【开发日记】Docker修改国内镜像源
  • Elasticsarch:使用全文搜索在 ES|QL 中进行过滤 - 8.17
  • 第432场周赛:跳过交替单元格的之字形遍历、机器人可以获得的最大金币数、图的最大边权的最小值、统计 K 次操作以内得到非递减子数组的数目
  • RK3399开发板Linux实时性改造
  • 青少年编程与数学 02-006 前端开发框架VUE 22课题、状态管理
  • Linux 内核中的 netif_start_queue 函数:启动网络接口发送队列的关键
  • 数据结构之顺序结构二叉树(超详解)
  • acwing_5722_十滴水
  • acwing-3194 最大的矩形
  • UnityDemo-TheBrave-制作笔记
  • 玩转 JMeter:Random Order Controller让测试“乱”出花样
  • VTK知识学习(33)-交互问题2
  • Centos9-SSH免密登录配置-修改22端口-关闭密码登录-提高安全性
  • SqlServer: An expression services limit has been reached异常处理
  • CentOS下安装Docker
  • WPF控件Grid的布局和C1FlexGrid的多选应用
  • Jenkins-持续集成、交付、构建、部署、测试
  • 高级第一次作业
  • Copula算法原理和R语言股市收益率相依性可视化分析
  • 反弹SHELL不回显带外正反向连接防火墙出入站文件下载
  • 后盾人JS--JS值类型使用