当前位置:网站首页>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();
}
}
}
边栏推荐
- [wechat applet development] style summary
- 【EXSI】主机间传输文件
- Const and the secret of pointers
- IEDA 右键源码文件菜单简介
- Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
- [small program project development -- Jingdong Mall] the home page commodity floor of uni app
- LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
- How to open a stock account? Also, is it safe to open an account online?
- JS to find duplicate elements in two arrays
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
猜你喜欢

旷世轻量化网络ShuffulNetV2学习笔记

Xception学习笔记

Huawei operator level router configuration example | BGP VPLS configuration example

PHP batch Excel to word

DenseNet网络论文学习笔记

【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)

Share Creators萌芽人才培养计划来了!

Mnasnet learning notes

基于OPENCV和图像减法的PCB缺陷检测

Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
随机推荐
Thread Detach
鼠标悬停效果七
Big orange crazy blog move notice
RestCloud ETL WebService数据同步到本地
Servlet [first introduction]
Xception learning notes
Is it safe to open a stock account? Shanghai stock account opening procedures.
鼠标悬停效果六
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
Lavaweb [first understanding the solution of subsequent problems]
Borrowing constructor inheritance and composite inheritance
访问url 404 的错误
STM32——一线协议之DS18B20温度采样
MnasNet学习笔记
Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find