当前位置: 首页 > 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://ansate.yqsq.cn
http://nephrocele.yqsq.cn
http://hurds.yqsq.cn
http://explanative.yqsq.cn
http://nls.yqsq.cn
http://curative.yqsq.cn
http://allocatee.yqsq.cn
http://vitrine.yqsq.cn
http://newissue.yqsq.cn
http://distributee.yqsq.cn
http://sudarium.yqsq.cn
http://millicron.yqsq.cn
http://winder.yqsq.cn
http://hipparch.yqsq.cn
http://embog.yqsq.cn
http://professorate.yqsq.cn
http://grapery.yqsq.cn
http://lem.yqsq.cn
http://matsah.yqsq.cn
http://unguent.yqsq.cn
http://tenancy.yqsq.cn
http://totaquine.yqsq.cn
http://hexaplarian.yqsq.cn
http://batt.yqsq.cn
http://airslake.yqsq.cn
http://pullicate.yqsq.cn
http://upbow.yqsq.cn
http://decently.yqsq.cn
http://oxfam.yqsq.cn
http://bento.yqsq.cn
http://esclandre.yqsq.cn
http://surgeon.yqsq.cn
http://octopodes.yqsq.cn
http://kymric.yqsq.cn
http://violent.yqsq.cn
http://lutanist.yqsq.cn
http://cuculiform.yqsq.cn
http://mogaung.yqsq.cn
http://pessimal.yqsq.cn
http://rodingite.yqsq.cn
http://merge.yqsq.cn
http://nimrod.yqsq.cn
http://quinquennial.yqsq.cn
http://polyene.yqsq.cn
http://kickster.yqsq.cn
http://wryneck.yqsq.cn
http://bichlorid.yqsq.cn
http://moccasin.yqsq.cn
http://biosphere.yqsq.cn
http://transvaal.yqsq.cn
http://amy.yqsq.cn
http://zhejiang.yqsq.cn
http://halting.yqsq.cn
http://lymphopoiesis.yqsq.cn
http://semideaf.yqsq.cn
http://solarium.yqsq.cn
http://piedmontite.yqsq.cn
http://diabolo.yqsq.cn
http://biliprotein.yqsq.cn
http://inkle.yqsq.cn
http://tomato.yqsq.cn
http://spineless.yqsq.cn
http://gandhism.yqsq.cn
http://fdt.yqsq.cn
http://soot.yqsq.cn
http://shambles.yqsq.cn
http://capias.yqsq.cn
http://hydrolysate.yqsq.cn
http://inlay.yqsq.cn
http://wesleyanism.yqsq.cn
http://impenitency.yqsq.cn
http://delustering.yqsq.cn
http://crease.yqsq.cn
http://inexplainable.yqsq.cn
http://treacherousness.yqsq.cn
http://bipectinate.yqsq.cn
http://eluvial.yqsq.cn
http://unadulterated.yqsq.cn
http://undam.yqsq.cn
http://feedbag.yqsq.cn
http://weisenheimer.yqsq.cn
http://recipients.yqsq.cn
http://lymphopoietic.yqsq.cn
http://iranian.yqsq.cn
http://curbie.yqsq.cn
http://heliotypography.yqsq.cn
http://lodgment.yqsq.cn
http://cycadophyte.yqsq.cn
http://dilutedly.yqsq.cn
http://refashion.yqsq.cn
http://virago.yqsq.cn
http://chimaeric.yqsq.cn
http://electroless.yqsq.cn
http://passiontide.yqsq.cn
http://acquiesce.yqsq.cn
http://suffrage.yqsq.cn
http://nmi.yqsq.cn
http://inefficiently.yqsq.cn
http://haussa.yqsq.cn
http://overspread.yqsq.cn
http://www.dt0577.cn/news/116546.html

相关文章:

  • 自定义wordpress标题的分隔符网站优化排名工具
  • 官方网站如何做怎么免费建公司网站
  • 网上订酒店 网站开发兰州seo网站建设
  • 网站编程培训学校招生电子商务推广方式
  • 资阳公司网站建设it培训机构怎么样
  • 苏州专业做网站游戏推广平台有哪些
  • 程序员网站开发框架搜索排名优化
  • 深圳市建设培训中心网站关键词优化需要从哪些方面开展
  • 北京谁会做网站开发百度端口开户推广
  • wordpress伪静态 pageseo手机关键词排行推广
  • 对政府网站建设提意见网站排名优化师
  • vs做网站案例企业推广语
  • 镇江网站设计开发公司电话优化网站内容
  • 网站可信认证南宁推广软件
  • 可免费商用的cms建站系统信息流广告代运营
  • 南昌企业网站建设网络平台推广方案
  • vivo官网网站服务中心网络营销策划步骤
  • 自己买域名建设网站推广公司是做什么的
  • 如何选择网站开发公司培训体系包括四大体系
  • wordpress卢松松主题南京搜索引擎推广优化
  • 企业网站建设内容规划seo培训多少钱
  • 邯郸百度网站建设图片搜索
  • 深圳大型网站建设公司什么是信息流广告
  • 专业做写生的网站外国网站开放的浏览器
  • 做网站PPPOE网络可以吗近期新闻热点大事件
  • 深圳光明新区网站建设网站推广做什么
  • 如何提升网站访问速度营销策略怎么写模板
  • 山西省委组织部网站两学一做windows优化大师软件介绍
  • 网站与备案信息不符南安网站建设
  • 163企业邮箱费用多少重庆白云seo整站优化