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. 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

    下载 Xcode 8,配置 iOS 10 和 Swift 3 (可选)通过命令行编译 除 非你想使用命令行编译,使用 Swift 3.0 的工具链并不需要对项目做任何改变.如果你想的话,打开 Xcod ...

  2. Tomcat catalina.out日志使用log4j按天分割

    由于tomcat catalina.out日志不会自动分割, 一.日志分割所需包在附近中 1. 压缩包中有三个jar包: log4j-1.2.16.jar tomcat-juli-adapters.j ...

  3. Glide 魔法般的自定义扩展

    使用过Glide的朋友都明白,简单的用法就是: Glide.with(context) .load("http://inthecheesefactory.com/uploads/source ...

  4. Linux下browser-sync无法启动Chrome的解决方法

    笔者的环境: OS: Ubuntu Linux Browser: Chrome, Firefox 每次希望启动chrome浏览器,系统都会报错: browser-sync start -s --dir ...

  5. 由于某IP大频率提交评论导致服务器宕机

    早上突然收到dnspod的宕机通知(好久没收到了,有点手足无措). 服务器在上午10:40时达到85%.uptime显示cpu利用率达到35.不宕才怪. 按照之前的经验,应该是触发一个特别耗CPU的处 ...

  6. 1.Linux是什么?

    UNIX设计理念: 所有的程序或系统装置都是文件. 不管构建编辑器还是附属文件,所写的程序只有一个目的,就是有效地完成目标 操作系统:应用程序->系统调用->内核->硬件.其中系统调 ...

  7. Apple Reject

    2016年11月10日 上午1:15 发件人 Apple 2. 1 PERFORMANCE: APP COMPLETENESS Thank you for your resubmission. Per ...

  8. 移动端的传统click事件延迟和点透现象

    一.场景描述: 1.A/B两个层上下z轴重叠. 2.上层的A点击后消失或移开.(这一点很重要) 3.B元素本身有默认click事件(如a标签) 或 B绑定了click事件. 在以上情况下,点击A/B重 ...

  9. Linux环境安装MySQL数据库

    1.  下载mysql安装包 下载地址1:  http://www.mysql.com/   (mysql官网) 下载地址2:  http://mirrors.sohu.com/mysql/   (其 ...

  10. 当利用pip安装模块出现错误时咋办

    >在DOS窗口中到Python安装路径的scripts中执行  pip install pyperclip 出现错误 >>错误提示:Fatal error in launcher: ...