poi读取树形excel
时间: 2023-06-21 15:23:27 浏览: 82
poi读取excel
要读取树形结构的Excel,需要使用递归遍历的方式来读取每一个单元格。在读取Excel的过程中,需要判断当前单元格是否为合并单元格,如果是,则需要获取合并单元格的起始行、起始列、结束行和结束列,然后将当前单元格的值设置为合并单元格的起始单元格的值。
以下是一个示例代码,可以读取树形结构的Excel:
```
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File file = new File("tree.xlsx");
FileInputStream fis = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
List<TreeNode> nodeList = new ArrayList<>();
readTree(sheet, nodeList, 0, 0);
for (TreeNode node : nodeList) {
System.out.println(node);
}
}
private static void readTree(Sheet sheet, List<TreeNode> nodeList, int row, int col) {
Row currentRow = sheet.getRow(row);
Cell currentCell = currentRow.getCell(col);
if (currentCell == null || currentCell.getCellType() == CellType.BLANK) {
return;
}
String value = currentCell.getStringCellValue();
int level = getLevel(sheet, row, col);
TreeNode node = new TreeNode(value, level);
nodeList.add(node);
int nextRow = row + 1;
int nextCol = col + 1;
while (nextRow <= sheet.getLastRowNum()) {
Row childRow = sheet.getRow(nextRow);
Cell childCell = childRow.getCell(col);
if (childCell == null || childCell.getCellType() == CellType.BLANK) {
nextRow++;
continue;
}
int childLevel = getLevel(sheet, nextRow, col);
if (childLevel <= level) {
break;
}
String childValue = childCell.getStringCellValue();
TreeNode childNode = new TreeNode(childValue, childLevel);
node.addChild(childNode);
readTree(sheet, nodeList, nextRow, nextCol);
nextRow++;
}
if (currentCell.getCellType() == CellType.MERGE) {
CellRangeAddress range = getMergedRegion(sheet, row, col);
if (range != null) {
int startRow = range.getFirstRow();
int startCol = range.getFirstColumn();
if (startRow != row || startCol != col) {
Cell startCell = sheet.getRow(startRow).getCell(startCol);
String startValue = startCell.getStringCellValue();
node.setValue(startValue);
}
}
}
}
private static int getLevel(Sheet sheet, int row, int col) {
Row currentRow = sheet.getRow(row);
int level = 0;
for (int i = 0; i < col; i++) {
Cell cell = currentRow.getCell(i);
if (cell != null && cell.getCellType() != CellType.BLANK) {
level++;
}
}
return level;
}
private static CellRangeAddress getMergedRegion(Sheet sheet, int row, int col) {
int count = sheet.getNumMergedRegions();
for (int i = 0; i < count; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
if (range.isInRange(row, col)) {
return range;
}
}
return null;
}
}
class TreeNode {
private String value;
private int level;
private List<TreeNode> children;
public TreeNode(String value, int level) {
this.value = value;
this.level = level;
this.children = new ArrayList<>();
}
public void addChild(TreeNode child) {
this.children.add(child);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < level; i++) {
sb.append(" ");
}
sb.append(value);
for (TreeNode child : children) {
sb.append("\n").append(child);
}
return sb.toString();
}
}
```
在上面的代码中,`readTree` 方法使用递归的方式遍历Excel表格,`getLevel` 方法获取单元格所在的层级,`getMergedRegion` 方法获取合并单元格的范围。`TreeNode` 类表示树形结构中的一个节点,包含节点的值、层级和子节点列表。
阅读全文