当前位置:网站首页>【poi导出excel之XSSFWorkbook】
【poi导出excel之XSSFWorkbook】
2022-08-08 16:24:00 【一个搬砖的农民工】
POI提供了HSSF、XSSF以及SXSSF三种方式操作Excel。
- HSSF:Excel97-2003版本,扩展名为.xls。一个sheet最大行数65536,最大列数256。
- XSSF:Excel2007版本开始,扩展名为.xlsx。一个sheet最大行数1048576,最大列数16384。
- SXSSF:是在XSSF基础上,POI3.8版本开始提供的支持低内存占用的操作方式,扩展名为.xlsx。
因为xssf是将数据存在内存,所以为了防止内存溢出,就出现了sxssf,例:
SXSSFWorkbook w3= new SXSSFWorkbook(1000);
SXSSFWorkbook 设置内存中最多只有1000行数据,当超过这个数据时,就将内存之前的数据删除,并且会在硬盘中生成临时文件。从而保证了低内存消耗。
注:针对 SXSSF Beta 3.8下,会有临时文件产生
1、XSSFWorkbook 生成excel
// 1、创建工作表
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
XSSFSheet xssfSheet = xssfWorkbook.createSheet("sheet名称");
XSSFRow xssfRow; // 行
XSSFCell xssfCell; // 列
// 2、在sheet中创建行,注意判断 第一行是否已经创建,否则会覆盖之前的数据
xssfRow = xssfSheet.getRow(1);
if (xssfRow == null) {
xssfRow = xssfSheet.createRow(1);
}
// 3、创建单元格
xssfCell = xssfRow.createCell(1);
// 4、设置单元格内容
xssfCell.setCellValue("测试");
// 5、导出excel
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("表名.xlsx", "UTF-8"));
ServletOutputStream out = response.getOutputStream();
xssfWorkbook.write(out);
out.close();
xssfWorkbook.close();
2、合并单元格
把上面第2步到第4步替换成如下
(注:创建的单元格和值要取合并单元格左上角第一个)
// 创建合并单元格(int firstRow, int lastRow, int firstCol, int lastCol)
CellRangeAddress range = new CellRangeAddress(1, 1, 2, 5);
// 创建行
xssfRow = xssfSheet.getRow(range.getFirstRow());
if (xssfRow == null) {
xssfRow = xssfSheet.createRow(range.getFirstRow());
}
// 创建列
xssfCell = xssfRow.createCell(range.getFirstColumn());
// 设置单元格内容
xssfCell.setCellValue("测试");
// 添加合并单元格到sheet
xssfSheet.addMergedRegion(range);
// 设置单元格样式:
CellStyle style = xssfWorkbook.createCellStyle();
// 1、水平\垂直居中
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 2、设置背景色
style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 全部填充
// 3、字段加粗
XSSFFont font = xssfWorkbook.createFont();
font.setBold(true);
style.setFont(font);
// 4、设置单元格样式为黑色实线(不是合并单元格,如果是合并单元格用下面第5个)
style.setBorderBottom(BorderStyle.THICK); //下边框
style.setBorderLeft(BorderStyle.THICK); //左边框
style.setBorderTop(BorderStyle.THICK); //上边框
style.setBorderRight(BorderStyle.THICK); //右边框
xssfCell.setCellStyle(style);
// 5、设置合并边框样式为黑色实线(放最后以免样式被覆盖)
RegionUtil.setBorderBottom(BorderStyle.THICK, range , xssfSheet);
RegionUtil.setBorderRight(BorderStyle.THICK, range , xssfSheet);
RegionUtil.setBorderTop(BorderStyle.THICK, range , xssfSheet);
RegionUtil.setBorderLeft(BorderStyle.THICK, range , xssfSheet);
单元格对应颜色:POI 设置Excel单元格背景色
3、单元格设置下拉列表
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("导入模板");
// 1、设置表头
。。。。。
// 2、为第3列设置下拉选项
String[] arr = {
"选项1","选项2"};
sheet.addValidationData(addValidationData(sheet, arr, 3));
/** * 设置下拉列表 * * @param sheet * @param datas * @param index * @return */
private XSSFDataValidation addValidationData(XSSFSheet sheet, String[] datas, Integer index) {
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(datas);
CellRangeAddressList addressList = null;
XSSFDataValidation validation = null;
addressList = new CellRangeAddressList(1, 100000, index, index);// 开始结束行,index表示开始和结束列
validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
// 这两行设置单元格只能是列表中的内容,否则报错
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
return validation;
}
边栏推荐
- leetcode 155. Min Stack最小栈(中等)
- Is the current safe and reliable domestic futures account opening process?
- 使用pymongo,将MongoDB生成的ObjectId类型数据与字符串之间的相互转化
- bzoj1251 序列终结者
- Guanghong Technology: The company provides manufacturing services for Xiaomi, Samsung, OPPO, Nokia and other products in India
- 使用FastApi服务解决程序反复调试导致速度过慢的问题(以tsfresh为例)
- Node简介
- Dry goods: design high concurrency architecture from scratch
- 目前安全靠谱的国内期货开户流程?
- All volunteers V853 chip Tina RTSP environment set up
猜你喜欢
api的封装
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
急了,Mysql索引中最不容易记的三个知识点通透了
基于ECS实现一分钟自动化部署【华为云至简致远】
它们不一样!透析【观察者模式】和【发布订阅模式】
耐心排序——专门快速解决最长递增子数组
腾讯云产品可观测最佳实践 (Function)
[Online interviewer] How to achieve deduplication and idempotency
C#/VB.NET convert PDF to PDF/X-1a:2001
Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
随机推荐
淘宝API常用接口列表与申请方式
找工作的我看了国聘app
ggplot2可视化水平箱图并使用fct_reorder排序数据、使用na.rm处理缺失值(reorder boxplot with fct_reorder)、按照箱图的中位数从大到小排序水平箱图
使用FastApi服务解决程序反复调试导致速度过慢的问题(以tsfresh为例)
分享这些2022设计师们决不能错过的Blender新插件
NSSCTF部分复现
MySQL数据库的简介及select语句的执行流程
是时候展现真正实力了!揭秘2022华为开发者大赛背后的技术能力
hdu2475 Box
耐心排序——专门快速解决最长递增子数组
‘xxxx‘ is declared but its value is never read.Vetur(6133)
Using PyGame's Bubble Sort Visualizer
The realization of the salary slip issuing function of WeChat public account + web background
最稳定的淘宝商品详情接口
Lecture 207, Class Schedule
它们不一样!透析【观察者模式】和【发布订阅模式】
Thread local storage ThreadLocal
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
用于视觉语言导航的自监督三维语义表示学习