import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.poi.ss.formula.functions.T;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;

import com.gsoft.cos.core.util.Assert;
import com.gsoft.modules.infrastructureManagement.utils.execl.ColumnVO;

/**
 * @ClassName: TableUtil
 * @Description: excel工具类
 */
public class TableUtil {

public static String print(List<List<ColumnVO>> list, List<T> datas) {

return print(list, null, datas);
    }

@SuppressWarnings("unchecked")
    public static String print(List<List<ColumnVO>> list, String title,
            List<?> datas) {
        Document doc = new DOMDocument();
        doc.setXMLEncoding("UTF-8");
        Element table = doc.addElement("table");
        table.addAttribute("class", "print-table");
        if (Assert.isNotEmpty(title)) {
            Element caption = table.addElement("caption");
            caption.setText(title);
        }
        // 表头
        List<String> contains = new ArrayList<String>();
        Map<String, String> keyMap = new HashMap<String, String>();
        for (int i = 0; i < list.size(); i++) {
            List<ColumnVO> columns = list.get(i);
            int startColumn = 0;
            Element tr = table.addElement("tr");
            tr.addAttribute("class", "print-th");
            for (int j = 0; j < columns.size(); j++) {
                if (!contains.contains(startColumn + "," + i)) {
                    ColumnVO column = columns.get(j);
                    if ((Assert.isNotEmpty(column.getHidden()) && column
                            .getHidden()) || !column.isExported()) {
                        continue;
                    }
                    if (Assert.isNotEmpty(column.getField())) {
                        keyMap.put(String.valueOf(startColumn),
                                column.getField());
                    }
                    Element td = tr.addElement("td");
                    td.setText(column.getTitle());
                    int colspan = 1;
                    if ((Assert.isNotEmpty(column.getColspan()) && column
                            .getColspan() > 1)) {
                        colspan = column.getColspan();
                    }
                    int rowspan = 1;
                    if (Assert.isNotEmpty(column.getRowspan())
                            && column.getRowspan() > 1) {
                        rowspan = column.getRowspan();
                    }
                    if (colspan > 1) {
                        td.addAttribute("colspan", String.valueOf(colspan));
                    }
                    if (rowspan > 1) {
                        contains.add(startColumn + "," + (rowspan - 1));
                        td.addAttribute("rowspan", String.valueOf(rowspan));
                    }
                    startColumn += colspan;
                } else {
                    startColumn += 1;
                }
            }
        }

Object object = null;
        // 遍历集合
        if (datas != null) {
            for (int i = 0; i < datas.size(); i++) {
                object = datas.get(i);
                Element tr = table.addElement("tr");
                tr.addAttribute("class", "print-tr");
                Map<String, Object> map = new HashMap<String, Object>();
                if (!(object instanceof Map)) {
                    try {
                        map = BeanUtils.describe(object);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }

for (int j = 0; j < keyMap.size(); j++) {
                    String key = keyMap.get(String.valueOf(j));
                    String value = MapUtils.getString(map, key);
                    Element td = tr.addElement("td");
                    td.addAttribute("class", "print-td");
                    td.setText(value);
                }
            }
        }
        return table.asXML();
    }
}

easyui DataGrid 工具类之 TableUtil class的更多相关文章

  1. easyui DataGrid 工具类之 WorkbookUtil class

    /** * @Title: WorkbookUtil.java * @Description: excel工具类 * @date 2014年5月29日 上午10:36:42 * @version V1 ...

  2. easyui DataGrid 工具类之 util js

    var jq;                var tab;                var tabsIndex;                                /**     ...

  3. easyui DataGrid 工具类之 Utils class

    import java.lang.reflect.InvocationTargetException;import java.text.ParseException;import java.text. ...

  4. easyui DataGrid 工具类之 后台生成列

    @SuppressWarnings({ "rawtypes", "unchecked" })    public Map<String, Object&g ...

  5. easyui DataGrid 工具类之 列属性class

    public class ColumnVO { /**     * 列标题文本     */    private String title; /**     * 列字段名称     */    pr ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(8)-MVC与EasyUI DataGrid 分页

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI Datagrid在加载的时候会提交一些分页的信息到后台,我们需要根据这些信息来进行数据分页再次返回到前台 实 ...

  7. 控制EasyUI DataGrid高度

    这次要说的是控制EasyUI的高度,平时我公司的项目,用EasyUI较多,然后datagrid这个组件是用的非常多的.平时我们都是固定高度,常见代码如下:             <table  ...

  8. easyui datagrid标题列宽度自适应

    最近项目中使用easyui做前端界面,相信大部分使用过easyui datagrid的朋友有这么一个疑问:如果在columns中不设置width属性能不能写个方法让datagrid的头部标题和数据主体 ...

  9. 实现easyui datagrid在没有数据时显示相关提示内容

    本示例实现easyui datagrid加载/查询数据时,如果没有相关记录,则在datagrid中显示没有相关记录的提示信息,效果如下图所示 本实例要实现如下图所示的效果: 本示例easyui版本为1 ...

随机推荐

  1. Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]

    Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...

  2. Raspberry pi 添加vnc远程桌面控制

    // 安装服务 apt-get install tightvncserver // 设置连接密码 vncpasswd // 在端口1处开启服务 tightvncserver :1

  3. 将时间转换为xxx天前 xxx..前

    <?php echo (time_fitle('2016-08-17 17:00:00')); function time_fitle($date){ date_default_timezone ...

  4. Canvas 知识体系简单总结

    Canvas 知识体系简单总结 标签(空格分隔): HTML5 Canvas 本文原创,如需转载,请注明出处 前言 知识点零零散散,一个上午整理了一下,内容不多,方便记忆. 本文不是教程,如需教程移步 ...

  5. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  6. OC ---- 字典集合 iOS学习-----细碎知识点总结

    实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:", @"age" ...

  7. java new Date()得到的时间和系统时间不一样

    造成这种问题的原因是:操作系统时区跟JVM的时区不一致. [root@paas244 ~]# timedatectl Local time: Thu 2016-12-29 15:35:44 CST U ...

  8. Oracle 11g中修改默认密码过期天数和锁定次数

    Oracle 11g中修改默认密码过期天数和锁定次数 密码过期的原因一般有两种可能: 一.由于Oracle中默认在default概要文件中设置了"PASSWORD_LIFE_TIME=180 ...

  9. 制作自己的嵌入式Linux电脑_转

    制作自己的嵌入式Linux电脑 http://os.51cto.com/art/201409/450334.htm 原文链接:http://blog.jobbole.com/75414/ 包含器件选择 ...

  10. Wampserver 2.5 多站点配置方法

    写在开头:本文适用于wampserver2.5版本,和wamp的老版本配置有语法上的区别,笔者正是因为被老版本的配置办法给整迷糊了所以才总结了一篇针对2.5版本的配置方法,如果您还停留在1.x或着已经 ...