zoukankan      html  css  js  c++  java
  • 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();
        }
    }

  • 相关阅读:
    python操作mysql数据库
    Turtle绘制带颜色和字体的图形(Python3)
    Windows单机最大TCP连接数的问题
    This network connection does not exist
    python3 条件判断,循环,三元表达式
    基于SolidWorks设计算例的柴油机飞轮平衡孔的研究
    VOC2007数据集转换成CSV格式[
    xml -> csv
    目标检测 – 解析VOC和COCO格式并制作自己的数据集
    什么是电磁阀,电磁阀常见故障与解决方法
  • 原文地址:https://www.cnblogs.com/ckaifeng/p/5130933.html
Copyright © 2011-2022 走看看