将树节点以列方式导出EXCEL

学java不容易 2011-09-29 06:16:17
将树节点:

保存在数据库中是以id,parentid,name...方式存储的
现在要将上面的节点导成如下的EXCEL形式:

请教高手指点一二,非常感谢...
...全文
615 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
学java不容易 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yktd26 的回复:]

最好是你自己去试一下
然后不符合你想法的地方你可以试着去改一下
[/Quote]
我将你的方法修改了一下,请高手指点。。。
private Object[][] recursive(Object[][] source, int level, List<ServiceLined> lineds, ServiceLined currentObj) {
if (lineds!=null&&lineds.size()>0) { //判断获取的节点集合是否为空
for (int i = 1; i < lineds.size(); i++) { //循环遍历节点集合
ServiceLined lined = lineds.get(i); //得到单个节点
int count = mgr.findCountSubLined(lined); //获取下一级子节点数量
if (count > 0) { //如果存在子节点嵌套加入数组中
for (int j = count - 1; j >= 0; j--) {
source = recursive(source, level + 1, lineds, lineds.get(j));
}
}
int rowcount = source.length; //获取现有行数
int colcount = level + 1; //获取现有列数也是tree中level数

source = Arrays.copyOf(source, ++rowcount); //拷贝原有数据并增加一新行
source[rowcount - 1] = new Object[colcount]; //创建这一新行中数组
source[rowcount - 1][colcount - 1] = currentObj; //赋值
}
}


return source;
}

/**
* 导出业务线下的模板信息
* @return
*/
public String exportServiceLine(){
try {
Integer parentId = serviceLineh.getId();
List<ServiceLined> lineds = mgr.selectAllChild(parentId, true); //得到模板1下的所有节点集合

if(lineds!=null&&lineds.size()>0){ //判断获取的节点集合是否为空
ServiceLined param = new ServiceLined();
param.setParentid(parentId);
param.setOther(Constant.IS_ROOT);
ServiceLined rlined = mgr.findRootLined(param); //获取根节点
Object[][] table = new Object[1][1];
table = recursive(table, 0, lineds, rlined);

HSSFWorkbook workbook = new HSSFWorkbook(); //创建xls workbook
HSSFSheet sheet = workbook.createSheet("page1"); //创建sheet

int newrow = 0;
for (int i = table.length - 1; i >= 0; i--) {
HSSFRow row = sheet.createRow(newrow++);
for (int j = 0; j < table[i].length; j++) {
if (table[i][j] != null) {
row.createCell((short)j).setCellValue(table[i][j].toString()); //输出节点值到cell
}
}
}
/*输出到文件*/
FileOutputStream out = new FileOutputStream("f://test.xls");
workbook.write(out);

out.close();
}

getResponse().setContentType("text/html;charset=utf-8");
getResponse().getWriter().print("导出业务线模板信息成功!");

} catch (Exception e) {
logger.error(e);
}

return null;
}
学java不容易 2011-10-08
  • 打赏
  • 举报
回复
终于搞定了,将成果展示一下。。。
/**
* 导出业务线下的模板信息
* @return
*/
public String exportServiceLine(){
try {
Integer parentId = serviceLineh.getId();
List<ServiceLined> lineds = mgr.selectAllChild(parentId, true);

List<ServiceLined> plineds=new ArrayList<ServiceLined>();
List<ServiceLined> slineds=new ArrayList<ServiceLined>();
List<ServiceLined> llineds=new ArrayList<ServiceLined>();

if(lineds!=null&&lineds.size()>0){
logger.debug("lineds**********************"+lineds.size());
ServiceLined param = new ServiceLined();
param.setParentid(parentId);
param.setOther(Constant.IS_ROOT);
ServiceLined rlined = mgr.findRootLined(param);
logger.debug("rlined.id*****************************"+rlined.getId());

for (int i = 1; i < lineds.size(); i++) {
ServiceLined lined = lineds.get(i);
logger.debug("equals**************"+lined.getParentid().equals(rlined.getId()));
if (lined.getParentid().equals(rlined.getId())) {
//int countSub = mgr.findCountSubLined(lined);
plineds.add(lined);

}
}

logger.debug("plineds**********************"+plineds.size());
if (plineds.size()>0) {
for (int i = 0; i < plineds.size(); i++) {
for (int j = 1; j < lineds.size(); j++) {
ServiceLined lined = lineds.get(j);
if(plineds.get(i).getId().equals(lined.getParentid())){
slineds.add(lined);
}
}
}

logger.debug("slineds**********************"+slineds.size());
logger.debug("lineds**********************"+lineds.size());

if(slineds.size()>0){
for (int i = 0; i < slineds.size(); i++) {
for (int j = 0; j < lineds.size(); j++) {
ServiceLined lined = lineds.get(j);
if (slineds.get(i).getId().equals(lined.getParentid())) {
llineds.add(lined);
}
}
}
}

logger.debug("llineds**********************"+llineds.size());
}

OutputStream os = new FileOutputStream(new File("f://test.xls"));
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet=book.createSheet("test", 0);
sheet.setColumnView(0, 20);
sheet.setColumnView(1, 20);
sheet.setColumnView(2, 60);
sheet.setRowView(0, 500);
WritableCellFormat wcff= new WritableCellFormat();
wcff.setWrap(true);
wcff.setShrinkToFit(true);

Label lableBuh=new Label(0,0,"父节点信息",wcff);
Label lableBuh2=new Label(1,0,"子节点信息",wcff);
Label lableBuh3=new Label(2,0,"叶节点信息",wcff);

sheet.addCell(lableBuh);
sheet.addCell(lableBuh2);
sheet.addCell(lableBuh3);

int count = 1;

Label lableBu=new Label(0,count,"",wcff);
Label lableBu2=new Label(1,count,"",wcff);
Label lableBu3=new Label(2,count,"",wcff);

boolean flag = false;
if (plineds.size()>0) {
for (int i = 0; i < plineds.size(); i++) {
lableBu = new Label(0,count,plineds.get(i).getNode(),wcff);

int countSubject = mgr.findCountSubLined(plineds.get(i));

sheet.addCell(lableBu);
if (countSubject>1) {
sheet.mergeCells(0, count, 0, count+countSubject-1);
}

if(slineds.size()>0){
for (int j = 0; j < slineds.size(); j++) {
if (plineds.get(i).getId().equals(slineds.get(j).getParentid())) {
lableBu2=new Label(1,count,slineds.get(j).getNode(),wcff);

sheet.addCell(lableBu2);

if(llineds.size()>0){
String nodeName = "";
for (int k = 0; k < llineds.size(); k++) {
if (slineds.get(j).getId().equals(llineds.get(k).getParentid())) {
nodeName += llineds.get(k).getNode()+" ";

int countSub = mgr.findCountSubLined(llineds.get(k));
if (countSub>0) {
for (int l = 0; l < lineds.size(); l++) {
if (llineds.get(k).getId().equals(lineds.get(l).getParentid())) {
nodeName += "\n "+lineds.get(l).getNode();

countSub = mgr.findCountSubLined(lineds.get(l));

if (countSub>0) {
for (int m = 0; m < lineds.size(); m++) {
if (lineds.get(l).getId().equals(lineds.get(m).getParentid())) {
nodeName += "\n "+lineds.get(m).getNode()+" ";
}
}
}
}
}
}


lableBu3 = new Label(2,count,nodeName,wcff);
sheet.addCell(lableBu3);

//count++;
}
}
}

count++;
flag = true;
}
}
}

if (!flag) {
count++;
}

}
}

book.write();
book.close();
os.close();

//getResponse().setContentType("text/html;charset=utf-8");
//getResponse().getWriter().print("导出业务线模板信息成功!");
}

} catch (Exception e) {
logger.error(e);
}

return null;
}
yktd26 2011-10-07
  • 打赏
  • 举报
回复
最好是你自己去试一下
然后不符合你想法的地方你可以试着去改一下
学java不容易 2011-10-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yktd26 的回复:]

给你个完整的
Java code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { //这个方法连到你的action里面
TreeModel tm = jTree1.getModel……
[/Quote]
请问你这段代码是按照以下这些规则吗?----
模板1下的一级节点放在第一列,二级节点放在第二列,三级及以下节点放在第三列,如果一级节点下有多个二级节点,则将一级节点跨行处理,二级节点及三级节点不必跨行。。。
学java不容易 2011-10-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yktd26 的回复:]

Java code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
TreeModel tm = jTree1.getModel();
Object root = tm.getRoot();
Object[][] table = new Object[1][1];
ta……
[/Quote]
高手能将这段代码解释一下么?或者加下注释什么的?
学java不容易 2011-10-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 softroad 的回复:]

这种方式不合适吧
[/Quote]
没办法啊,公司的项目需要。。。
yktd26 2011-10-05
  • 打赏
  • 举报
回复
给你个完整的
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {    				//这个方法连到你的action里面                                     
TreeModel tm = jTree1.getModel(); //获取jTree的model含有数据
Object root = tm.getRoot(); //获取根节点
Object[][] table = new Object[1][1];
table = recursive(table, 0, tm, root);

HSSFWorkbook workbook = new HSSFWorkbook(); //创建xls workbook
HSSFSheet sheet = workbook.createSheet("page1"); //创建sheet

int newrow = 0;
for (int i = table.length - 1; i >= 0; i--) {
HSSFRow row = sheet.createRow(newrow++);
for (int j = 0; j < table[i].length; j++) {
if (table[i][j] != null) {
row.createCell(j).setCellValue(table[i][j].toString()); //输出节点值到cell
}
}
}
/*输出到文件*/
FileOutputStream out = null;
try {
out = new FileOutputStream("E:\\test.xls");
workbook.write(out);
}
catch (FileNotFoundException ex) {ex.printStackTrace();}
catch (IOException ex) {ex.printStackTrace();}
finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {ex.printStackTrace();}
}
}
private Object[][] recursive(Object[][] source, int level, TreeModel tm, Object currentObj) {
int count = tm.getChildCount(currentObj); //获取下一级子节点数量
if (count > 0) { //如果存在子节点嵌套加入数组中
for (int i = count - 1; i >= 0; i--) {
source = recursive(source, level + 1, tm, tm.getChild(currentObj, i));
}
}
int rowcount = source.length; //获取现有行数
int colcount = level + 1; //获取现有列数也是tree中level数

source = Arrays.copyOf(source, ++rowcount); //拷贝原有数据并增加一新行
source[rowcount - 1] = new Object[colcount]; //创建这一新行中数组
source[rowcount - 1][colcount - 1] = currentObj; //赋值

return source;
}
学java不容易 2011-09-30
  • 打赏
  • 举报
回复

||
\ || /
\ /
\/
学java不容易 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yktd26 的回复:]

分析树将其解析到一个二维数组中,
然后遍历这个二维数组存在excel中
简单的话做成cvs文件每个cell用tab分隔

不然就是poi hssf/xssf, 一个道理
[/Quote]
实现起来没那么容易啊,哥们给个代码例子吧。。。只能用二维数组吗?可以用什么集合吗?
学java不容易 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yktd26 的回复:]

分析树将其解析到一个二维数组中,
然后遍历这个二维数组存在excel中
简单的话做成cvs文件每个cell用tab分隔

不然就是poi hssf/xssf, 一个道理
[/Quote]
实现起来没那么容易啊,哥们给个代码例子吧。。。
yktd26 2011-09-30
  • 打赏
  • 举报
回复
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
TreeModel tm = jTree1.getModel();
Object root = tm.getRoot();
Object[][] table = new Object[1][1];
table = recursive(table, 0, tm, root);

for (int i = table.length - 1; i >= 0; i--){
for (int j = 0; j < table[i].length; j++){
System.out.print(table[i][j]+" ");
}
System.out.println();
}
}

private Object[][] recursive(Object[][] source, int level, TreeModel tm, Object currentObj){
int count = tm.getChildCount(currentObj);
if (count > 0){
for (int i = count - 1; i >= 0 ; i--){
source = recursive(source, level+1, tm, tm.getChild(currentObj, i));
}
}
int rowcount = source.length;
int colcount = level+1;
if (colcount > source[0].length){
for (int i = 0; i< rowcount; i++){
source[i] = Arrays.copyOf(source[i], colcount);
}
}
source = Arrays.copyOf(source, ++rowcount);
source[rowcount - 1] = new Object[colcount];
source[rowcount - 1][colcount - 1] = currentObj;

return source;
}
softroad 2011-09-30
  • 打赏
  • 举报
回复
这种方式不合适吧
学java不容易 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 softroad 的回复:]

引用 5 楼 softroad 的回复:

以这种方式输出?
http://hi.csdn.net/attachment/201109/30/51138_1317350083p8tC.jpg
[/Quote]
对啊。。。
softroad 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 softroad 的回复:]

以这种方式输出?
http://hi.csdn.net/attachment/201109/30/51138_1317350083p8tC.jpg
[/Quote]
softroad 2011-09-30
  • 打赏
  • 举报
回复
yktd26 2011-09-29
  • 打赏
  • 举报
回复
分析树将其解析到一个二维数组中,
然后遍历这个二维数组存在excel中
简单的话做成cvs文件每个cell用tab分隔

不然就是poi hssf/xssf, 一个道理

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧