当前位置:网站首页>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();
}
}
}
边栏推荐
- Servlet [first introduction]
- js中的原型和原型链
- 咱就是说 随便整几千个表情包为我所用一下
- STM32 - DS18B20 temperature sampling of first-line protocol
- How to verify whether the contents of two files are the same
- Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
- Const and the secret of pointers
- Here comes the share creators budding talent training program!
- Poj-3486-computers[dynamic planning]
- 世界上最好的学习法:费曼学习法
猜你喜欢
![[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)

【EXSI】主机间传输文件

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

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

POI导出excel,按照父子节点进行分级显示

php批量excel转word

Dell server restart Idrac method
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

限流组件设计实战

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
随机推荐
Borrowing constructor inheritance and composite inheritance
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
Huawei operator level router configuration example | configuration static VPLS example
Summary of problems encountered in debugging positioning and navigation
MySQL index --01--- design principle of index
MCU firmware packaging Script Software
STM32——一线协议之DS18B20温度采样
POI导出excel,按照父子节点进行分级显示
在国内如何买港股的股?用什么平台安全一些?
Sampling Area Lights
Mouse over effect VI
访问url 404 的错误
mybati sql 语句打印
Restcloud ETL practice to realize incremental data synchronization without identification bit
最新接口自动化面试题
Big orange crazy blog move notice
Magnetic manometer and measurement of foreign coins
Ipmitool download address and possible problems during compilation and installation
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
C language a little bit (may increase in the future)