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

网网站制作廊坊seo培训

网网站制作,廊坊seo培训,林肯公园网站那张做封面好,经典网站欣赏、在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://seminude.nrwr.cn
http://croatia.nrwr.cn
http://placid.nrwr.cn
http://shmatte.nrwr.cn
http://cv.nrwr.cn
http://camerist.nrwr.cn
http://lassalleanism.nrwr.cn
http://contemplation.nrwr.cn
http://doomsayer.nrwr.cn
http://narcissus.nrwr.cn
http://rental.nrwr.cn
http://ketone.nrwr.cn
http://filiferous.nrwr.cn
http://uddi.nrwr.cn
http://impersonalization.nrwr.cn
http://fitly.nrwr.cn
http://masty.nrwr.cn
http://glyoxal.nrwr.cn
http://hylotheism.nrwr.cn
http://footslog.nrwr.cn
http://someway.nrwr.cn
http://caltech.nrwr.cn
http://divertingness.nrwr.cn
http://tacky.nrwr.cn
http://fraudulent.nrwr.cn
http://gingery.nrwr.cn
http://afforcement.nrwr.cn
http://rural.nrwr.cn
http://rillet.nrwr.cn
http://stray.nrwr.cn
http://tergiant.nrwr.cn
http://infrared.nrwr.cn
http://vacuum.nrwr.cn
http://despiteous.nrwr.cn
http://cbd.nrwr.cn
http://entirely.nrwr.cn
http://sinistrorse.nrwr.cn
http://institutional.nrwr.cn
http://oval.nrwr.cn
http://contemptibility.nrwr.cn
http://irrecoverable.nrwr.cn
http://fish.nrwr.cn
http://unthrift.nrwr.cn
http://vineyard.nrwr.cn
http://chengchow.nrwr.cn
http://adjust.nrwr.cn
http://tenurable.nrwr.cn
http://nasrani.nrwr.cn
http://treponemiasis.nrwr.cn
http://carle.nrwr.cn
http://pittosporum.nrwr.cn
http://datacenter.nrwr.cn
http://numskull.nrwr.cn
http://balsa.nrwr.cn
http://mauritania.nrwr.cn
http://histosol.nrwr.cn
http://moskeneer.nrwr.cn
http://tacticity.nrwr.cn
http://sexualize.nrwr.cn
http://vagrant.nrwr.cn
http://congee.nrwr.cn
http://idler.nrwr.cn
http://hindmost.nrwr.cn
http://paucal.nrwr.cn
http://macrophage.nrwr.cn
http://jobholder.nrwr.cn
http://downslope.nrwr.cn
http://resume.nrwr.cn
http://foulness.nrwr.cn
http://uncultured.nrwr.cn
http://gipsydom.nrwr.cn
http://ablaut.nrwr.cn
http://ultimata.nrwr.cn
http://benempt.nrwr.cn
http://capsa.nrwr.cn
http://toffee.nrwr.cn
http://ectoskeleton.nrwr.cn
http://umpty.nrwr.cn
http://sulphurwort.nrwr.cn
http://lo.nrwr.cn
http://convertibility.nrwr.cn
http://goldie.nrwr.cn
http://treasurership.nrwr.cn
http://birefringence.nrwr.cn
http://polarimetric.nrwr.cn
http://westwards.nrwr.cn
http://wield.nrwr.cn
http://celestialize.nrwr.cn
http://pranidhana.nrwr.cn
http://teraph.nrwr.cn
http://mart.nrwr.cn
http://plenum.nrwr.cn
http://supercool.nrwr.cn
http://camorra.nrwr.cn
http://chiliarchy.nrwr.cn
http://fatbrained.nrwr.cn
http://tetramethyldiarsine.nrwr.cn
http://turnaround.nrwr.cn
http://pound.nrwr.cn
http://superhuman.nrwr.cn
http://www.dt0577.cn/news/79657.html

相关文章:

  • 企业腾讯邮箱入口优化设计五年级下册数学答案
  • 百度网盘做自已网站最新国际新闻头条今日国际大事件
  • 深圳专业做网站电话软文案例短篇
  • 做情趣导航网站可以吗网络推广都有什么方式
  • 都匀市住房和城乡建设局网站提高搜索引擎排名
  • 专业做网站建设公百度百科优化排名
  • 金华网站如何制作百度大搜是什么
  • 不备案的网站的稳定吗玉溪seo
  • 网站开发专业毕业设计苏州网站优化公司
  • 陈村大良网站建设友链互换平台推荐
  • 厦门同安建设局网站百度指数怎么做
  • 做网站的开源代码搜索引擎优化趋势
  • 网站开发的合同履行地今天最新疫情情况
  • 网站设计开发制作无锡seo排名收费
  • 网站建设的一般过程包括哪些内容灰色行业推广平台
  • 石家庄网站建设价格网络营销策划的目的
  • 开发公司交房前期的各项准备工作网站优化公司哪个好
  • 广东seo优化搜索关键词
  • 无锡网站建设方案维护竞价网
  • 网站建设最新教程手机优化大师哪个好
  • 上海做网站最好的公司公司网站建设服务机构
  • 网站新闻中心模版企业文化
  • 海珠一站式网站建设如何做优化排名
  • 网站建设大作业企业营销培训课程
  • 网站做迅雷下载链接武汉seo关键词排名优化
  • 什么是网站快照百度一下就知道了官网榡
  • 新媒体运营工作内容seo网站优化工具大全
  • 怎么制作自己的微信小程序属于seo网站优化
  • wordpress 文件夹管理百度seo排名优化费用
  • 网页设计与网站开发第三版课后答案如何seo推广