当前位置:网站首页>POI导出excel,按照父子节点进行分级显示
POI导出excel,按照父子节点进行分级显示
2022-07-01 02:54:00 【白衣席城】
POI导出excel,按照父子节点进行分级显示
这个功能有两部分:
- 第一部分:生成excel并导出
- 第二部分:给生成的excel按照父子节点分级显示。
第一部分很简单,直接略过。
参考文献:
https://www.cnblogs.com/mingyue1818/p/4828895.html
第二部分需要按照某一列(单位级数)进行分级,并折叠起来
思路:观察数据可以发现已经排好序并且有7级。
1、遍历找到最大级数。maxLevel = 7
2、先处理最大一级,也就是第7级.。
先折叠叶子节点,折叠后将dataset中的值替换为null
3、去掉当前的叶子节点,则上级成为了叶子节点,折叠上级;以此类推全部折叠。
如上图,先折叠第7级
在折叠第6级

代码如下
public class ExcelExportUtil {
public static void exportManySheetExcel(String file, List<ExcelExp> mysheets) {
HSSFWorkbook wb = new HSSFWorkbook();// 创建工作薄
List<ExcelExp> sheets = mysheets;
// 表头样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 字体样式
HSSFFont fontStyle = wb.createFont();
fontStyle.setFontName("微软雅黑");
fontStyle.setFontHeightInPoints((short) 12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(fontStyle);
for (ExcelExp excel : sheets) {
// 新建一个sheet
HSSFSheet sheet = wb.createSheet(excel.getFileName());// 获取该sheet名称
String[] handers = excel.getHanders();// 获取sheet的标题名
HSSFRow rowFirst = sheet.createRow(0);// 第一个sheet的第一行为标题
// 写标题
for (short i = 0; i < handers.length; i++) {
// 获取第一行的每个单元格
HSSFCell cell = rowFirst.createCell(i);
// 往单元格里写数据
cell.setCellValue(handers[i]);
cell.setCellStyle(style); // 加样式
sheet.setColumnWidth(i, (short) 6000); // 设置每列的列宽
}
// 写数据集
List<String[]> dataset = excel.getDataset();
maxJs = 0;
for (int i = 0; i < dataset.size(); i++) {
String[] data = dataset.get(i);// 获取该对象
// 创建数据行
HSSFRow row = sheet.createRow(i + 1);
for (short j = 0; j < data.length; j++) {
// 设置对应单元格的值
row.createCell(j).setCellValue(data[j]);
}
if (StringUtils.isNotEmpty(data[2])) {
getMaxJs(Integer.valueOf(data[2]));
}
}
if (maxJs > 0) {
groupNonDetailLevel(dataset, sheet);
}
}
// 写文件
try {
FileOutputStream out = new FileOutputStream(new File(file));
out.flush();
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static int maxJs = 0;
/** * * @Description: 获取最大级数 * @author fhy * @date 2022年6月22日 */
public static int getMaxJs(int dwJs) {
if (dwJs > maxJs) {
maxJs = dwJs;
}
return maxJs;
}
/** * * @Description: 按照级数从大到小遍历 * @author fhy * @date 2022年6月22日 */
public static void groupNonDetailLevel(List<String[]> dataset,HSSFSheet sheet) {
for (int j = maxJs; j > 1; j--) {
groupDetailLevel(dataset,sheet,j);
}
}
/** * * @Description: 明细级分级展示。先折叠叶子节点,折叠后将dataset中的值替换为null * (去掉当前的叶子节点,则上级成为了叶子节点,折叠上级;以此类推全部折叠) * @author fhy * @date 2022年6月22日 */
public static void groupDetailLevel(List<String[]> dataset, HSSFSheet sheet,int level) {
String dwJs = "";
int startRow, endRow;
List<Integer> fromRowToRow = new ArrayList<Integer>();
for (int i = 0; i < dataset.size(); i++) {
String[] data = dataset.get(i);// 获取该对象
if (null == data) {
continue;
}
dwJs = data[2];
if (level == Integer.valueOf(dwJs)) {
//获取到叶子节点所在下标,放入list
fromRowToRow.add(i);
//置空
dataset.set(i, null);
} else {
if (CollectionUtils.isNotEmpty(fromRowToRow)) {
//从list中取出第一个和最后一个叶子节点的下标作为开始分组和结束分组的下标
startRow = fromRowToRow.get(0);
endRow = fromRowToRow.get(fromRowToRow.size() - 1);
sheet.groupRow(startRow + 1, endRow + 1);//分组
sheet.setRowGroupCollapsed(startRow + 1, true);//折叠
fromRowToRow.clear();
}
}
}
if (CollectionUtils.isNotEmpty(fromRowToRow)) {
startRow = fromRowToRow.get(0);
endRow = fromRowToRow.get(fromRowToRow.size() - 1);
sheet.groupRow(startRow + 1, endRow + 1);
sheet.setRowGroupCollapsed(startRow + 1, true);
fromRowToRow.clear();
}
}
}
边栏推荐
- PTA 1016
- 鼠标悬停效果四
- kubernetes资源对象介绍及常用命令(二)
- Completely solve the lost connection to MySQL server at 'reading initial communication packet
- Huawei operator level router configuration example | BGP VPLS configuration example
- Image preloading in JS
- 鼠标悬停效果八
- 园区运营效率提升,小程序容器技术加速应用平台化管理
- Share Creators萌芽人才培养计划来了!
- Pulsar geo replication/ disaster recovery / regional replication
猜你喜欢

产业互联网中,「小」程序有「大」作为

【小程序项目开发-- 京东商城】uni-app之首页商品楼层

xxl-job使用指南

Restcloud ETL data realizes incremental data synchronization through timestamp

Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
![Od modify DLL and exe pop-up contents [OllyDbg]](/img/ff/7249e6e6644376ae048b23bf63b046.jpg)
Od modify DLL and exe pop-up contents [OllyDbg]

Completely solve the lost connection to MySQL server at 'reading initial communication packet

The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)

咱就是说 随便整几千个表情包为我所用一下
随机推荐
Catch 222222
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
Here comes the share creators budding talent training program!
基于Pytorch完整的训练一个神经网络并进行验证
Résumé des styles de développement d'applets Wechat
Big orange crazy blog move notice
Mnasnet learning notes
Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
【微信小程序开发】样式汇总
Mouse over effect I
Gartner研究:在中国,混合云的采用已成为主流趋势
单片机 MCU 固件打包脚本软件
MCU firmware packaging Script Software
Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
ipmitool下载地址和编译安装时可能出现的问题
Pychart software deployment gray unable to point
Restcloud ETL WebService data synchronization to local
Viewing JVM parameters