引入pom文件

 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.0</version></dependency>

工具类,自动换行

@Slf4j
public class ExcelUtil {public static void download(HttpServletResponse response, String fileName, List list,String sheetName){try {response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");HorizontalCellStyleStrategy horizontalCellStyleStrategy =new HorizontalCellStyleStrategy(StyleUtils.getHeadStyle(), StyleUtils.getContentStyle());EasyExcel.write(response.getOutputStream(), SegmentDownloadData.class).sheet(sheetName).registerWriteHandler(new ExcelCellWidthStyleStrategy()).registerWriteHandler(horizontalCellStyleStrategy).doWrite(list);} catch (IOException e) {log.info("下载异常,文件:"+fileName,e);}}}

设置自动宽度

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;import java.util.HashMap;
import java.util.List;
import java.util.Map;public class ExcelCellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {// 可以根据这里的最大宽度,按自己需要进行调整,搭配单元格样式实现类中的,自动换行,效果更好private static final int MAX_COLUMN_WIDTH = 50;private  Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);@Overrideprotected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> 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 > MAX_COLUMN_WIDTH) {columnWidth = MAX_COLUMN_WIDTH;}Integer maxColumnWidth = (Integer)((Map)maxColumnWidthMap).get(cell.getColumnIndex());if (maxColumnWidth == null || columnWidth > maxColumnWidth) {((Map)maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);}}}}private Integer dataLength(List<WriteCellData<?>> 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;}}}}}

设置格式

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;public class StyleUtils {private static final String[] _formats = new String[]{"General","0","0.00","#,##0","#,##0.00","\"$\"#,##0_);(\"$\"#,##0)","\"$\"#,##0_);[Red](\"$\"#,##0)","\"$\"#,##0.00_);(\"$\"#,##0.00)","\"$\"#,##0.00_);[Red](\"$\"#,##0.00)","0%", "0.00%", "0.00E+00","# ?/?", "# ??/??","m/d/yy", "d-mmm-yy","d-mmm", "mmm-yy","h:mm AM/PM","h:mm:ss AM/PM","h:mm", "h:mm:ss","m/d/yy h:mm","reserved-0x17","reserved-0x18","reserved-0x19","reserved-0x1A","reserved-0x1B","reserved-0x1C","reserved-0x1D","reserved-0x1E","reserved-0x1F","reserved-0x20","reserved-0x21","reserved-0x22","reserved-0x23","reserved-0x24","#,##0_);(#,##0)","#,##0_);[Red](#,##0)","#,##0.00_);(#,##0.00)","#,##0.00_);[Red](#,##0.00)","_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)","_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)","_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)","_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)","mm:ss","[h]:mm:ss","mm:ss.0","##0.0E+0","@" // 文本格式};/*** 标题样式* @return*/public static WriteCellStyle getHeadStyle(){// 头的策略WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 背景颜色
//        headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
//        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);// 字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontName("宋体");//设置字体名字headWriteFont.setFontHeightInPoints((short)10);//设置字体大小headWriteFont.setBold(true);//字体加粗headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;// 样式headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;headWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;headWriteCellStyle.setWrapped(true);  //设置自动换行;headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适return headWriteCellStyle;}/*** 内容样式* @return*/public static WriteCellStyle getContentStyle(){// 内容的策略WriteCellStyle contentWriteCellStyle = new WriteCellStyle();// 背景绿色// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
//        contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
//        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);// 设置字体WriteFont contentWriteFont = new WriteFont();contentWriteFont.setFontHeightInPoints((short) 9);//设置字体大小contentWriteFont.setFontName("宋体"); //设置字体名字contentWriteCellStyle.setWriteFont(contentWriteFont);//在样式用应用设置的字体;//设置样式;contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;contentWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;contentWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;contentWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色;contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中contentWriteCellStyle.setWrapped(true); //设置自动换行;contentWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适return contentWriteCellStyle;}}

easyExcel工具类相关推荐

  1. easyexcel工具类_阿里巴巴程序员常用的 15 款开发者工具

    从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的技术积淀,通过开源.云上实现或工具等形式对外开放,本文将精选了一些阿里巴巴的开发者工具, ...

  2. easyexcel 工具类_问了个在阿里的同学,他们常用的15款开发者工具!

    来源:jianshu.com/p/58ec32eef2d4 整理自公众号:程序员闪充宝 从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的 ...

  3. easyexcel 工具类_阿里程序员常用的 15 款开发者工具~

    从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的技术积淀,通过开源.云上实现或工具等形式对外开放,本文将精选了一些阿里巴巴的开发者工具, ...

  4. EasyExcel工具类(开箱即用)

    所需依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</art ...

  5. Excel解析easyexcel工具类

    本文使用基于阿里的easyexcel编写的工具类对xls后缀的Excel文件(即03版)读取并写成xlsx后缀的Excel文件(即07版),中间转换过程使用String二维数组和对象列表两种形式. e ...

  6. EasyExcel工具类封装, 做到一个函数完成简单的读取和导出

    目录 工具包目录和依赖 工具类 Service实现 Dto类 Controller实现 工具包目录和依赖 工具包目录 依赖(请根据自己需要自行修改版本) <properties><e ...

  7. EasyExcel使用与详细说明,EasyExcel工具类

    文章目录 1.Apache POI 1.1 学习使用成本较高 1.2 POI的内存消耗较大 1.3 特点 2. 初识EasyExcel 2.1 重写了POI对07版Excel的解析 2.2 特点 3. ...

  8. EasyExcel使用的正确姿势,工具类封装

    项目中很可能用到导出excel文件的需求. easyexcel代码量较小,使用简单,而且性能较佳,是一个非常好的选择. 源码地址:https://github.com/alibaba/easyexce ...

  9. Excel工具类 - POI / Easyexcel

    在项目中经常使用Excel 文件做导入导出功能,下面介绍两种经常使用的工具类 Apache POI ,Ali EasyExcel. Excel 分为03 版 (xls), 07版(xlsx),下面介绍 ...

最新文章

  1. java calendar与date_java---Calendar与Date
  2. RabbitMQ的集群模式
  3. C#“.NET研究”类类型
  4. VTK修炼之道26:图像基本操作_三维图像切片提取
  5. 通俗地解释脏读、不可重复读、幻读
  6. java中的基本数据类型_Java中的基本数据类型和引用数据类型
  7. 卡特兰数的性质及其应用扩展
  8. 本地套接字示例[来源:Advanced Linux Programming]
  9. html 图片使用scale,缩放:scale() - CSS3 | 绿叶学习网
  10. 电脑mod_(电脑游戏) 层层恐惧2、冒险游戏——电脑配置要求单机游戏MOD攻略修改器下载...
  11. so库文件控制导出符号
  12. 雅思阅读真经总纲_你们要的刘洪波《雅思阅读真经总纲》高清PDF扫描版来了!!...
  13. ngx_http_headers_module模块add_header和expires指令
  14. Java知多少(76)语言包(java.lang)简介
  15. 8647服务器装系统,今天重新安装了系统,麻烦请红夜鬼先生进来帮我看一下
  16. 张晨北京科技大学计算机,北京大学2010拟初取推荐免试研究生公示名单.doc
  17. Linux系统压力测试工具stress
  18. 如何使用PS改变只有一种颜色图片的颜色
  19. Java开发16个经典面试问题
  20. 解决“远程主机被迫关闭了一个现有的连接”的问题

热门文章

  1. acme.sh 使用记录
  2. acme.sh免费自动更新https证书
  3. gstreamer-1.0学习笔记
  4. 锂电池和铅酸电池的比较
  5. 《Unity3D平台AR开发快速上手--基于EasyAR4.0》随书资源和相关说明
  6. java tree 类_Java TreeSet类
  7. lego ev3 matlab,科学网—[转载]【源码】乐高MINDSTORMS EV3硬件的MATLAB支持包 - 刘春静的博文...
  8. MySQL评估索引合理性字段---Cardinality
  9. 蓝牙耳机半入耳式推荐,NANK南卡和FIIL斐耳蓝牙耳机哪个更好?
  10. win10卸载photoshop cs6残留ADMUI3.fon字体文件删不掉