是帮别人做了东西,然后顺便发到这个上面,为以后也提供方便。
现有List<Node>,具体多少级,不详;
最后需求:
其内的数据为每个node的id;
具体实现代码如下:
public class Node {
private String id;
private String pid;
private List<Node> children;
private int level;
........get,set, 构造方法;
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import com.yyzq.entity.Node;
public class PoiTest {
static HashMap<String,Node> nodeMap = new HashMap<String,Node>();
static ArrayList<String> le = new ArrayList<String>();
static Set set = new HashSet();
/**
* 通过递归,遍历出每个Node
* @param nodes
*/
public static void test07241505(List<Node> nodes){
if(nodes.size() > 0){
for(int i=0;i<nodes.size();i++){
if(nodes.get(i).getChildren().size() > 0){
//将每个node的id和他的Object保存到modeMap<HashMap>
nodeMap.put(nodes.get(i).getId(), nodes.get(i));
//保存每个node的id、level、和children到le<ArrayList>
le.add(nodes.get(i).getId()+","+nodes.get(i).getLevel()+","+nodes.get(i).getChildren().size());
//保存所有的level到set<Set>,这里的set利用set集合不重复,为了找到具体的层级。
set.add(nodes.get(i).getLevel());
test07241505(nodes.get(i).getChildren());
}
}
}
}
public static void calculateData(List<Node> nodes) throws IOException{
test07241505(nodes);
HSSFWorkbook work = new HSSFWorkbook();
Sheet sheet = work.createSheet("sheet1");
List<Integer> list = new ArrayList<Integer>();
Iterator its = set.iterator();
while(its.hasNext()){
list.add((Integer) its.next());
}
Collections.sort(list);
int rowCount = list.get(list.size()-1) + 2;//总行数:最后一级因为children的size是0;所以没有加入,再加入标题栏的那行
int cellCount = 0;//总列数
int levelNum = list.get(list.size()-1);//找到倒数第二级的level
for(int i=le.size()-1;i>-1;i--){
String str1 = le.get(i);
String[] nodeArr = str1.split(",");//length = 3,
if(Integer.parseInt(nodeArr[1]) == levelNum){//通过倒数第二级的level找到其children的size,并相加,得到最后一列的列数;
cellCount += Integer.parseInt(nodeArr[2]);
}
}
//创建每行每列;
for(int i=0;i<rowCount;i++){
Row row = sheet.createRow(i);
for(int j=0;j<cellCount;j++){
row.createCell(j);
}
}
sheet.addMergedRegion(new CellRangeAddress(0,0,0,cellCount-1));
Map<String,Integer> map = new HashMap<String,Integer>();
for(int j=list.size()-1;j>-1;j--){//遍历level,以为加入set,是唯一不重复;
int level = list.get(j);
int cellNum = 0;
for(int i=le.size()-1;i>-1;i--){//遍历List --- ID + Level + children.size();--->就是遍历除了最底层的node之外的每个node;
String str1 = le.get(i);
String[] nodeArr = str1.split(",");//length = 3,
if(Integer.parseInt(nodeArr[1]) == level){//
if(j == list.size()-1){
Row row = sheet.getRow(level + 1);
sheet.getRow(level ).getCell(cellNum).setCellValue(nodeArr[0]);
sheet.addMergedRegion(new CellRangeAddress(level,level,cellNum,cellNum+Integer.parseInt(nodeArr[2])-1));
map.put(nodeArr[0], Integer.parseInt(nodeArr[2]));
List<Node> lnode = nodeMap.get(nodeArr[0]).getChildren();
for(int s=0;s<lnode.size();s++){
row.getCell(s+cellNum).setCellValue(lnode.get(s).getId());
}
cellNum += Integer.parseInt(nodeArr[2]);
}else{
Row row = sheet.getRow(level );
Cell cell = row.getCell(cellNum);
String ts = nodeArr[0];
row.getCell(cellNum).setCellValue(nodeArr[0]);
int tempCellNum = cellNum;
int nodeColumn = 0;
List<Node> lnode = nodeMap.get(nodeArr[0]).getChildren();
for(int s=0;s<lnode.size();s++){
Node tempNode = lnode.get(s);
String id = tempNode.getId();
int size = map.get(id);
cellNum += size;
nodeColumn += size;
}
//System.out.println("+++++++++++++++++++++++"+cellNum);
sheet.addMergedRegion(new CellRangeAddress(level,level,tempCellNum,cellNum-1));
map.put(nodeArr[0], nodeColumn);
}
}
}
}
String fileName = "E:/a";
File file1 = new File(fileName);
if (!file1.exists()) {
file1.mkdirs();
}
File file = new File(fileName + "/test.xlsx");
FileOutputStream fileOut = new FileOutputStream(file);
work.write(fileOut);
work.close();
fileOut.close();
}
/**
* 构造假数据;
* @return
*/
public static List<Node> getData(){
List<Node> nodes = new ArrayList<Node>();
int id = 0;
for(int i=0;i<2;i++){
Node node = new Node();
node.setId(String.valueOf(id));
id += 1;
node.setLevel(1);
List<Node> nodess = new ArrayList<Node>();
for(int i1=0;i1<2;i1++){
Node node1 = new Node();
node1.setId(String.valueOf(id));
id += 1;
node1.setLevel(2);
List<Node> nodes1 = new ArrayList<Node>();
for(int i2=0;i2<2;i2++){
Node node2 = new Node();
node2.setId(String.valueOf(id));
id += 1;
node2.setLevel(3);
List<Node> nodes2 = new ArrayList<Node>();
for(int i3=0;i3<2;i3++){
Node node3 = new Node();
node3.setId(String.valueOf(id));
id += 1;
node3.setLevel(4);
List<Node> nodes3 = new ArrayList<Node>();
for(int i4=0;i4<3;i4++){
Node node4 = new Node();
node4.setId(String.valueOf(id));
id += 1;
node4.setLevel(5);
nodes3.add(node4);
}
node3.setChildren(nodes3);
nodes2.add(node3);
}
node2.setChildren(nodes2);
nodes1.add(node2);
}
node1.setChildren(nodes1);
nodess.add(node1);
}
node.setChildren(nodess);
nodes.add(node);
}
return nodes;
}
public static void main(String[] args) throws IOException {
List<Node> list = getData();
//System.out.println(list.size());
calculateData(list);
//test07241620();
//copyFile("E:\\assets","E:\\test");
}
}
保存node信息,map和list俩个有重复,可以做点筛选工作。自己选择。
Q/V/E : 554336412
微博:九道北