当前位置:网站首页>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();
}
}
}
边栏推荐
- 产业互联网中,「小」程序有「大」作为
- 联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
- PCB defect detection based on OpenCV and image subtraction
- Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
- 股票开账户如何优惠开户?还有,在线开户安全么?
- JS to find duplicate elements in two arrays
- 鼠标悬停效果八
- [machine learning] vectorized computing -- a must on the way of machine learning
- Classic programming problem: finding the number of daffodils
- 基于OPENCV和图像减法的PCB缺陷检测
猜你喜欢
Servlet [first introduction]
Restcloud ETL practice to realize incremental data synchronization without identification bit
Gartner研究:在中国,混合云的采用已成为主流趋势
Od modify DLL and exe pop-up contents [OllyDbg]
Restcloud ETL data realizes incremental data synchronization through timestamp
PCB defect detection based on OpenCV and image subtraction
Prototype and prototype chain in JS
Sampling Area Lights
记一次服务部署失败问题排查
js中的原型和原型链
随机推荐
Poj-3486-computers[dynamic planning]
Densenet network paper learning notes
ipmitool下载地址和编译安装时可能出现的问题
[applet project development -- JD mall] uni app commodity classification page (first)
MCU firmware packaging Script Software
咱就是说 随便整几千个表情包为我所用一下
PTA 1016
如果我在北京,到哪里开户比较好?另外,手机开户安全么?
Gartner研究:在中国,混合云的采用已成为主流趋势
SAP ALV summary is inconsistent with exported excel summary data
VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Classic programming problem: finding the number of daffodils
import tensorflow. contrib. Slim as slim error
Restcloud ETL实践之数据行列转换
STM32 - DS18B20 temperature sampling of first-line protocol
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
[applet project development -- Jingdong Mall] classified navigation area of uni app
php批量excel转word
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip