java中三层树形结构怎么写,Java实现树形层级构造的表格 二

继续中午....

源码链接:https://github.com/niqingyang/cntrust-report

源码中com.cntrust.report.example包下已经提供三种表格的实例代码,可直接运行。

列头表格:ColHeadReport

使用场景:类似常见的信息列表,由列头和数据行构成,如下图:

140354797.png

生成代码:

package com.cntrust.report.example;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import jxl.write.WriteException;

import com.cntrust.report.Cell;

import com.cntrust.report.ColHeadReport;

import com.cntrust.report.format.DefaultExcelReportFormat;

public class ColHeadReportTest {

/**

* @param args

* @throws IOException

* @throws WriteException

*/

public static void main(String[] args) throws WriteException, IOException {

List cols = new ArrayList();

/**

*

* 向列头列表中填充单元格

*

*/

cols.add(new Cell("sh1", "上海市第一中学", "-1"));

cols.add(new Cell("sh2", "上海市第二中学", "-1"));

//单元格的构造函数参数列表:单元格唯一标识、单元格显示名称、上级单元格的唯一标识

cols.add(new Cell("sh2,一班", "一班", "sh2"));

cols.add(new Cell("sh2,一班,数学", "数学", "sh2,一班"));

cols.add(new Cell("sh2,一班,语文", "语文", "sh2,一班"));

cols.add(new Cell("03", "二班", "sh2"));

cols.add(new Cell("03,01", "数学", "03"));

cols.add(new Cell("03,01", "语文", "03"));

cols.add(new Cell("04", "一班", "sh1"));

cols.add(new Cell("04,01", "数学", "04"));

cols.add(new Cell("04,02", "语文", "04"));

cols.add(new Cell("05", "二班", "sh1"));

cols.add(new Cell("05,01", "数学", "05"));

cols.add(new Cell("05,01", "语文", "05"));

//根据列头构建报表

ColHeadReport report = new ColHeadReport(cols);

for (int i = 1; i < 11; i++) {

//为上海市第二中学一班的数学添加数据

/**

*

* 向表格中插入数据有点类似于根据坐标打点的方式,

* 插入的数据根据数据单元格对应的行头单元格ID和列头单元格ID定位,

* 然后想单元格内部的Map集合内插入键值对,也支持直接插入一个Map集合

*

*/

report.setData(i, "sh2,一班,数学", "score", i);

/**

*

* 或是这样,通过Map集合的方式插入数据

*

*/

//Map map = new HashMap();

//map.put("score", i);

//report.setData(i, "sh2,一班,数学", map);

}

//设置标题

report.setTitle("上海市中学成绩表");

//默认的格式化类通过传人键“score”从数据单元格中读取出对应的值显示在Excel中

report.toExcel(new DefaultExcelReportFormat("score"), new File("D:/成绩表_ColHeadReport.xls"));

System.out.println("完成!");

}

}

行头表格:RowHeadReport

使用场景:这种表格不怎么常见,不过还是加上吧,有时候交叉表格不太好实现的就用这个替代一下也可以的,如下图:

140354798.png

生成代码:

格式化的代码:DailyReportFormat

package com.cntrust.report.example.format;

import jxl.format.Alignment;

import jxl.format.VerticalAlignment;

import jxl.write.Label;

import jxl.write.WritableCell;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WriteException;

import com.cntrust.report.Cell;

import com.cntrust.report.Report;

import com.cntrust.report.Row;

import com.cntrust.report.format.ExcelReportFormat;

public class DailyReportFormat extends ExcelReportFormat{

private String key = "co";

private WritableSheet sheet;

public DailyReportFormat(){

}

public DailyReportFormat(String key){

this.key = key;

}

@Override

public void after(WritableSheet sheet, Report report) throws WriteException {

// TODO Auto-generated method stub

}

@Override

public void before(WritableSheet sheet, Report report) throws WriteException {

sheet.setName("sheet1");

//设置第一列宽度

sheet.setColumnView(0, 28);

sheet.setColumnView(1, 30);

sheet.setColumnView(2, 125);

//设置第一二三四行高度

sheet.setRowView(0, 660);

//sheet.setRowView(1, 360);

//合并单元格

sheet.mergeCells(0, 0, report.getColSpan() - 1, 0);

// sheet.mergeCells(0, 1, report.getColSpan() - 1, 1);

WritableCellFormat format = this.getCellFormatWithNoBorder(this.getFont(WritableFont.ARIAL, 16, true, false));

Label title = new Label(0, 0, report.getTitle(), format);

sheet.addCell(title);

format = this.getCellFormatWithNoBorder(this.getFont(WritableFont.ARIAL, 10, true, false));

//水平居左

format.setAlignment(Alignment.LEFT);

//设置垂直居下

format.setVerticalAlignment(VerticalAlignment.BOTTOM);

//format.setShrinkToFit(false);

//sheet.addCell(new Label(0, 1, "日期:" + new Date().toLocaleString().substring(0, 10), format));

this.sheet = sheet;

}

@Override

public WritableCell getCell(int colIndex, int rowIndex, Cell c) throws WriteException {

WritableFont font = null;

WritableCell cell = null;

String cellType = c.getData().get("cellType") == null ? null : c.getData().get("cellType").toString();

if(Report.CELL_TYPE_Data.equals(cellType)){

font = this.getFont(WritableFont.ARIAL, 10, false, false);

WritableCellFormat format = this.getCellFormatWithSimpleBorder(font);

//水平居中

format.setAlignment(Alignment.LEFT);

//行高

this.sheet.setRowView(rowIndex, 1000);

//cell = new jxl.write.Number(colIndex, rowIndex, value, format);

cell = new Label(colIndex, rowIndex, c.getName(), format);

}else if(Report.CELL_TYPE_RowHead.equals(cellType)){

font = this.getFont(WritableFont.ARIAL, 10, true, false);

WritableCellFormat format = this.getCellFormatWithSimpleBorder(font);

cell = new Label(colIndex, rowIndex, c.getName(), format);

//行高

this.sheet.setRowView(rowIndex, 1000);

}else{

font = this.getFont(WritableFont.ARIAL, 10, true, false);

WritableCellFormat format = this.getCellFormatWithSimpleBorder(font);

cell = new Label(colIndex, rowIndex, c.getName(), format);

}

return cell;

}

@Override

public int getColOffset() {

return 0;

}

@Override

public int getRowOffset() {

return 1;

}

@Override

public int getTableRowHeight(Report report, Row row, int rowIndex) {

return 360;

}

}

构造表格代码:

package com.cntrust.report.example;

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import jxl.write.WriteException;

import com.cntrust.report.Cell;

import com.cntrust.report.Report;

import com.cntrust.report.RowHeadReport;

import com.cntrust.report.example.format.DailyReportFormat;

public class DailyReportTest {

/**

* @param args

* @throws IOException

* @throws WriteException

*/

public static void main(String[] args) throws WriteException, IOException {

/**

*

* 构造一个包含cellType的Map集合,将会放入Cell中,

* 用于在格式化时根据此类型进行不同的显示

*

*/

Map map = new HashMap();

map.put("cellType", Report.CELL_TYPE_Data);

List rowList = new ArrayList();

rowList.add(new Cell("日期", "日期", "-1"));

rowList.add(new Cell("项目", "项目", "日期"));

rowList.add(new Cell("内容", "内容", "项目"));

rowList.add(new Cell("01", "2014年7月16日", "-1"));

rowList.add(new Cell("01,01", "湖北一期", "01"));

Cell cell1 = new Cell("01,01,01", "今天我都做了些什么呢?", "01,01");

cell1.setData(map);

rowList.add(cell1);

Cell cell2 = new Cell("01,01,02", "我也不知道都做了些什么!", "01,01");

cell2.setData(map);

rowList.add(cell2);

rowList.add(new Cell("01,02", "湖北一期", "02"));

Cell cell3 = new Cell("01,02,01", "今天我都做了些什么呢?", "01,02");

cell3.setData(map);

rowList.add(cell3);

Cell cell4 = new Cell("01,02,02", "我也不知道都做了些什么!", "01,02");

cell4.setData(map);

rowList.add(cell4);

rowList.add(new Cell("02", "2014年7月17日", "-1"));

rowList.add(new Cell("02,01", "湖北二期", "02"));

Cell cell5 = new Cell("02,01,01", "我今天做了很多东西", "02,01");

cell5.setData(map);

rowList.add(cell5);

Cell cell6 = new Cell("02,01,02", "今天的确做了很多东西!", "02,01");

cell6.setData(map);

rowList.add(cell6);

Cell cell7 = new Cell("02,01,03", "嗯,我确定!", "02,01");

cell7.setData(map);

rowList.add(cell7);

Report report = new RowHeadReport(rowList);

report.toExcel(new DailyReportFormat(), new File("D:/日报_RowHeadReport.xls"));

System.out.println("完成!");

}

}

交叉表格:CrossReport

使用场景:这类表格使用还是蛮频繁的,特别是对大量数据进行统计,再以很多维度进行显示的情况下,如下图:

140354799.png

实例代码我就不写了,如果数据是从数据库中查询出来在展现,代码量还是蛮少的,作为一个非常懒的程序猿如果让我一一写的话,我会疯掉的!!有兴趣的话自己试着写写吧

140354800.gif

以上图片均为统一的接口导出的Excel文件,那如何在页面中用统一的方式展现呢?如下:

简单的CSS样式:

.table_currentDate{

width: 550px;

background-color: White;

text-align: center;

font-weight: 800;

font-size: 18px;

}

.table_dailyReport{

border-collapse: collapse;

border: 1px solid #CCCCCC;

width: 100%;

width: 550px;

background-color: White;

}

.table_dailyReport td{

word-wrap: nowrap;

word-break: keep-all;/**----强制文字不换行----**/

border-color: #E3E3E3;/**#ccc**/

padding: 1px 2px 1px 4px;

color: #333;

}

.colHead{

border-bottom: 1px solid windowtext;

height: 20px;

width: 90px;

text-align: center;

font-weight: 800;

font-size: 16px;

background-color: #F7F7F7;

}

.data{

border-left: 1px solid windowtext;

border-right: 1px solid windowtext;

border-bottom: 1px solid windowtext;

height: 70px;

width: 70px;

font-size: 14px;

text-align: center;

}

.today{

background-color: #29A0D3;

}

JSP中通过EL表达式进行统一的绘制表格(report对象已放入上下文中):

${cell.data['day']}

${cell.name}

差不多就这样吧,展示的思想就是通过行列坐标定位单元格,然后填充数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值