当前位置:网站首页>POI exports excel and displays hierarchically according to parent-child nodes
POI exports excel and displays hierarchically according to parent-child nodes
2022-07-01 03:03:00 【White Xi City】
POI export excel, Hierarchical display according to parent-child nodes
This function has two parts :
- The first part : Generate excel And export
- The second part : To produce excel Hierarchical display according to parent-child nodes .
The first part is very simple , Skip directly .
reference :
https://www.cnblogs.com/mingyue1818/p/4828895.html
The second part needs to follow a certain column ( Unit series ) Grading , And fold it up
Ideas : By observing the data, it can be found that the order has been arranged and 7 level .
1、 Traverse to find the maximum series .maxLevel = 7
2、 First deal with the largest level , That is the first. 7 level ..
First collapse leaf nodes , After folding, the dataset Replace the value in with null
3、 Remove the current leaf node , The parent becomes a leaf node , Collapse superior ; And so on .
Pictured above , Fold first 7 level 
On the folding page 6 level

The code is as follows
public class ExcelExportUtil {
public static void exportManySheetExcel(String file, List<ExcelExp> mysheets) {
HSSFWorkbook wb = new HSSFWorkbook();// Create a workbook
List<ExcelExp> sheets = mysheets;
// Header style
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Create a centered format
// Font style
HSSFFont fontStyle = wb.createFont();
fontStyle.setFontName(" Microsoft YaHei ");
fontStyle.setFontHeightInPoints((short) 12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(fontStyle);
for (ExcelExp excel : sheets) {
// Create a new one sheet
HSSFSheet sheet = wb.createSheet(excel.getFileName());// Get the sheet name
String[] handers = excel.getHanders();// obtain sheet Title name of
HSSFRow rowFirst = sheet.createRow(0);// first sheet The first line title
// Write the title
for (short i = 0; i < handers.length; i++) {
// Get each cell in the first row
HSSFCell cell = rowFirst.createCell(i);
// Write data into cells
cell.setCellValue(handers[i]);
cell.setCellStyle(style); // Add style
sheet.setColumnWidth(i, (short) 6000); // Set the column width of each column
}
// Write data sets
List<String[]> dataset = excel.getDataset();
maxJs = 0;
for (int i = 0; i < dataset.size(); i++) {
String[] data = dataset.get(i);// Get the object
// Create data rows
HSSFRow row = sheet.createRow(i + 1);
for (short j = 0; j < data.length; j++) {
// Set the value of the corresponding cell
row.createCell(j).setCellValue(data[j]);
}
if (StringUtils.isNotEmpty(data[2])) {
getMaxJs(Integer.valueOf(data[2]));
}
}
if (maxJs > 0) {
groupNonDetailLevel(dataset, sheet);
}
}
// Writing documents
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: Get the maximum number of stages * @author fhy * @date 2022 year 6 month 22 Japan */
public static int getMaxJs(int dwJs) {
if (dwJs > maxJs) {
maxJs = dwJs;
}
return maxJs;
}
/** * * @Description: Traverse from large to small according to the series * @author fhy * @date 2022 year 6 month 22 Japan */
public static void groupNonDetailLevel(List<String[]> dataset,HSSFSheet sheet) {
for (int j = maxJs; j > 1; j--) {
groupDetailLevel(dataset,sheet,j);
}
}
/** * * @Description: Detail level hierarchical display . First collapse leaf nodes , After folding, the dataset Replace the value in with null * ( Remove the current leaf node , The parent becomes a leaf node , Collapse superior ; And so on ) * @author fhy * @date 2022 year 6 month 22 Japan */
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);// Get the object
if (null == data) {
continue;
}
dwJs = data[2];
if (level == Integer.valueOf(dwJs)) {
// Get the subscript of the leaf node , Put in list
fromRowToRow.add(i);
// empty
dataset.set(i, null);
} else {
if (CollectionUtils.isNotEmpty(fromRowToRow)) {
// from list Take the subscripts of the first and last leaf nodes as the subscripts of the start and end groups
startRow = fromRowToRow.get(0);
endRow = fromRowToRow.get(fromRowToRow.size() - 1);
sheet.groupRow(startRow + 1, endRow + 1);// grouping
sheet.setRowGroupCollapsed(startRow + 1, true);// Fold
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();
}
}
}
边栏推荐
- Densenet network paper learning notes
- A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
- Complete training and verification of a neural network based on pytorch
- MCU firmware packaging Script Software
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- XXL job User Guide
- Mouse over effect I
- Lenovo x86 server restart management controller (xclarity controller) or TSM method
- 【EXSI】主机间传输文件
- 最新接口自动化面试题
猜你喜欢

Communication protocol -- Classification and characteristics Introduction

Restcloud ETL WebService data synchronization to local

Metadata in NFT

第03章_用户与权限管理

Lenovo x86 server restart management controller (xclarity controller) or TSM method

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)

Dell server restart Idrac method

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

Huawei operator level router configuration example | configuration static VPLS example

php批量excel转word
随机推荐
POI导出excel,按照父子节点进行分级显示
JS to find duplicate elements in two arrays
php批量excel转word
PCB defect detection based on OpenCV and image subtraction
Const and the secret of pointers
An article explaining the publisher subscriber model and the observer model
几行事务代码,让我赔了16万
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
IEDA 右键源码文件菜单简介
Metadata in NFT
Prototype and prototype chain in JS
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
PTA 1017
鼠标悬停效果八
第03章_用戶與權限管理
鼠标悬停效果七
基于Pytorch完整的训练一个神经网络并进行验证
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
鼠标悬停效果九
In the industrial Internet, "small" programs have "big" effects