joget 数据导出插件

在joget流程中,根据客户化sql,将表数据导出为csv文件

思路 :执行sql 遍历rs,使用io流将数据写入到csv文件 (好简单好不啦)

核心代码

遍历rs 将数据保存到List> 中

//将sql中的字段提取出来
            String[] colums = CommUtil.getColums(sql, conn);
            List list = new ArrayList();
            //将数据保存在 list 中
            while (rs.next()){
                Map data = new HashMap();
                for (int i = 0; i < colums.length; i++) {
                    String colum = colums[i];
                    Object o =  rs.getObject(i+1);
                    String value = null;
                    if(o != null){
                        value = o.toString();
                    }else{
                        value = "\t";
                    }
                    data.put(colum,value);
                }
                list.add(data);
            }

其中CommUtil中的代码如下

public static String[] getColums(String sql, Connection conn){
        sql  = sql.trim().toLowerCase();
        //获取select 和from 之间的内容
        String str = sql.substring(sql.indexOf("select")+7,sql.indexOf("from")).trim();
        if(!str.equals("*")){
            String arr [] = str.split(",");
            for (int i = 0; i-1){
                   s = s.substring(s.indexOf("as")+3);
                   s = s.replace("\'","").trim();
                   s = s.replace("\"","").trim();
                   arr[i] = s;
                   continue;
                }
                if(s.indexOf(".")>-1){
                    arr[i] = s.split("[.]")[1];
                }
            }
            return arr;
        }else{
            str = sql.substring(sql.indexOf("from")+5).trim()+" ";

            String tableName = str.substring(0,str.indexOf(" "));
            sql = "SHOW FULL COLUMNS FROM "+ tableName;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                List list = new ArrayList();
                while (rs.next()){
                    String colum = (String) rs.getObject(1);
                    list.add(colum);
                }
                String arr [] = new String[list.size()];
                for (int i = 0; i 

核心导出代码如下

public static boolean exportWithSeprator(String fileType, String filePath, List list,String[] colums,String seprator){
        //保存列名
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i  map = list.get(i);
            for (int j = 0; j

你可能感兴趣的:(joget 数据导出插件)