EasyExcel导出自动适应列宽 Excel样式
EasyExcel自动适应列宽 导出直接套用此方法即可使用 在导出时引用即可 导出引用示例在下方
package com.gaiaworks.cn.opm.emp.biz.util.excel;import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** excel自适应列宽 拉过去就能直接套用自动适应列宽*/
public class CustomCellWriteUtil extends AbstractColumnWidthStyleStrategy {private static final int MAX_COLUMN_WIDTH = 255;private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);public CustomCellWriteUtil() {}protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);if (needSetWidth) {Map<Integer, Integer> maxColumnWidthMap = (Map) CACHE.get(writeSheetHolder.getSheetNo());if (maxColumnWidthMap == null) {maxColumnWidthMap = new HashMap(16);CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);}Integer columnWidth = this.dataLength(cellDataList, cell, isHead);if (columnWidth >= 0) {if (columnWidth > 255) {columnWidth = 255;}Integer maxColumnWidth = (Integer) ((Map) maxColumnWidthMap).get(cell.getColumnIndex());if (maxColumnWidth == null || columnWidth > maxColumnWidth) {((Map) maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 7250);}}}}private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {if (isHead) {return cell.getStringCellValue().getBytes().length;} else {CellData cellData = (CellData) cellDataList.get(0);CellDataTypeEnum type = cellData.getType();if (type == null) {return -1;} else {switch (type) {case STRING:return cellData.getStringValue().getBytes().length;case BOOLEAN:return cellData.getBooleanValue().toString().getBytes().length;case NUMBER:return cellData.getNumberValue().toString().getBytes().length;default:return -1;}}}}
}
设置EasyExcel样式的方法
package com.gaiaworks.cn.opm.emp.biz.util.excel;import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.gaiaworks.cn.opm.emp.component.constants.CommonConstant;
import com.gaiaworks.cn.opm.emp.component.constants.Symbol;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;import java.lang.reflect.Field;
import java.util.List;public class ExcelUtil {/*** 设置excel样式* 返回样式 Style 具体细节可以在网上查找样式元素 自行替换 现在这个也是可以直接套用的* @return*/public static HorizontalCellStyleStrategy getStyleStrategy() {// 头的策略 样式调整WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 头背景 浅绿headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());WriteFont headWriteFont = new WriteFont();// 头字号headWriteFont.setFontHeightInPoints((short) 14);// 字体样式headWriteFont.setFontName("宋体");headWriteCellStyle.setWriteFont(headWriteFont);// 自动换行headWriteCellStyle.setWrapped(false);// 设置细边框headWriteCellStyle.setBorderBottom(BorderStyle.THIN);headWriteCellStyle.setBorderLeft(BorderStyle.THIN);headWriteCellStyle.setBorderRight(BorderStyle.THIN);headWriteCellStyle.setBorderTop(BorderStyle.THIN);// 设置边框颜色 25灰度headWriteCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());headWriteCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());headWriteCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());headWriteCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());// 水平对齐方式headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 垂直对齐方式headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 内容的策略 宋体WriteCellStyle contentStyle = new WriteCellStyle();// 设置垂直居中contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 设置 水平居中
// contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);WriteFont contentWriteFont = new WriteFont();// 内容字号contentWriteFont.setFontHeightInPoints((short) 12);// 字体样式contentWriteFont.setFontName("宋体");contentStyle.setWriteFont(contentWriteFont);// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现return new HorizontalCellStyleStrategy(headWriteCellStyle, contentStyle);}// 下面这两个方法没用到过 之前也是粘贴过来的现在不知道 又什么用 (>@_@<)!!public static Boolean isEmpty(String className, Object excel){try {Class clazz = Class.forName(className);Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {field.setAccessible(true); // 允许通过反射访问该字段}for (Field field : fields) {if (ObjectUtils.isNotEmpty(field.get(excel))) {field.setAccessible(false);return Boolean.FALSE;}field.setAccessible(false);}} catch (ClassNotFoundException | IllegalAccessException e) {e.printStackTrace();}return Boolean.TRUE;}public static Boolean checkExcel(String fileName){String substringFileName = fileName.substring(fileName.lastIndexOf(Symbol.POINT) + 1);List<String> names = CommonConstant.EXCELTYE;for (String name : names) {if (name.equals(substringFileName)) {return true;}}return false;}}
引用的方式 怎么在导出Excel的时候使用他们
WriteSheet sheet01 = EasyExcel.writerSheet().sheetName("导出Sheet名").head(导出的模板类.class).registerWriteHandler(new CustomCellWriteUtil())//自适应列宽引用方式.registerWriteHandler(ExcelUtil.getStyleStrategy())// Excel样式引用方式.build();
EasyExcel导出自动适应列宽 Excel样式相关推荐
- 教你用EasyExcel导出包含图片列的excel
教你用EasyExcel导出包含图片列的excel 前情概要 众所周知,导入及导出功能在后台服务中很常见,博主目前参与的这个项目就有多Excel的导入和导出,但是在我昨天完成需求的时候,突然发现项目里 ...
- R先生一步步教你用EasyExcel导出包含多图片的Excel
R先生一步步教你用EasyExcel导出包含多图片的Excel 光与光之间的黑暗,路人永不得知 书接上文说到,R先生之前有写过一篇用EasyExel导出包含图片的Excel文件,由于在官方文档并没有找 ...
- EasyExcel 的行高列宽的单位,直接设置行高列宽的大小
前言 有没有小伙伴用EasyExcel设置表的行高列宽时,会有点疑惑,这个 Integer 或者 Short 对应的是什么单位,我们应该设置多大的值,才能满足表的样式需要? 我刚开始使用EasyExc ...
- DataGridView自动设定列宽和行高
这篇文章介绍了DataGridView自动设定列宽和行高的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 设定行高和列宽自动调整 设定包括Header和所有 ...
- EasyExcel导出、按列设置样式、根据表头字数设置列宽、Converter转换
文章目录 一.Excel导出实体类 二.自定义样式handler 三.servicesImple类中实现方法 四.表头设置列宽handler 五.Converter转换 一.Excel导出实体类 im ...
- 【Easyexcel】使用easyexcel导出带有固定表头的excel代码,并且有合并单元格操作
以下是一个使用EasyExcel导出带有固定表头并且带有合并单元格的Excel代码示例: //创建excel对象 ExcelWriter writer = EasyExcel.write(" ...
- easyExcel导出多个sheet的excel
easyExcel导出 导出背景 依赖 单个sheet的excel导出 定义导出模型 controller层代码 多个sheet的excel导出 所用的导出模型 controller层代码 所用到的工 ...
- com.alibaba.easyexcel导出指定的列_使用Python导入导出Excel表格
这篇文章的目的是讲解使用python导入导出Excel表格,目前还不涉及数据处理,主要实现为数据的展示. 第一步,准备材料 python3.8的安装包 一个Excel表格 第二步,安装软件 pytho ...
- easyexcel 导出设置标题_EasyPoi设置Excel导出样式(边框,背景色,字体)
**导出样式如下图** **使用方式** 创建一个样式工具类.工具类如下: import cn.afterturn.easypoi.excel.entity.params.ExcelExportEnt ...
最新文章
- 机房批量改计算机名计算机组,批量设置IP地址和计算机名
- 多重继承与虚继承编程实验
- 线性链条件随机场与HMM在viterbi算法中的图解对比
- html5 摄像头拍摄视频教程,html5实现调用摄像头并拍照功能
- python中test_在python中生成py.test测试
- lock和synchronized的同步区别与选择
- Django表中的字段
- 第二课 了解编程环境
- js混淆还原工具_技术分享:几种常见的JavaScript混淆和反混淆工具分析实战
- 100页ppt讲清楚云原生
- matlab corner 舍弃,成长就是不断地丢弃与拾取 — 读The Glass Castle《玻璃城堡》有感...
- gromacs 安装_GROMACS简介与安装
- win10未能解析服务器名,win10系统提示“无法解析服务器的dns地址”的修复方法...
- 中国半导体工业测试设备市场深度研究分析报告
- 笔记本键盘两个ctrl键同时失灵怎么解决?
- 如何查看vue打印的console.log日志
- POA委员会选举机制
- 免费下载各种json包的网址
- 全球及中国二手车市场销量渠道规模及发展格局建议报告2021-2027年
- 记一次服务器“挖矿“处理
热门文章
- 数据结构与算法题目及C++解答
- 网络协议分析(仅供参考,后面的wireshark抓包内容最好自己看书研究)
- 小暑将至,一起来看看品牌小暑海报吧
- Android 开发中的SSL pinning
- 有了这些工具,可以助我工作效率起飞,强烈建议收藏!!
- 京东API提取方法-获得JD商品详情API
- 人活着的意义__2014思想篇
- 楼层部分的计算机术语大全,计算机术语(大全)
- 共享存储之SAN,NAS的深入比较
- 问题:The ABAP program lines are wider than the internal table.