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. vert.x学习(一),开篇之hello world

    今天决定学习下vert.x这个框架,记录下学习笔记. 下面列下我的开发环境: Java版本 1.8 maven版本 3.3 IDEA版本 2016 在idea中使用vert.x不用下载或安装其他东西了 ...

  2. SET QUOTED_IDENTIFIER ON和SET ANSI_NULLS ON

    distinct是sqlserver的标识符,如果想以distinct为表时,在QUOTED_IDENTIFIER为off的情况下,是不能创建表名为distinct的表的,因为在QUOTED_IDEN ...

  3. vuejsLearn--- -- 怎么查看、修改、追加数据---->data对象

    实例观察的数据对象.可以用一个新的对象替换.实例代理了它的数据对象的属 我们现在对data2添加几项 使用数组push()追加 但是直接这样不能进行数组操作 var data2 = { city: ' ...

  4. 如何安装并使用hibernate tools

    参考资料:http://radiumwong.iteye.com/blog/358585 http://linjia880714.iteye.com/blog/859334 Hibernate Too ...

  5. Python之路【第十九章】:Django进阶

    Django路由规则 1.基于正则的URL 在templates目录下创建index.html.detail.html文件 <!DOCTYPE html> <html lang=&q ...

  6. Python之路【第十八章】:Django基础

    Django基本配置 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Se ...

  7. Python模拟入栈出栈操作

    目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [ ...

  8. js toString()

  9. jS正则和WEB框架Django的入门

    JS正则 -test 判断字符串是否符合规定的正则表达式 -exec 获取匹配的数据 test的例子: 从上述的例子我们可以看出,如果rep.test匹配到了就返回true,否则返回false exe ...

  10. JFinal学习

    1 jfinal-1.9-bin.jar 2 继承Controller编写控制器 public void sendJPushToXXX() { String userId = getPara(&quo ...