• easyui DataGrid 工具类之 TableUtil class


    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();
        }
    }

  • 相关阅读:
    获取spring源码并导入到eclipse
    Android的EditText设置可编辑与不可编辑的方法
    漫谈设计模式笔记:模板模式
    jfreechar中文乱码设置主题样式解决
    FrameLayout布局下让图片居中的方法
    java典型模块实例1:英文,数字,中文混合的验证码
    学习Lucene笔记一:创建索引
    How to Display a PDF File in a HTML Web Page
    NET数据类型及字节数
    2012年1月编程语言排行榜
  • 原文地址:https://www.cnblogs.com/ckaifeng/p/5130933.html
Copyright © 2020-2023  润新知