当前位置:网站首页>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();
}
}
}
边栏推荐
- mybati sql 语句打印
- Use ipmitool to configure BMC network and user information of X86 server
- 【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
- Lenovo x86 server restart management controller (xclarity controller) or TSM method
- How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
- Ipmitool download address and possible problems during compilation and installation
- 股票开户安全吗?上海股票开户步骤。
- Mysql知识点
- Network address translation (NAT) technology
- Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
猜你喜欢

Sampling Area Lights
![[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)](/img/73/a22ab1dbb46e743ffd5f78b40e66a2.png)
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)

Dell server restart Idrac method

Sampling Area Lights

Densenet network paper learning notes

Const and the secret of pointers

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

STM32——一线协议之DS18B20温度采样

世界上最好的学习法:费曼学习法

Share Creators萌芽人才培養計劃來了!
随机推荐
鼠标悬停效果九
Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
鼠标悬停效果五
[machine learning] vectorized computing -- a must on the way of machine learning
Completely solve the lost connection to MySQL server at 'reading initial communication packet
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
Nacos configuration center tutorial
Optimal transport Series 1
How to determine the progress bar loaded in the loading interface when opening the game
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
Xception learning notes
访问url 404 的错误
单片机 MCU 固件打包脚本软件
Here comes the share creators budding talent training program!
鼠标悬停效果八
Optimal Transport系列1
Const and the secret of pointers
Sampling Area Lights
【微信小程序开发】样式汇总
lavaweb【初识后续问题的解决】