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

门户建设网站沈阳线上教学

门户建设网站,沈阳线上教学,成都企业模版网站建设,专门查大学的网站在EasyExcel中自定义拦截器不仅可以帮助我们不止步于数据的填充,而且可以对样式、单元格合并等带来便捷的功能。下面直接开始 我们定义一个MergeWriteHandler的类继承AbstractMergeStrategy实现CellWriteHandler public class MergeLastWriteHandler extends Abst…

在EasyExcel中自定义拦截器不仅可以帮助我们不止步于数据的填充,而且可以对样式、单元格合并等带来便捷的功能。下面直接开始

我们定义一个MergeWriteHandler的类继承AbstractMergeStrategy实现CellWriteHandler

public class MergeLastWriteHandler extends AbstractMergeStrategy implements CellWriteHandler 

当中我们重写merge方法

@Overrideprotected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {}

我们可以在重写的方法中得到形参中的Cel,那我们可以通过调用cell.getStringCellValue()得到当前单元格的内容,判断当前单元格的内容是否是目标单元格,例如下面代码

if (cell.getStringCellValue().equals("说明")) {cell.setCellValue("说明:这是一条说明");//获取表格最后一行int lastRowNum = sheet.getLastRowNum();CellRangeAddress region = new CellRangeAddress(lastRowNum, lastRowNum, 0, 5);sheet.addMergedRegionUnsafe(region);}

并且这里我们通过sheet中的 getLastRowNum()获取最后一行,最终通过CellRangeAddress来进行单元格合并,从下面源码我们可以了解到合并的规则是什么(通过int firstRow, int lastRow, int firstCol, int lastCol)

/*** Creates new cell range. Indexes are zero-based.** @param firstRow Index of first row* @param lastRow Index of last row (inclusive), must be equal to or larger than {@code firstRow}* @param firstCol Index of first column* @param lastCol Index of last column (inclusive), must be equal to or larger than {@code firstCol}*/public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {super(firstRow, lastRow, firstCol, lastCol);if (lastRow < firstRow || lastCol < firstCol) {throw new IllegalArgumentException("Invalid cell range, having lastRow < firstRow || lastCol < firstCol, " +"had rows " + lastRow + " >= " + firstRow + " or cells " + lastCol + " >= " + firstCol);}}

这样就可以实现合并单元格,不过这里可能会出现一个问题

java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cellat org.apache.poi.xssf.streaming.SXSSFCell.typeMismatch(SXSSFCell.java:943)at org.apache.poi.xssf.streaming.SXSSFCell.getStringCellValue(SXSSFCell.java:460)

因为会访问所有的单元格,有可能会出现是不是字符串类型的单元格,所以我们最好在开始的时候对其进行处理一次

if (cell.getCellType().equals(CellType.NUMERIC)){double numericCellValue = cell.getNumericCellValue();String s = Double.toString(numericCellValue);String substring = s.substring(0, s.indexOf("."));cell.setCellValue(substring);}

但这里可能我们还需要一个操作,例如如果我们全局配置了表框线条,但是不想当前的单元格有线条,如何处理呢,定义CustomCellWriteHandler拦截器继承AbstractCellWriteHandler

public class CustomCellWriteHandler extends AbstractCellWriteHandler{}

重写当中的afterCellDispose方法,得到

 @Overridepublic void afterCellDispose(CellWriteHandlerContext context) {super.afterCellDispose(context);}

现在我们对其进行操作

  Cell cell = context.getCell();if(BooleanUtils.isNotTrue(context.getHead())){if(cell.getStringCellValue().contains("说明"))){Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();CellStyle cellStyle = workbook.createCellStyle();cellStyle.setBorderTop(BorderStyle.THIN);cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setAlignment(HorizontalAlignment.LEFT);cell.setCellStyle(cellStyle);context.getFirstCellData().setWriteCellStyle(null); //关键代码,不设置不生效}
}

最后只需要在写入的时候,把拦截器放进去就可以了,看完整代码

public class CustomCellWriteHandler extends AbstractCellWriteHandler {@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {short height = 600;row.setHeight(height);}@Overridepublic void afterCellDispose(CellWriteHandlerContext context) {Cell cell = context.getCell();if(BooleanUtils.isNotTrue(context.getHead())){if(cell.getStringCellValue().contains("说明"))){Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();CellStyle cellStyle = workbook.createCellStyle();cellStyle.setBorderTop(BorderStyle.THIN);cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setAlignment(HorizontalAlignment.LEFT);cell.setCellStyle(cellStyle);context.getFirstCellData().setWriteCellStyle(null);}}super.afterCellDispose(context);}
}

合并单元格的拦截器

public class MergeLastWriteHandler extends AbstractMergeStrategy implements CellWriteHandler {public static HorizontalCellStyleStrategy getStyleStrategy() {// 头的策略WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 设置对齐//headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);// 背景色, 设置为绿色,也是默认颜色headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());// 字体//WriteFont headWriteFont = new WriteFont();//headWriteFont.setFontHeightInPoints((short) 12);//headWriteCellStyle.setWriteFont(headWriteFont);// 内容的策略WriteCellStyle contentWriteCellStyle = new WriteCellStyle();// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定// contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);// 字体策略WriteFont contentWriteFont = new WriteFont();//contentWriteFont.setFontHeightInPoints((short) 12);contentWriteCellStyle.setWriteFont(contentWriteFont);//设置 自动换行contentWriteCellStyle.setWrapped(true);//设置 垂直居中contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置 水平居中contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置边框样式contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);contentWriteCellStyle.setBorderTop(BorderStyle.THIN);contentWriteCellStyle.setBorderRight(BorderStyle.THIN);contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);return horizontalCellStyleStrategy;}@Overrideprotected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {if (cell.getCellType() == CellType.NUMERIC) {double numericCellValue = cell.getNumericCellValue();String s = Double.toString(numericCellValue);String substring = s.substring(0, s.indexOf("."));cell.setCellValue(substring);}if (cell.getStringCellValue().equals("说明")) {cell.setCellValue("说明:这是一条说明");//获取表格最后一行int lastRowNum = sheet.getLastRowNum();CellRangeAddress region = new CellRangeAddress(lastRowNum, lastRowNum, 0, 5);sheet.addMergedRegionUnsafe(region);}}
}

看一下Controller层

@GetMapping("/excelWrapper")public void excelWrapper(HttpServletResponse response) throws IOException {try {List<User> userList =  DataByExcel();  //获取数据的列表List<BudgetForm> budgetForm = BeanUtil.copyToList(userList,BudgetForm.class);String fileName = one.getProjectName() + ".xlsx";response.setContentType("application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName );// 创建ExcelWriter对象WriteSheet writeSheet = EasyExcel.writerSheet("表格sheet").registerWriteHandler(new MergeLastWriteHandler()).registerWriteHandler(new CustomCellWriteHandler()).build();// 向Excel中写入数据ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), BudgetForm.class).build();excelWriter.write(userList , writeSheet);// 关闭流excelWriter.finish();} catch (Exception e) {e.printStackTrace();}}

对表头设置(自己对应表格设置对应字段)

@Data
@ContentRowHeight(47) //内容行高
@HeadRowHeight(35)//表头行高
public class BudgetForm implements Serializable  {@ColumnWidth(6)@ExcelProperty(value ={"表格","序号"} ,index = 0)@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)private Integer serialNumber;@ColumnWidth(15)@ExcelProperty(value ={"表格","名称"} ,index = 1)@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)private String name;@ColumnWidth(26)@ExcelProperty(value = {"表格","备注"},index = 2)@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)private String remark;@ColumnWidth(26)@ExcelProperty(value = {"表格","其他"},index = 3)@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)private String budgetary;@ExcelIgnore@ApiModelProperty("合计")private String total;}


文章转载自:
http://hypochlorite.c7624.cn
http://barret.c7624.cn
http://imidazole.c7624.cn
http://napoleonize.c7624.cn
http://hypogastric.c7624.cn
http://leech.c7624.cn
http://saltire.c7624.cn
http://reengineer.c7624.cn
http://merosymmetry.c7624.cn
http://montpellier.c7624.cn
http://gummosis.c7624.cn
http://musicianly.c7624.cn
http://campion.c7624.cn
http://wormy.c7624.cn
http://capulet.c7624.cn
http://heterogeny.c7624.cn
http://parallelepiped.c7624.cn
http://heathy.c7624.cn
http://rosicrucian.c7624.cn
http://hemialgia.c7624.cn
http://bangalore.c7624.cn
http://crowded.c7624.cn
http://jai.c7624.cn
http://agrestic.c7624.cn
http://pyorrhoea.c7624.cn
http://furor.c7624.cn
http://circumstanced.c7624.cn
http://lotion.c7624.cn
http://scioptic.c7624.cn
http://osp.c7624.cn
http://intimism.c7624.cn
http://sackcloth.c7624.cn
http://unoffended.c7624.cn
http://fleshly.c7624.cn
http://phocomelia.c7624.cn
http://ikebana.c7624.cn
http://cabomba.c7624.cn
http://woefully.c7624.cn
http://turco.c7624.cn
http://luteous.c7624.cn
http://invulnerability.c7624.cn
http://strut.c7624.cn
http://friable.c7624.cn
http://dou.c7624.cn
http://galumph.c7624.cn
http://gadsbodikins.c7624.cn
http://raptatorial.c7624.cn
http://emanatory.c7624.cn
http://diaphanous.c7624.cn
http://uniformly.c7624.cn
http://aeg.c7624.cn
http://bowpot.c7624.cn
http://inflorescent.c7624.cn
http://transshape.c7624.cn
http://tapadera.c7624.cn
http://gryke.c7624.cn
http://midship.c7624.cn
http://kituba.c7624.cn
http://colored.c7624.cn
http://gso.c7624.cn
http://snr.c7624.cn
http://martinmas.c7624.cn
http://directorship.c7624.cn
http://jo.c7624.cn
http://merchandising.c7624.cn
http://carefree.c7624.cn
http://rubytail.c7624.cn
http://rulable.c7624.cn
http://bouncy.c7624.cn
http://shick.c7624.cn
http://isothere.c7624.cn
http://transoceanic.c7624.cn
http://pyruvate.c7624.cn
http://butskellism.c7624.cn
http://vibrometer.c7624.cn
http://tegucigalpa.c7624.cn
http://bionic.c7624.cn
http://mollisol.c7624.cn
http://nasal.c7624.cn
http://switchgrass.c7624.cn
http://plumbeous.c7624.cn
http://scleromyxoedema.c7624.cn
http://advise.c7624.cn
http://fadedly.c7624.cn
http://barcarole.c7624.cn
http://carotene.c7624.cn
http://quadriphonic.c7624.cn
http://bisulfide.c7624.cn
http://pastern.c7624.cn
http://suberose.c7624.cn
http://oxytone.c7624.cn
http://pushpin.c7624.cn
http://archenemy.c7624.cn
http://frass.c7624.cn
http://montenegrin.c7624.cn
http://revivalism.c7624.cn
http://synosteosis.c7624.cn
http://loathy.c7624.cn
http://chancy.c7624.cn
http://corium.c7624.cn
http://www.zhongyajixie.com/news/90528.html

相关文章:

  • 零食类营销网站怎么做如何在百度发布广告
  • 密云青岛网站建设网站推广优化业务
  • 电商网站开发流程下店拓客团队
  • 手机p2p网站建设百度怎么发布广告
  • 深圳自适应网站建设报价网络营销推广方案模板
  • 织梦怎么做淘客网站搜索引擎推广方案
  • 做网站副业山东网站seo推广优化价格
  • 极速网站开发网络营销课程培训机构
  • 龙江网站建设公司天眼查企业查询入口
  • 大型网络游戏排行榜2021前十名苏州seo排名优化课程
  • 建站公司 源码申请百度竞价推广什么意思
  • 江苏网站建设yijuceseo诊断优化专家
  • 网站功能需求列表销售外包
  • 行唐县网站建设公司电销外包团队在哪找
  • 西安哪家做网站好百度非企推广开户
  • 毛片a做片在线观看网站爱站工具包官网下载
  • 乌兰察布做网站的公司精准引流的网络推广方法
  • 缙云建设局网站品牌运营策略有哪些
  • 新闻网站系统源代码查网址
  • 怎么用记事本做网站少女长尾关键词挖掘
  • 有没有做图的网站站点
  • 北京市城乡建设部网站首页网站建设费用都选网络
  • 河南建筑公司排名青岛seo
  • 手机端网站怎么做网络推广好做吗多少钱
  • 重庆网站设计生产厂家招聘网站排名
  • 做网站要多少的服务器seo视频网页入口网站推广
  • 赤水网站建设免费推广的网站有哪些
  • 网站素材包括哪些广州最新重大新闻
  • 网站如何做百度才会收录网站建设方案书范文
  • 企业网站建设服务今天的新闻联播