Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
   */
  package com.github.sog.render.csv;
  
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

该类是把数据转化成csv字符串做了简要的封装 List headers是显示数据每列的属性,建议使用字符 List data数据,单个元素格式可以为Array,list,map,model,record List columns 表示需要显示的数据,如果data是Array与list,输入希望显示列的下标即可 如果data是map,model,record,输入希望显示列的key值即可。
 
 public class CsvUtil {
 
     private static UserSettings userSettings = new UserSettings();
 
     private CsvUtil() {
     }

    
将文本头与数据共同转成csv字符串

Parameters:
headers 列属性
data 数据
columns 需要显示列的key值
Returns:
csv字符串
 
     @SuppressWarnings({"unchecked""rawtypes"})
     public static String createCSV(List headersList dataList columns) {
         StringBuffer strOut = new StringBuffer("");
         if (null != headers && !headers.isEmpty()) { // 如果文本不为空则添加到csv字符串中
             listToCSV(strOutheaders);
         }
         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()) {
                 if (obj != null) {
                     Object[] objs = (Object[]) obj;
                     if (objs != null) {
                         for (Object obj1 : objs) {
                             createCol(strOutobj1);
                             strOut.append(.);
                         }
                         strOut = strOut.deleteCharAt(strOut.length() - 1); // 去点多余逗号
                         strOut.append(.);
                     }
                 }
             } else if (obj instanceof List) {
                 List objlist = (Listobj;
                 if (null == columns || columns.isEmpty()) { // 如果没有限制,默认全部显示
                     listToCSV(strOutobjlist);
                 } else {
                     for (Object column : columns) {
                         createCol(strOutobjlist.get((Integercolumn));
                         strOut.append(.);
                     }
                     strOut = strOut.deleteCharAt(strOut.length() - 1);
                     strOut.append(.);
                 }
             } else if (obj instanceof Map) {
                 Map objmap = (Mapobj;
                 if (null == columns || columns.isEmpty()) { // 如果没有限制,默认全部显示
                     Set keyset = objmap.keySet();
                     for (Object key : keyset) {
                         createCol(strOutobjmap.get(key));
                         strOut.append(.);
                     }
                     strOut = strOut.deleteCharAt(strOut.length() - 1);
                     strOut.append(.);
                 } else {
                     for (Object column : columns) {
                         createCol(strOutobjmap.get(column));
                         strOut.append(.);
                     }
                     strOut = strOut.deleteCharAt(strOut.length() - 1);
                     strOut.append(.);
                 }
             } else if (obj instanceof Model) {
                 Model objmodel = (Modelobj;
                 if (null == columns || columns.isEmpty()) { // 如果没有限制,默认全部显示
                    Set<Entry<StringObject>> entries = objmodel.getAttrsEntrySet();
                    for (Entry entry : entries) {
                        createCol(strOutentry.getValue());
                        strOut.append(.);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(.);
                } else {
                    for (Object column : columns) {
                        createCol(strOutobjmodel.get(column + .));
                        strOut.append(.);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(.);
                }
            } else if (obj instanceof Record) {
                Record objrecord = (Recordobj;
                Map<StringObjectmap = objrecord.getColumns();
                if (null == columns || columns.isEmpty()) { // 如果没有限制,默认全部显示
                    Set<Stringkeys = map.keySet();
                    for (String key : keys) {
                        createCol(strOutobjrecord.get(key));
                        strOut.append(.);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(.);
                } else {
                    for (Object column : columns) {
                        createCol(strOutobjrecord.get(column + .));
                        strOut.append(.);
                    }
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append(.);
                }
            } else {
                while (itr.hasNext()) {
                    Object objs = itr.next();
                    if (objs != null) {
                        createCol(strOutobjs);
                        strOut.append(.);
                    }
                }
            }
            obj = null;
        }
        return strOut.toString();
    }

    
把单纯的集合转化成csv字符串

Parameters:
strOut StringBuffer
list 数据
    public static void listToCSV(StringBuffer strOutList<?> list) {
        if (null != list && !list.isEmpty()) { // 如果文本不为空则添加到csv字符串中
            for (Object aList : list) {
                createCol(strOutaList);
                strOut.append(.);
            }
            strOut = strOut.deleteCharAt(strOut.length() - 1);
            strOut.append(.);
        }
    }
    // 把单个元素转化
    public static void createCol(StringBuffer strOutObject obj) {
        if (obj != null) {
            strOut.append(.);
            String content = null;
            if (obj instanceof Boolean) {
                content = ((Booleanobj).toString();
            } else if (obj instanceof Calendar) {
                content = ((Calendarobj).toString();
            } else if (obj instanceof Timestamp) {
                content = ..format(new Date(((Timestampobj).getTime()));
            } else if (obj instanceof Date) {
                content = ..format((Dateobj);
            } else {
                content = write(String.valueOf(obj));
            }
            strOut.append(content);
            strOut.append(.);
        } else {
            strOut.append("\" \" ");
        }
    }
    public static String write(String content) {
        boolean textQualify = .;
        if (content.length() > 0) {
            content = content.trim();
        }
        if (!textQualify
                && .
                && (content.indexOf(.) > -1 || content.indexOf(.) > -1
                || (content.indexOf(.) > -1 || content.indexOf(.) > -1)
                || (content.indexOf(.) > -1)
                || (content.length() > 0 && content.charAt(0) == .) || (content.length() == 0))) {
            textQualify = true;
        }
        if (. && !textQualify && content.length() > 0) {
            char firstLetter = content.charAt(0);
            if (firstLetter == . || firstLetter == .) {
                textQualify = true;
            }
            if (!textQualify && content.length() > 1) {
                char lastLetter = content.charAt(content.length() - 1);
                if (lastLetter == . || lastLetter == .) {
                    textQualify = true;
                }
            }
        }
        if (textQualify) {
            if (. == .) {
                content = replace(content"" + ."" + . + .);
                content = replace(content"" + ."" + .
                        + .);
            } else {
                content = replace(content"" + ."" + .
                        + .);
            }
        } else if (. == .) {
            content = replace(content"" + ."" + . + .);
            content = replace(content"" + ."" + . + .);
            content = replace(content"" + ."" + . + .);
            content = replace(content"" + ."" + . + .);
        }
        return content;
    }
    // 特殊字符的转换 "\t" -> "\\t"
    public static String replace(String originalString patternString replace) {
        final int len = pattern.length();
        int found = original.indexOf(pattern);
        if (found > -1) {
            StringBuffer sb = new StringBuffer();
            int start = 0;
            while (found != -1) {
                sb.append(original.substring(startfound));
                sb.append(replace);
                start = found + len;
                found = original.indexOf(patternstart);
            }
            sb.append(original.substring(start));
            return sb.toString();
        } else {
            return original;
        }
    }
New to GrepCode? Check out our FAQ X