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

网站建设公司用5g最新全国疫情消息

网站建设公司用5g,最新全国疫情消息,免费直播软件下载,seo就业前景怎么样文章目录 前言一、自定义拦截器二、自定义操作1.自定义颜色2.合并单元格 三、复杂操作示例1.实体(使用了注解式样式):2.自定义拦截器3.代码4.最终效果 前言 本文简单介绍阿里的EasyExcel的复杂导出操作,包括自定义样式,根据数据合并单元格等。…

文章目录

  • 前言
  • 一、自定义拦截器
  • 二、自定义操作
    • 1.自定义颜色
    • 2.合并单元格
  • 三、复杂操作示例
    • 1.实体(使用了注解式样式):
    • 2.自定义拦截器
    • 3.代码
    • 4.最终效果


前言

本文简单介绍阿里的EasyExcel的复杂导出操作,包括自定义样式,根据数据合并单元格等。

点击查看EasyExcel官方文档


一、自定义拦截器

要实现复杂导出,靠现有的拦截器怕是不大够用,EasyExcel 已经有提供部分像是 自定义样式的策略HorizontalCellStyleStrategy
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
通过源码,我们不难发现其原理正是实现了拦截器接口,使用了afterCellDispose方法,在数据写入单元格后会调用该方法,因此,需要进行复杂操作,我们需要自定义拦截器,在afterCellDispose方法进行逻辑处理,其中我们可以通过context参数获取到表,行,列及单元格数据等信息:
在这里插入图片描述

二、自定义操作

1.自定义颜色

由于WriteCellStyle 及CellStyle接口的设置单元格背景颜色方法setFillForegroundColor不支持自定义颜色,我在网上找了半天,以及询问阿里自家ai助手通义得到的答案都是往里塞一个XSSFColor这样的答案,但这个方法传参是一个short类型的index呀,是预设好的颜色,里面也没有找到其他重载方法。(这里针对的是导出xlsx文件)

在这里插入图片描述

而真正可以自定义颜色的是XSSFCellStyle类,XSSFCellStyle实现CellStyle接口,并重载了该方法,于是我们只需要在workbook.createCellStyle()的时候将其强转为XSSFCellStyle:

// 将背景设置成浅蓝色
XSSFColor customColor = new XSSFColor(new java.awt.Color(181, 198, 234), null);
XSSFCellStyle style = (XSSFCellStyle)workbook.createCellStyle();
style.setFillForegroundColor(customColor);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);

在idea我们可以使用 ctrl + alt + 鼠标点击接口,来查看接口的所有实现类(HSSF是针对xls的):

在这里插入图片描述

然而在我们自定义的拦截器中,操作当前单元格样式时会无法生效,这是因为在3.1.x版本后有一个FillStyleCellWriteHandler拦截器,他会把OriginCellStyle和WriteCellStyle合并,会已WriteCellStyle样式为主,他的order是50000,而我们自定义的拦截器默认是0,因此我们修改的样式会被覆盖。
在这里插入图片描述
在这里插入图片描述
解决方法很简单,我们可以在我们的自定义拦截器重写order方法,将其值设置大于50000即可

  @Overridepublic int order() {return 50001;}

如果你没有使用自定义拦截器(如HorizontalCellStyleStrategy )以及没有设置WriteCellStyle 样式,则还可以将ignoreFillStyle置为true,

 @Overridepublic void afterCellDispose(CellWriteHandlerContext context) {context.setIgnoreFillStyle(true);// 做其他样式操作}

2.合并单元格

 ```java@Overridepublic void afterCellDispose(CellWriteHandlerContext context) {// 判断当前为表头,不执行操作if (isHead) {log.info("\r\n当前为表头, 不执行操作");return;}// 获取当前单元格context.getCell()// 当前 SheetSheet sheet = cell.getSheet();// 当前单元格所在行索引int rowIndexCurr = cell.getRowIndex();// 当前单元格所在列索引int columnIndex = cell.getColumnIndex();// 当前单元格所在行的上一行索引int rowIndexPrev = rowIndexCurr - 1;// 当前单元格所在行的 Row 对象Row rowCurr = cell.getRow();// 当前单元格所在行的上一行 Row 对象Row rowPrev = sheet.getRow(rowIndexPrev);// 当前单元格的上一行同列单元格Cell cellPrev = rowPrev.getCell(columnIndex);// 合并同列不同行的相邻两个单元格sheet.addMergedRegion(new CellRangeAddress(rowIndexPrev, rowIndexCurr,columnIndex, columnIndex));}

需要注意的是,如果要合并的单元格已经被其他单元格合并过,则不能直接使用这个合并方法,需要先解除合并,再进行组合合并:

 // 从 Sheet 中,获取所有合并区域List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();// 判断是否合并过boolean merged = false;// 遍历合并区域集合for (int i = 0; i < mergedRegions.size(); i++) {CellRangeAddress cellAddresses = mergedRegions.get(i);// 判断 cellAddress 的范围是否是从 rowIndexPrev 到 cell.getColumnIndex()if (cellAddresses.isInRange(rowIndexPrev, cell.getColumnIndex())) {// 解除合并sheet.removeMergedRegion(i);// 设置范围最后一行,为当前行cellAddresses.setLastRow(rowIndexCurr);// 重新进行合并sheet.addMergedRegion(cellAddresses);merged = true;break;}}// merged=false,表示当前单元格为第一次合并if (!merged) {CellRangeAddress cellAddresses = new CellRangeAddress(rowIndexPrev, rowIndexCurr, cell.getColumnIndex(), cell.getColumnIndex());sheet.addMergedRegion(cellAddresses);}

三、复杂操作示例

自定义拦截器代码如下(示例):

1.实体(使用了注解式样式):

package com.mhqs.demo.tool.easyExcel.entity;import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
import lombok.EqualsAndHashCode;import java.math.BigDecimal;/*** 账单实体类* @author 棉花* */
@Data
@EqualsAndHashCode(callSuper = false)
@HeadFontStyle(fontHeightInPoints = 10)
@HeadRowHeight(27)
@ColumnWidth(13)
@ContentFontStyle(fontName = "宋体",fontHeightInPoints = 11)
public class DemoEntity extends EasyExcelEntity {@ExcelProperty({"账期"})private String settlePeriod;@ExcelProperty({"服务商"})private String stockCreatorMchid;@ExcelProperty({"地区"})private String place;@ExcelProperty({"金额(元)"})private BigDecimal consumeAmount;public DemoEntity(String settlePeriod, String stockCreatorMchid,String place, BigDecimal consumeAmount){this.settlePeriod = settlePeriod;this.stockCreatorMchid = stockCreatorMchid;this.place = place;this.consumeAmount = consumeAmount;}}

2.自定义拦截器

package com.mhqs.demo.tool.easyExcel.handler;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.mhqs.demo.tool.easyExcel.entity.DemoEntity;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;import java.math.BigDecimal;
import java.util.*;/*** @author bcb* 账单导出样式处理*/
public class CustomCellWriteHandler implements CellWriteHandler {/*** 自定义颜色*/private final java.awt.Color color;/*** 自定义颜色样式*/private CellStyle colorfulCellStyle;/*** 自定义特殊金额样式*/private CellStyle specialCellStyle;/*** 头样式*/private final WriteCellStyle headWriteCellStyle;/*** 内容样式*/private final WriteCellStyle contentWriteCellStyle;/*** 头样式(可自定义颜色)*/private CellStyle headCellStyle;/*** 内容样式(可自定义颜色)*/private CellStyle contentCellStyle;public CustomCellWriteHandler(WriteCellStyle headWriteCellStyle,WriteCellStyle contentWriteCellStyle, java.awt.Color color) {this.headWriteCellStyle = headWriteCellStyle;this.contentWriteCellStyle = contentWriteCellStyle;this.color = color;}@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {// 在创建单元格之前的操作(如果需要)Workbook workbook = writeSheetHolder.getSheet().getWorkbook();if (colorfulCellStyle == null) {colorfulCellStyle = createColorfulCellStyle(workbook);}// 合并样式(以WriteCellStyle为主)headCellStyle = StyleUtil.buildCellStyle(workbook, colorfulCellStyle, headWriteCellStyle);contentCellStyle = StyleUtil.buildCellStyle(workbook, workbook.createCellStyle(), contentWriteCellStyle);}/** 创建自定义颜色样式*/private CellStyle createColorfulCellStyle(Workbook workbook) {XSSFColor customColor = new XSSFColor(color, null);XSSFCellStyle style = (XSSFCellStyle)workbook.createCellStyle();// 设置自定义颜色style.setFillForegroundColor(customColor);style.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 设置边框style.setBorderTop(BorderStyle.THIN);style.setBorderBottom(BorderStyle.THIN);style.setBorderLeft(BorderStyle.THIN);style.setBorderRight(BorderStyle.THIN);// 设置垂直对齐方式style.setVerticalAlignment(VerticalAlignment.CENTER);// 设置水平对齐方式style.setAlignment(HorizontalAlignment.CENTER);return style;}/** 创建自定义特殊金额样式*/private CellStyle createSpecialCellStyle(Workbook workbook) {if (specialCellStyle == null) {XSSFCellStyle style = (XSSFCellStyle)createColorfulCellStyle(workbook);Font font = workbook.createFont();// 字体加粗font.setBold(true);style.setFont(font);specialCellStyle = style;}return specialCellStyle;}/*** 在 Cell 写入后处理** @param writeSheetHolder* @param writeTableHolder* @param cellDataList* @param cell               当前 Cell* @param head* @param relativeRowIndex   表格内容行索引,从除表头的第一行开始,索引为0* @param isHead             是否是表头,true表头,false非表头*/@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {// 当前 SheetSheet sheet = cell.getSheet();// 判断当前为表头,执行对应样式操作if (isHead) {cell.setCellStyle(headCellStyle);} else {cell.setCellStyle(contentCellStyle);}// 判断当前为表头,不执行操作if (isHead || relativeRowIndex == 0) {return;}int columnIndex = cell.getColumnIndex();// 当前 Cell 所在行索引int rowIndexCurr = cell.getRowIndex();// 当前 Cell 所在行的上一行索引int rowIndexPrev = rowIndexCurr - 1;// 当前 Cell 所在行的 Row 对象Row rowCurr = cell.getRow();// 当前 Cell 所在行的上一行 Row 对象Row rowPrev = sheet.getRow(rowIndexPrev);// 当前单元格的上一行同列单元格Cell cellPrev = rowPrev.getCell(columnIndex);// 当前单元格的值Object cellValueCurr = cell.getCellType() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();if (columnIndex == 3 && cellValueCurr != null && (double)cellValueCurr > 200) {// 判断金额大于200就设置特定颜色并加粗,并将上一列同一行的数据也设置特定颜色CellStyle cellStyle = createSpecialCellStyle(sheet.getWorkbook());cell.setCellStyle(cellStyle);// 当前单元格的同行上一列单元格Cell cellPreC = rowCurr.getCell(columnIndex - 1);cellPreC.setCellStyle(colorfulCellStyle);}// 上面单元格的值Object cellValuePrev = cellPrev.getCellType() == CellType.STRING ? cellPrev.getStringCellValue() : cellPrev.getNumericCellValue();/** 只判断前两列相同行数据*/if (columnIndex != 0 && columnIndex != 1) {return;}// 判断当前单元格与上面单元格是否相等,不相等不执行操作if (!cellValueCurr.equals(cellValuePrev)) {return;}/** 当第一列上下两个单元格不一样时,说明不是一个账期数据*/if (!rowPrev.getCell(0).getStringCellValue().equals(rowCurr.getCell(0).getStringCellValue())) {return;}// 从 Sheet 中,获取所有合并区域List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();// 是否合并过boolean merged = false;// 遍历合并区域集合for (int i = 0; i < mergedRegions.size(); i++) {CellRangeAddress cellAddresses = mergedRegions.get(i);//判断 cellAddress 的范围是否是从 rowIndexPrev 到 cell.getColumnIndex()if (cellAddresses.isInRange(rowIndexPrev, columnIndex)) {// 从集合中移除sheet.removeMergedRegion(i);// 设置范围最后一行,为当前行cellAddresses.setLastRow(rowIndexCurr);// 重新添加到 Sheet 中sheet.addMergedRegion(cellAddresses);// 已完成合并merged = true;break;}}// merged=false,表示当前单元格为第一次合并if (!merged) {CellRangeAddress cellAddresses = new CellRangeAddress(rowIndexPrev, rowIndexCurr, columnIndex, columnIndex);sheet.addMergedRegion(cellAddresses);}}/*** 获取当前处理器优先级*/@Overridepublic int order() {return 50001;}}

3.代码

public static void main(String[] args) {String fileName = "D:\\temp\\账单.xlsx";// 设置 Cell 样式WriteCellStyle writeCellStyle = new WriteCellStyle();// 设置垂直对齐方式writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 设置水平对齐方式writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 设置边框writeCellStyle.setBorderTop(BorderStyle.THIN);writeCellStyle.setBorderBottom(BorderStyle.THIN);writeCellStyle.setBorderLeft(BorderStyle.THIN);writeCellStyle.setBorderRight(BorderStyle.THIN);// 自定义颜色java.awt.Color color = new java.awt.Color(181, 198, 234);List<DemoEntity> dataList = new ArrayList<>();for (int i = 0; i < 5; i++) {dataList.add(new DemoEntity("202301","服务商" + i%2,"地区" + i,new BigDecimal(i * 100)));}dataList.sort(Comparator.comparing(DemoEntity::getSettlePeriod).thenComparing(DemoEntity::getStockCreatorMchid));ExcelWriter excelWriter = EasyExcel.write(fileName, DemoEntity.class).build();WriteSheet writeSheet = EasyExcel.writerSheet(0, "账单").registerWriteHandler(new CustomCellWriteHandler(null,writeCellStyle,color)).build();excelWriter.write(dataList, writeSheet);// 需要多sheet则可以继续// WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "第二个sheet")excelWriter.finish();}

4.最终效果

在这里插入图片描述

待续…


参考文章:
easyexcel 3.1.0+,设置RBG背景颜色
EasyExcel导出多sheet并设置单元格样式
EasyExcel的CellWriteHandler注入CellStyle不生效


文章转载自:
http://hydrastinine.sqLh.cn
http://videoplayer.sqLh.cn
http://sondage.sqLh.cn
http://antelucan.sqLh.cn
http://synchroflash.sqLh.cn
http://scrupulousness.sqLh.cn
http://interplait.sqLh.cn
http://sheargrass.sqLh.cn
http://unharness.sqLh.cn
http://leathercraft.sqLh.cn
http://cabin.sqLh.cn
http://hydrogeology.sqLh.cn
http://kirk.sqLh.cn
http://noel.sqLh.cn
http://unhat.sqLh.cn
http://leishmania.sqLh.cn
http://omnipotence.sqLh.cn
http://galliambic.sqLh.cn
http://zoogamete.sqLh.cn
http://entablement.sqLh.cn
http://numbness.sqLh.cn
http://hilch.sqLh.cn
http://asbestosis.sqLh.cn
http://jeerer.sqLh.cn
http://bacteriolytic.sqLh.cn
http://thumbmark.sqLh.cn
http://autocratic.sqLh.cn
http://geosphere.sqLh.cn
http://prartition.sqLh.cn
http://epitomize.sqLh.cn
http://afterglow.sqLh.cn
http://ladybird.sqLh.cn
http://honda.sqLh.cn
http://absolve.sqLh.cn
http://kin.sqLh.cn
http://seamark.sqLh.cn
http://grime.sqLh.cn
http://welshman.sqLh.cn
http://printmaker.sqLh.cn
http://pentastyle.sqLh.cn
http://wide.sqLh.cn
http://nakhodka.sqLh.cn
http://biceps.sqLh.cn
http://institutional.sqLh.cn
http://engine.sqLh.cn
http://bye.sqLh.cn
http://conakry.sqLh.cn
http://hyphal.sqLh.cn
http://falsidical.sqLh.cn
http://comicality.sqLh.cn
http://birotation.sqLh.cn
http://crossarm.sqLh.cn
http://electroacoustic.sqLh.cn
http://compilatory.sqLh.cn
http://hydrolyse.sqLh.cn
http://opercula.sqLh.cn
http://noncellular.sqLh.cn
http://happily.sqLh.cn
http://seventeen.sqLh.cn
http://selectron.sqLh.cn
http://simba.sqLh.cn
http://loculicidal.sqLh.cn
http://synactic.sqLh.cn
http://emblements.sqLh.cn
http://pachytene.sqLh.cn
http://yorktown.sqLh.cn
http://permutation.sqLh.cn
http://enterohepatitis.sqLh.cn
http://pronominal.sqLh.cn
http://neutretto.sqLh.cn
http://safen.sqLh.cn
http://serein.sqLh.cn
http://humane.sqLh.cn
http://cervine.sqLh.cn
http://lungan.sqLh.cn
http://hypothec.sqLh.cn
http://refit.sqLh.cn
http://wnp.sqLh.cn
http://cetologist.sqLh.cn
http://dahabeeyah.sqLh.cn
http://cheater.sqLh.cn
http://gomorrah.sqLh.cn
http://aspersion.sqLh.cn
http://crankish.sqLh.cn
http://oneirocritic.sqLh.cn
http://histogenic.sqLh.cn
http://alunite.sqLh.cn
http://priestling.sqLh.cn
http://xenomorphic.sqLh.cn
http://amatory.sqLh.cn
http://timbre.sqLh.cn
http://unifilar.sqLh.cn
http://feoffment.sqLh.cn
http://imperialistic.sqLh.cn
http://tangly.sqLh.cn
http://cockeyed.sqLh.cn
http://empale.sqLh.cn
http://liftback.sqLh.cn
http://baddeleyite.sqLh.cn
http://comer.sqLh.cn
http://www.15wanjia.com/news/94720.html

相关文章:

  • wordpress首页默认文件夹上海企业seo
  • 怎样做读书会网站网站换友链平台
  • 制作公司网站备案需要提供什么资料友情链接例子
  • 秦皇岛网站制作源码全球搜钻
  • 没有网站可以做淘宝客东莞做网站公司首选
  • vi设计是啥意思宁波网站制作优化服务
  • 叙述网站建设的流程湖人最新排名最新排名
  • 个人创建网站程序怎么开网店新手入门
  • 专门做环保设备的网站网站关键词优化软件
  • 泰安百度推广代理专业网站优化公司
  • 网站开发人员如何写工作日志seo网站培训
  • 大淘客做的网站可以吗谷歌关键词排名查询工具
  • 个人网站如何做流量百度指数在线查询小程序
  • 南阳网站seo公司希爱力跟万艾可哪个猛
  • 网站备案信息查询申请成都关键词优化排名
  • 陕西网站建设公司全域seo
  • 做外贸常用的网站房地产销售
  • 网站开发做什么的网站服务器信息查询
  • 做外汇网站代理商青岛seo经理
  • 南京市的网站是由那几家公司做的线上广告投放渠道
  • java开发网站跟php开发网站区别杭州seo招聘
  • 佛山seo网站排名怎样淘宝seo排名优化
  • 找个美工做淘宝网站需要多少钱上海快速排名优化
  • a设计网站有哪些如何给公司做网络推广
  • 可以做私募股权投资的网站朋友圈推广
  • 杭州好的做网站公司网站优化外包找谁
  • 网站常用颜色会计培训机构排名
  • 网站建设心得.doc南京seo优化
  • 做地方生活网站突发大事震惊全国
  • 外军网站建设优化大师下载安装app