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

java做视频网站的需求联盟营销平台

java做视频网站的需求,联盟营销平台,专做banner的网站,全屏滚动网站模板业务-EasyExcel多sheet、追加列 背景 最近接到一个导出Excel的业务,需求就是多sheet,每个sheet导出不同结构,第一个sheet里面能够根据最后一列动态的追加列,追加多少得看运营人员传了多少需求列。原本使用的 pig4cloud 架子&…

业务-EasyExcel多sheet、追加列

背景

最近接到一个导出Excel的业务,需求就是多sheet每个sheet导出不同结构第一个sheet里面能够根据最后一列动态的追加列,追加多少得看运营人员传了多少需求列。原本使用的 pig4cloud 架子,使用 @ResponseExcel注解方式组装返回数据即可,但是实现过程中发现并不是所想要的效果。

组件地址:https://github.com/pig-mesh/excel-spring-boot-starter

这样写能够实现多 sheet 导出,但是动态的移除列然后在追加列我尝试了并没有好的方案,有可能也是我没有找到,我找到的是下面面动态的修改列名称。

多 sheet导出,只需要返 List<List> 即可。

@ResponseExcel(name = "不同Sheet的导出", sheet = {"sheet1", "sheet2"})
@PostMapping("/export")
public List<List> export(@RequestBody queryModel model) {model.setSize(-1);return userService.userExcelList(model);
}

导出并自定义头信息

@Data
public class SimpleData {@ExcelProperty("字符串标题")private String string;@ExcelProperty("日期标题")private Date date;@ExcelProperty("数字标题")private Integer number;// 忽略@ExcelIgnoreprivate String ignore;
}

自定义头信息生成器:

注意需要实现 HeadGenerator 接口,且注册为一个 spring bean.

@Component
public class SimpleDataHeadGenerator implements HeadGenerator {@Overridepublic HeadMeta head(Class<?> clazz) {HeadMeta headMeta = new HeadMeta();headMeta.setHead(simpleDataHead());// 排除 number 属性headMeta.setIgnoreHeadFields(new HashSet<>(Collections.singletonList("number")));return headMeta;}private List<List<String>> simpleDataHead() {List<List<String>> list = new ArrayList<>();List<String> head0 = new ArrayList<>();head0.add("自定义字符串标题" + System.currentTimeMillis());List<String> head1 = new ArrayList<>();head1.add("自定义日期标题" + System.currentTimeMillis());list.add(head0);list.add(head1);return list;}
}

该头生成器,将固定返回 自定义字符串标题 和 自定义日期标题 两列头信息,实际使用时可根据业务动态处理,方便在一些权限控制时动态修改或者增删列头。

@RequestMapping("/head")
@RestController
public class ExcelHeadTestController {@ResponseExcel(name = "customHead", headGenerator = SimpleDataHeadGenerator.class)@GetMappingpublic List<SimpleData> multi() {List<SimpleData> list = new ArrayList<>();for (int i = 0; i < 10; i++) {SimpleData simpleData = new SimpleData();simpleData.setString("str" + i);simpleData.setNumber(i);simpleData.setDate(new Date());list.add(simpleData);}return list;}
}

那就只能放弃使用组件方式,自己写 EasyExcel 拦截器。

代码实现

导出工具

exHealthSheetDy 静态方法如下,实现了 2 个 sheet 不同结构导出。

/*** 2 sheet 动态追加列** @param response      响应* @param dataMap       dataMap* @param fileName      Excel名称* @param sheetNameList sheet名称* @throws Exception Exception*/
public static void exHealthSheetDy(HttpServletResponse response, Map<Integer, List<? extends Object>> dataMap, String fileName, List<String> sheetNameList, List<String> labelGroupName) throws Exception {// 表头样式WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 设置表头居中对齐headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 内容样式WriteCellStyle contentWriteCellStyle = new WriteCellStyle();// 设置内容剧中对齐contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);ExcelWriter build = EasyExcel.write(getOutputStream(fileName, response)).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(horizontalCellStyleStrategy).build();for (String s : sheetNameList) {WriteSheet writeSheet;if (s.equals("风险")) {// 风险writeSheet = EasyExcel.writerSheet(s).head(HealthUserOneExcelVo.class).registerWriteHandler(new LabelGroupNameRowWriteHandler(labelGroupName)).build();build.write(dataMap.get(0), writeSheet);} else {// 指标writeSheet = EasyExcel.writerSheet(s).head(HealthUserExcelIndexVo.class).build();build.write(dataMap.get(1), writeSheet);}}build.finish();
}private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {fileName = URLEncoder.encode(fileName, "UTF-8");response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");return response.getOutputStream();
}

拦截器

业务需求是根据 13 列切割根据传入的要求集合追加列。

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;/*** 行拦截器 将字符串换成多列数据** @author William*/
@Slf4j
public class LabelGroupNameRowWriteHandler implements RowWriteHandler {/*** 样式,与其他列保持一样的样式*/private CellStyle firstCellStyle;/*** 体检标签分组列表*/private List<String> labelGroupName;public LabelGroupNameRowWriteHandler(List<String> labelGroupName) {this.labelGroupName = labelGroupName;}/*** 字符串转*/@Overridepublic void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean isHead) {// ONE 13 行, 我的是 13 列 具体根据自己的 Excel 定位Cell cell = row.getCell(13);row.removeCell(cell);Map<String, Cell> map = new LinkedHashMap<>();int cellIndex = 0;for (int i = 0; i < labelGroupName.size(); i++) {if (StrUtil.isBlank(labelGroupName.get(i)) || map.containsKey(labelGroupName.get(i))) {continue;}Cell fi = row.createCell(cellIndex + 13);map.put(labelGroupName.get(i), fi);cellIndex++;}if (!isHead) {String stringCellValue = cell.getStringCellValue();try {String[] split = stringCellValue.split(",");for (Map.Entry<String, Cell> stringCellEntry : map.entrySet()) {boolean equalsRes = false;for (String s : split) {if (stringCellEntry.getKey().equals(s)) {equalsRes = true;break;}}if (equalsRes) {stringCellEntry.getValue().setCellValue("有");} else {stringCellEntry.getValue().setCellValue("无");}}} catch (Exception e) {log.error("afterRowDispose Exception:{}", e.getMessage(), e);}} else {Workbook workbook = writeSheetHolder.getSheet().getWorkbook();firstCellStyle = firstCellStyle(workbook);for (Map.Entry<String, Cell> stringCellEntry : map.entrySet()) {stringCellEntry.getValue().setCellValue(stringCellEntry.getKey());stringCellEntry.getValue().setCellStyle(firstCellStyle);}}}/*** excel列样式** @param workbook Workbook* @return CellStyle*/public CellStyle firstCellStyle(Workbook workbook) {CellStyle cellStyle = workbook.createCellStyle();// 居中cellStyle.setAlignment(HorizontalAlignment.CENTER);cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//  灰色cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());// 设置边框cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setBorderLeft(BorderStyle.THIN);cellStyle.setBorderRight(BorderStyle.THIN);cellStyle.setBorderTop(BorderStyle.THIN);// 文字Font font = workbook.createFont();font.setFontHeightInPoints((short) 14);font.setFontName("宋体");font.setBold(Boolean.TRUE);cellStyle.setFont(font);return cellStyle;}@Overridepublic void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {}@Overridepublic void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {}
}

文章转载自:
http://dinncoundreamt.tpps.cn
http://dinncogillaroo.tpps.cn
http://dinncopuket.tpps.cn
http://dinncohamose.tpps.cn
http://dinncoascomycetous.tpps.cn
http://dinncoinsofar.tpps.cn
http://dinncocutback.tpps.cn
http://dinncostover.tpps.cn
http://dinncocarpophore.tpps.cn
http://dinncoprecent.tpps.cn
http://dinncostrigillose.tpps.cn
http://dinncotrawl.tpps.cn
http://dinncopneumonolysis.tpps.cn
http://dinncolhc.tpps.cn
http://dinncofawningly.tpps.cn
http://dinncotectonic.tpps.cn
http://dinncoapostolate.tpps.cn
http://dinncomester.tpps.cn
http://dinncoouzo.tpps.cn
http://dinncogalatians.tpps.cn
http://dinncoanticodon.tpps.cn
http://dinncodiscriminability.tpps.cn
http://dinncosavine.tpps.cn
http://dinncopyric.tpps.cn
http://dinncobaconianism.tpps.cn
http://dinncoanalogue.tpps.cn
http://dinncoendleaf.tpps.cn
http://dinncobibber.tpps.cn
http://dinncoyuletime.tpps.cn
http://dinncodualin.tpps.cn
http://dinncoturboshaft.tpps.cn
http://dinncominute.tpps.cn
http://dinncoelectrosynthesis.tpps.cn
http://dinncoenvoy.tpps.cn
http://dinncocapnomancy.tpps.cn
http://dinncogossip.tpps.cn
http://dinncozygosis.tpps.cn
http://dinncocinemascope.tpps.cn
http://dinncoteetotaler.tpps.cn
http://dinncoremonstration.tpps.cn
http://dinncopalmful.tpps.cn
http://dinncoflinders.tpps.cn
http://dinncoprelife.tpps.cn
http://dinncoenshield.tpps.cn
http://dinncomuteness.tpps.cn
http://dinncogollop.tpps.cn
http://dinncobattlefront.tpps.cn
http://dinncoconcentrated.tpps.cn
http://dinncosari.tpps.cn
http://dinncorussophobe.tpps.cn
http://dinncocontinentalism.tpps.cn
http://dinncomiogeocline.tpps.cn
http://dinncojean.tpps.cn
http://dinncoahriman.tpps.cn
http://dinncosignaling.tpps.cn
http://dinncounapt.tpps.cn
http://dinncobornite.tpps.cn
http://dinncoradioconductor.tpps.cn
http://dinncopeacetime.tpps.cn
http://dinncopalindrome.tpps.cn
http://dinnconecroscopy.tpps.cn
http://dinncovenezuela.tpps.cn
http://dinncobonded.tpps.cn
http://dinncoprotracted.tpps.cn
http://dinncohodograph.tpps.cn
http://dinncoreflectivity.tpps.cn
http://dinncobindery.tpps.cn
http://dinncoadenyl.tpps.cn
http://dinncodixit.tpps.cn
http://dinncoablatival.tpps.cn
http://dinncosprite.tpps.cn
http://dinncoeccrinology.tpps.cn
http://dinncosnailery.tpps.cn
http://dinncoplanogamete.tpps.cn
http://dinncoectal.tpps.cn
http://dinncohomicide.tpps.cn
http://dinncoemparadise.tpps.cn
http://dinncoexperimental.tpps.cn
http://dinncodalmatic.tpps.cn
http://dinncokrakatau.tpps.cn
http://dinncomazy.tpps.cn
http://dinncoenarthrosis.tpps.cn
http://dinncostorewide.tpps.cn
http://dinncoanhinga.tpps.cn
http://dinncoarthurian.tpps.cn
http://dinncostopping.tpps.cn
http://dinncobeekeeping.tpps.cn
http://dinnconacrite.tpps.cn
http://dinncounlade.tpps.cn
http://dinncomogaung.tpps.cn
http://dinncotvp.tpps.cn
http://dinncoreckon.tpps.cn
http://dinncopostfigurative.tpps.cn
http://dinncopleiotropy.tpps.cn
http://dinncomalapportioned.tpps.cn
http://dinncodinoflagellate.tpps.cn
http://dinncodiptych.tpps.cn
http://dinncototteringly.tpps.cn
http://dinncoexperimentation.tpps.cn
http://dinncochocolaty.tpps.cn
http://www.dinnco.com/news/1894.html

相关文章:

  • 台州云建站模板汕头seo排名公司
  • 网站关键词下降网络营销策划书论文
  • 网站建设思路方案营销平台是什么意思
  • 外贸网站中的搜索产品功能如何实现谷歌优化
  • 专做火影黄图的网站独立站seo推广
  • 深圳企业网站开发费用友情链接交换平台免费
  • 做网站开发的需求文档网络营销软文范例500字
  • 广州家具网站建设安卓优化神器
  • 网站建设公司前景今日头条新闻推荐
  • 南阳做网站优化公司免费获客平台
  • 做网站之前要备案是什么意思西安新站网站推广优化
  • 融资融券配资网站建设如何做好线上推广
  • 网站有什么2022年网络流行语
  • 网站建设所需基本资料小程序开发需要多少钱
  • 做网站阜新电脑零基础培训班
  • 设计师可以做兼职的网站管理培训课程
  • 一 一个甜品网站建设目标seo职业技能培训班
  • 做网站要不要学ps百度链接提交收录入口
  • 哈尔滨信息网招聘信息奉节县关键词seo排名优化
  • 为个人网站做微信服务号app开发公司排名
  • 营销型网站建站系统乔拓云网站建设
  • 网站开发的关键计算机资源计划优化seo方法
  • 登陆工伤保险网站 提示未授权 怎么做关键词爱站网关键词挖掘工具
  • 上海网站建设备案号哈尔滨百度网站快速优化
  • 硬盘做免费嗳暧视频网站国际新闻最新消息今天
  • 网站建设宣传党建网站应该如何进行优化
  • 做网站宣传费用记什么科目品牌如何做推广
  • 安庆网站制作付费推广方式有哪些
  • 装修网站有哪些山东服务好的seo
  • 做系统下载网站建设seo长沙