文章目录

  • 1.全局设置标题和内容字体格式
  • 2.个性化设置某一列格式
  • 3.无内容时 (预制模板,流形式写会)

1.全局设置标题和内容字体格式

通过WriteCellStyle 的dataFormat属性和BuiltinFormats指定字体格式
这种单元格有内容时字体才会生效,无内容时还是"常规"格式

    private static WriteHandler templateWriteHandler;static {//表头样式WriteCellStyle headWriteCellStyle = new WriteCellStyle();//字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontHeightInPoints((short) 11);headWriteFont.setBold(true);headWriteCellStyle.setWriteFont(headWriteFont);//边框headWriteCellStyle.setBorderBottom(BorderStyle.THIN);headWriteCellStyle.setBorderLeft(BorderStyle.THIN);headWriteCellStyle.setBorderRight(BorderStyle.THIN);//前景色headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());//是否换行headWriteCellStyle.setWrapped(true);headWriteCellStyle.setLocked(true);//表体样式WriteCellStyle bodyWriteCellStyle = new WriteCellStyle();//设置数据格式索引bodyWriteCellStyle.setDataFormat((short)49);templateWriteHandler = new HorizontalCellStyleStrategy(headWriteCellStyle,bodyWriteCellStyle);}//快速根据索引获取字符串、或根据字符串获取索引public static void main(String[] args) {int builtinFormat = BuiltinFormats.getBuiltinFormat("h:mm:ss AM/PM");System.out.println(builtinFormat);String builtinFormat1 = BuiltinFormats.getBuiltinFormat(49);System.out.println(builtinFormat1);}

记得注册

        excelWriterBuilder.registerWriteHandler(templateWriteHandler);

2.个性化设置某一列格式

继承 AbstractVerticalCellStyleStrategy ,实现个性化方法 单独设置某一列

package com.example.easyexceldemo.bo;import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;public class DemoVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {@Overrideprotected WriteCellStyle headCellStyle(Head head) {WriteCellStyle headWriteCellStyle = new WriteCellStyle();//字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontHeightInPoints((short) 11);headWriteFont.setBold(true);headWriteCellStyle.setWriteFont(headWriteFont);//边框headWriteCellStyle.setBorderBottom(BorderStyle.THIN);headWriteCellStyle.setBorderLeft(BorderStyle.THIN);headWriteCellStyle.setBorderRight(BorderStyle.THIN);//前景色headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());//是否换行headWriteCellStyle.setWrapped(true);headWriteCellStyle.setLocked(true);return headWriteCellStyle;}@Overrideprotected WriteCellStyle contentCellStyle(Head head) {Integer columnIndex = head.getColumnIndex();//这里只单独设置第4列,看情况自己修改,DateFormat取值见 demo1 的 BuiltinFormats 类if(3 == columnIndex){WriteCellStyle bodyWriteCellStyle = new WriteCellStyle();//设置数据格式索引bodyWriteCellStyle.setDataFormat((short)49);return bodyWriteCellStyle;}else {return new WriteCellStyle();}}
}

记得注册

        excelWriterBuilder.registerWriteHandler(templateWriteHandler);

3.无内容时 (预制模板,流形式写会)

其实我遇到的场景,就只是简单的空白模板,网上找了好多为无内容excel设置文本格式的资料,都没有解决。后来干脆把模板上传到resource。

    @GetMapping("/invoiceTemplateDownload2")public void templateDownload2(HttpServletResponse response) throws IOException {response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");String fileName = URLEncoder.encode("模板", "UTF-8").replaceAll("\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");byte[] fileToByteArray = FileUtils.readFileToByteArray(new File("src/main/resources/invoiceTemplate.xlsx"));response.getOutputStream().write(fileToByteArray);}

总结

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 前端开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的 点击我的GitHub免费获取。
还有免费的 高级web全套视频教程 前端架构 H5 vue node 小程序 视频+资料+代码+面试题!

全方面的web前端进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。

EasyExcel 设置单元格格式为 文本相关推荐

  1. easyexcel设置单元格格式为文本

    导出时注册registerWriteHandler(new RowWriteHandlerImpl()) /*** @author jamin* @date 2020/7/29 15:18*/ pub ...

  2. easyexcel 设置标题_easyexcel设置单元格格式为文本

    导出时注册registerWriteHandler(new RowWriteHandlerImpl()) /** * @author jamin * @date 2020/7/29 15:18 */ ...

  3. xlwings设置单元格格式为文本-改正身份证显示问题+excel单元格设置为文本为何还要双击

    关键代码 sht.range("a:a").api.NumberFormat = "@" import xlwings wb = xlwings.Book() ...

  4. PhpSpreadsheet数据导出Excel 设置单元格格式为文本

    PhpOffice\PhpSpreadsheet数据导出Excel 的时候,设置单元格格式为文本,用NumberFormat::FORMAT_TEXT这个方法不起作用,长数字还是不显示,于是找了很久的 ...

  5. poi设置单元格格式为文本_身份证号乱码?日期显示不对?都是单元格格式的锅...

    私信回复关键词[福利],获取丰富办公资源,助你高效办公早下班! 大家好,我是秋小 E~这里是秋叶 Excel 的[问答精华]专栏! 问题主要来自秋叶 Excel 学习班的同学,回答由讲师.助教共同完成 ...

  6. phpexcel 数字格式_php excel 设置单元格格式为文本格式

    学习源头:https://www.cnblogs.com/php-linux/p/6179442.html 解决 PHPExcel 长数字串显示为科学计数 在excel中如果在一个默认的格中输入或复制 ...

  7. html输入长文本格式,文本输入方式有哪两种 设置单元格格式为文本格式

    PPT 文本框中输入文本有哪两种方式,分别使用场合? word文本编辑操作中,文本输入的方式有插入.改写...1.可以双击状态栏中的"OVR"(改写)来打开或关闭改写模式. 2.可 ...

  8. EasyExcel设置空单元格格式为文本

    EasyExcel设置空单元格格式为文本 导语 解决方式 实现效果 导语 本文是解决空白表格设置格式为文本并不需要表格中有内容,适合导出编写模版的需求.我在网上找了一圈也没具体的解决方案,有很多只能修 ...

  9. POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写...

    再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数.百分比.货币.日期.科学计数法和中文大写这些将在下面一一写出 以下将要介绍的每一 ...

最新文章

  1. 再回首数据结构—链表
  2. LNMP安装常见问题集锦(一)
  3. ora-12528 : message 12528 not found; product=RDBMS ; facility=ora
  4. “12306” 的架构到底有多牛逼?
  5. Windows 10 搭建Python3 安装使用 protobuf
  6. Liferay Dynamic CSS Filter方法的研究 - 总体过程
  7. window下配置定时任务实现类似linux的cron定时任务
  8. 项目总结【Excel中附件导出和导入】
  9. 深入浅出mysql第二,mysql-深入浅出MySQL(第2版)-ITBook分享(pdf mongodb java javascript node )...
  10. 这应该是把计算机网络五层模型讲的最好是文章了,看不懂你打我
  11. 小米5splus(高配版/全网通)解BL锁教程申请BootLoader解锁教程
  12. VLAN及VLAN间路由
  13. windows2019关闭Windows server的IE浏览器的增强安全配置
  14. 《崩坏3》评测:游戏设计中整体性和利用率分析(中)
  15. PS怎么做出旧电视图像故障视觉特效
  16. python之路金角大王_Python 之路03 - Python基础3
  17. Sublime Plugin - Python PEP8 Autoformat
  18. 实现strncmp函数
  19. 内容为王 一个好的摘客
  20. 对话华纳前CEO西尔弗曼:如何成功打造电影《神奇女侠》

热门文章

  1. 算法leetcode|17. 电话号码的字母组合(rust重拳出击)
  2. 计算机毕业设计(附源码)python新能源汽车销售管理系统
  3. 阿里云服务器连接github
  4. Java画sin图像
  5. 安装harbor私库
  6. arduino+ZE08-CH2O甲醛模块,输出甲醛含量
  7. 一图带你看懂什么是智慧城市
  8. 删除另一个计算机用户权限,如何解除电脑用户权限
  9. 新媒体短视频运营门槛高吗
  10. linux bash 漏洞 利用,Linux Bash发现重大漏洞