简体   繁体   中英

How I can export to excel a tree hierarchy using Apache POI?

I have a class:

class Node{
private Node parent;
private List<Node> children;
...
}

How I can export a tree of it's items to excel using Apache POI for getting document like this (I need to shift only first column in table):

A
 B
  C
 D
 E
  F
   G

A simple solution would be to create a NodeWriter class that essentially writes the Node onto an Excel Spreadsheet:

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NodeWriter {
    public void write(Node tree, String filePathName) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Tree");
        writeHelp(0, 1, tree, sheet);
        try (FileOutputStream outputStream = new FileOutputStream(filePathName)) {
            workbook.write(outputStream);
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeHelp(int indent, int rowNum, Node tree, XSSFSheet sheet) {
        if (sheet.getRow(rowNum) != null) {
            writeHelp(indent, rowNum+1, tree, sheet);
        } else {
            Row row = sheet.createRow(rowNum);
            Cell cell = row.createCell(indent);
            cell.setCellValue(tree.getNodeName());
            for (Node child : tree.getChildren()) {
                writeHelp(indent + 1, rowNum + 1, child, sheet);
            }
        }
    }
}

I've made some assumptions about your Node class. This solution ensures that you create a new Row and don't overwrite existing rows (as you would if that if loop wasn't there in writeHelp ).

My solution - merging. Have a nice day. Thanks.

Looks like this: https://docs.oracle.com/cd/E36352_01/epm.1112/disclosure_mgmt_admin/new_files/image002.jpg

My solution with merging:

private int createHierarchy(Sheet sheet, Node node, int currentRowIdx, int nodeLevel) {
    if(node.getParent() == null){
        sheet.setColumnWidth(8, 1000);
        Row row = sheet.createRow(currentRowIdx);
        row.createCell(nodeLevel).setCellValue(node.getName());
        row.createCell(9).setCellValue(node.getValue());
        sheet.addMergedRegion(new CellRangeAddress(currentRowIdx, currentRowIdx, nodeLevel, 8));
        nodeLevel++;
    }

    for (Node node : node.getChildren()) {
        Row row = sheet.createRow(++currentRowIdx);
        row.createCell(nodeLevel).setCellValue(node.getName());
        row.createCell(9).setCellValue(node.getValue());
        sheet.addMergedRegion(new CellRangeAddress(currentRowIdx, currentRowIdx, nodeLevel, 8));
        currentRowIdx = createHierarchy(sheet, node, currentRowIdx, nodeLevel+1);
    }

    return currentRowIdx;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM