简体   繁体   English

如何使用Apache POI导出到excel树状结构?

[英]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): 我如何使用Apache POI将其项目树导出到excel以获取这样的文档(我只需要移动表中的第一列):

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: 一个简单的解决方案是创建一个NodeWriter类,该类实际上将Node写入Excel电子表格中:

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. 我对您的Node类做了一些假设。 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 ). 该解决方案可确保您创建一个新的行和不覆盖现有的行(因为你想,如果那if环是不存在的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 看起来像这样: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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