goja-web/src/main/java/goja/mvc/csv/CsvUtil.java
@SuppressWarnings({"unchecked", "rawtypes"})
    public static String createCSV(List headers, List data, List columns) {
        StringBuffer strOut = new StringBuffer("");
        if (null != headers && !headers.isEmpty()) { // 如果文本不为空则添加到csv字符串中
            listToCSV(strOut, headers);
        }
        if (null == data || data.isEmpty()) {
            return strOut.toString();
        }

        Iterator itr = data.iterator();
        while (itr.hasNext()) {
            Object obj = itr.next(); // 将数据添加到csv字符串
            Class cls = obj.getClass();
            if (cls != null && cls.isArray()) {
                Object[] objs = (Object[]) obj;
                for (Object obj1 : objs) {
                    createCol(strOut, obj1);
                    strOut.append(COMMA);
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1); // 去点多余逗号
                strOut.append(NEWLINE);
            } else if (obj instanceof List) {
                List objlist = (List) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    listToCSV(strOut, objlist);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objlist.get((Integer) column));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Map) {
                Map objmap = (Map) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set keyset = objmap.keySet();
                    for (Object key : keyset) {
                        createCol(strOut, objmap.get(key));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objmap.get(column));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Model) {
                Model objmodel = (Model) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set<Entry<String, Object>> entries = objmodel._getAttrsEntrySet();
                    for (Entry entry : entries) {
                        createCol(strOut, entry.getValue());
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objmodel.get(column + EMPTY));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Record) {
                Record objrecord = (Record) obj;
                Map<String, Object> map = objrecord.getColumns();
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set<String> keys = map.keySet();
                    for (String key : keys) {
                        createCol(strOut, objrecord.get(key));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objrecord.get(column + EMPTY));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else {
                while (itr.hasNext()) {
                    Object objs = itr.next();
                    if (objs != null) {
                        createCol(strOut, objs);
                        strOut.append(NEWLINE);
                    }
                }
            }
            obj = null;
        }
        return strOut.toString();
    }
@SuppressWarnings({"unchecked", "rawtypes"})
    public static String createCSV(List headers, List data, List columns) {
        StringBuffer strOut = new StringBuffer("");
        if (null != headers && !headers.isEmpty()) { // 如果文本不为空则添加到csv字符串中
            listToCSV(strOut, headers);
        }
        if (null == data || data.isEmpty()) {
            return strOut.toString();
        }

        Iterator itr = data.iterator();
        while (itr.hasNext()) {
            Object obj = itr.next(); // 将数据添加到csv字符串
            Class cls = obj.getClass();
            if (cls != null && cls.isArray()) {
                Object[] objs = (Object[]) obj;
                for (Object obj1 : objs) {
                    createCol(strOut, obj1);
                    strOut.append(COMMA);
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1); // 去点多余逗号
                strOut.append(NEWLINE);
            } else if (obj instanceof List) {
                List objlist = (List) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    listToCSV(strOut, objlist);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objlist.get((Integer) column));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Map) {
                Map objmap = (Map) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set keyset = objmap.keySet();
                    for (Object key : keyset) {
                        createCol(strOut, objmap.get(key));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objmap.get(column));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Model) {
                Model objmodel = (Model) obj;
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set<Entry<String, Object>> entries = objmodel._getAttrsEntrySet();
                    for (Entry entry : entries) {
                        createCol(strOut, entry.getValue());
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objmodel.get(column + EMPTY));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else if (obj instanceof Record) {
                Record objrecord = (Record) obj;
                Map<String, Object> map = objrecord.getColumns();
                if (null == columns || columns.isEmpty()) { // 如果没有限制，默认全部显示
                    Set<String> keys = map.keySet();
                    for (String key : keys) {
                        createCol(strOut, objrecord.get(key));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                } else {
                    for (Object column : columns) {
                        createCol(strOut, objrecord.get(column + EMPTY));
                        strOut.append(COMMA);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(NEWLINE);
                }
            } else {
                while (itr.hasNext()) {
                    Object objs = itr.next();
                    if (objs != null) {
                        createCol(strOut, objs);
                        strOut.append(NEWLINE);
                    }
                }
            }
            obj = null;
        }
        return strOut.toString();
    }
@ SuppressWarnings ( { "unchecked" , "rawtypes" } ) public static String createCSV ( List headers , List data , List columns ) { StringBuffer strOut = new StringBuffer ( "" ) ; if ( null != headers && ! headers . isEmpty ( ) ) { // 如果文本不为空则添加到csv字符串中 listToCSV ( strOut , headers ) ; } if ( null == data || data . isEmpty ( ) ) { return strOut . toString ( ) ; } Iterator itr = data . iterator ( ) ; while ( itr . hasNext ( ) ) { Object obj = itr . next ( ) ; // 将数据添加到csv字符串 Class cls = obj . getClass ( ) ; if ( cls != null && cls . isArray ( ) ) { Object [ ] objs = ( Object [ ] ) obj ; for ( Object obj1 : objs ) { createCol ( strOut , obj1 ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; // 去点多余逗号 strOut . append ( NEWLINE ) ; } else if ( obj instanceof List ) { List objlist = ( List ) obj ; if ( null == columns || columns . isEmpty ( ) ) { // 如果没有限制，默认全部显示 listToCSV ( strOut , objlist ) ; } else { for ( Object column : columns ) { createCol ( strOut , objlist . get ( ( Integer ) column ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } } else if ( obj instanceof Map ) { Map objmap = ( Map ) obj ; if ( null == columns || columns . isEmpty ( ) ) { // 如果没有限制，默认全部显示 Set keyset = objmap . keySet ( ) ; for ( Object key : keyset ) { createCol ( strOut , objmap . get ( key ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } else { for ( Object column : columns ) { createCol ( strOut , objmap . get ( column ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } } else if ( obj instanceof Model ) { Model objmodel = ( Model ) obj ; if ( null == columns || columns . isEmpty ( ) ) { // 如果没有限制，默认全部显示 Set < Entry < String , Object > > entries = objmodel . _getAttrsEntrySet ( ) ; for ( Entry entry : entries ) { createCol ( strOut , entry . getValue ( ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } else { for ( Object column : columns ) { createCol ( strOut , objmodel . get ( column + EMPTY ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } } else if ( obj instanceof Record ) { Record objrecord = ( Record ) obj ; Map < String , Object > map = objrecord . getColumns ( ) ; if ( null == columns || columns . isEmpty ( ) ) { // 如果没有限制，默认全部显示 Set < String > keys = map . keySet ( ) ; for ( String key : keys ) { createCol ( strOut , objrecord . get ( key ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } else { for ( Object column : columns ) { createCol ( strOut , objrecord . get ( column + EMPTY ) ) ; strOut . append ( COMMA ) ; } strOut = strOut . deleteCharAt ( strOut . length ( ) - 1 ) ; strOut . append ( NEWLINE ) ; } } else { while ( itr . hasNext ( ) ) { Object objs = itr . next ( ) ; if ( objs != null ) { createCol ( strOut , objs ) ; strOut . append ( NEWLINE ) ; } } } obj = null ; } return strOut . toString ( ) ; }
将文本头与数据共同转成csv字符串

@param headers 列属性
@param data    数据
@param columns 需要显示列的key值
@return csv字符串
将文本头与数据共同转成csv字符串