⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 report.java

📁 Excel Report是一款基于Excel的报表生成工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		                    resetParam = true;
		                }
		            }
	            }
            }
            //如果更改过数据源参数,则需从新检索数据.
            if(resetParam){
                ds.queryData();
            }
        }else{
            log.error("Error Can't find any datasource named ("  
                    + groupSourceName + ")");
        }
        return ds;
    }
    
    private void writeValue(WritableCell wc, Map dataSource){
        //从单元格的模板中取出对应的DataSource名和字段名
        String value = getDataFromDataSource(dataSource, wc.getContents());
        if(null != value /*&& !"".equals(value)*/){
	        //将值写入报表
	        Label lab = (Label)wc;
	        lab.setString(value);
        }
    }
    
    private void writeValue(WritableCell wc, IDataSource ds){
        //从单元格的模板中取出对应的DataSource名和字段名
        String templet = wc.getContents();
        String temp = templet.substring(1, templet.length()-1);
        String value = "";
        int index = 0;
        
        index = temp.indexOf(".");
        if(-1 == index){
            log.warn("Fail to get the field's value! field=" + 
                    templet);
            return;
        }
        
        String field = temp.substring(temp.indexOf(".") + 1);
        value = ds.getString(field);
        log.debug("templet:" + templet + "  value=" + 
                value);
        
        if(null != value && !"".equals(value)){
	        //将值写入报表
	        Label lab = (Label)wc;
	        lab.setString(value);
        }
    }
    
    /*
     * 从传入变量模板中取出模板对应字段DataSource中的值.
     */
    private String getDataFromDataSource(Map dataSource, String templet){
        String temp = templet.substring(1, templet.length()-1);
        String value = "", dsName = "";
        StringBuffer bufTemp = new StringBuffer(temp);
        StringBuffer bufValue = new StringBuffer();
        
        if(splitString(bufTemp,bufValue,".")){
	        dsName = bufTemp.toString();
	        value = bufValue.toString();
	        log.debug(" write value(" + temp + "): source="
	                + temp + " field=" + value);
	        //从数据源中取出数据.
	        IDataSource ds = (IDataSource)dataSource.get(dsName);
	        if(null != ds){
	            value = ds.getString(value);
	            log.debug("value=" + value);
	        }else{
	            log.error("Can't get the datasource named:" + dsName
	                    	+ " templet value is " + temp);
	        }
        }
        return value;
    }
    
    public boolean isGroupBegin(WritableCell wc){
        if(wc.getContents().startsWith(FLAG_GROUP_BEGIN)){
            return true;
        }else{
            return false;
        }
    }
    
    /*
     * 取得组信息.
     */
    public Group getGroup(WritableCell wc){
        Group group = new Group();
        group.setBeginPos(wc.getRow(), wc.getColumn());
        group.setGoupInfo(wc.getContents());
        return group;
    }
    
    /*
     * 判断当前单元格是否包含组结束内容.
     */
    public boolean isGroupEnd(WritableCell wc){
        if(wc.getContents().startsWith(FLAG_GROUP_END)){
            return true;
        }else{
            return false;
        }
    }
    
    /*
     * 判断当前单元格是否是指定组的组结束标记.
     */
    public boolean isGroupEnd(WritableCell wc, Group group){
        if(wc.getContents().indexOf(group.getName()) == -1){
            return false;
        }else{
            //保存组结束位置信息.
            group.setEndPos(wc.getRow(), wc.getColumn());
            return true;
        }
    }
    
    public boolean isVariable(WritableCell wc){
        String content = wc.getContents();
        if(content.startsWith("[") && content.endsWith("]")){
            return true;
        }else{
            return false;
        }
    }
    
    public boolean splitString(StringBuffer bufName, StringBuffer value, String flag){
        String temp = null;
        String name = bufName.toString();
        
        int index = name.indexOf(flag);
        if (index != -1) {
            temp = name.substring(0, index);
            bufName.delete(0, bufName.length());
            bufName.append(temp);
            
            temp = name.substring(index + 1);
            value.delete(0, value.length());
            value.append(temp);
            return true;
        }
        return false;
    }

    private ReportConfig reportConfig = null;
    private GroupManager groups = null; //保存模板组信息
    private Map dataSources = null; //保存所有数据源信息.
    private ReportEngine reportEngine = null;
    private Map params = null;

    private class GroupManager {
        public GroupManager() {
            index = 0;
            groups = new ArrayList();
        }

        public boolean add(Group groupInfo) {
            return groups.add(groupInfo);
        }

        /*
         * 取下一个组
         */
        public Group next() {
            if (index < groups.size()) {
                return (Group) groups.get(index++);
            } else {
                return null;//or throw Exception();//???
            }
        }

        /*
         * 是否还有欠套组,有则返回子欠套组,否则返回null
         */
        public Group hasNext() {
            if (index < groups.size()) {
                return (Group) groups.get(index);
            } else {
                return null;
            }
        }
        
        public ArrayList getAllGroups(){
            return groups;
        }
        
        public Group get(int curIndex){
            if (curIndex < groups.size()) {
                return (Group) groups.get(curIndex);
            } else {
                return null;
            }
        }

        /*
         * 取得嵌套组数量
         */
        public int size() {
            return groups.size();
        }
        /*
         * 返回当前组的所有子嵌套组,如果不存在子嵌套组则返回空
         */
        public GroupManager copySubGorous(){
            if(index < groups.size()){
                GroupManager newGroup = new GroupManager();
                for(int i=0; i<this.groups.size();i++){
                    newGroup.add((Group)this.groups.get(i));
                }
                return newGroup;
            }
            return null;
        }

        private ArrayList groups = null;
        private int index;
    }

    private class Group {
        private int beginRow, beginCol;
        private int endRow, endCol;
        
        public Group(){
            paramsMap = new HashMap();
            mergedCells = new ArrayList();
        }
        
        public boolean contain(int row, int col) {
            if ((row >= beginRow && row <= endRow)
                    /*&& (col >= beginCol && col <= endCol)*/) {
                return true;
            }
            return false;
        }

        public int getBeginCol() {
            return beginCol;
        }

        public int getBeginRow() {
            return beginRow;
        }

        public int getEndCol() {
            return endCol;
        }

        public int getEndRow() {
            return endRow;
        }
        
        public void setBeginPos(int beginRow, int beginCol) {
            this.beginRow = beginRow;
            this.beginCol = beginCol;
        }

        public void setEndPos(int endRow, int endCol) {
            this.endCol = endCol;
            this.endRow = endRow;
        }
        
        public String getDataSource(){
            return dataSourceName;
        }
        
        public String getName(){
            return name;
        }
        
        /*
         * 取得组内包含的合并单元格.
         */
        public Object[] getMergedCells(){
            return mergedCells.toArray();
        }
        
        /*
         * 插入一个合并单元格信息.
         */
        public void setMergedCell(Range range){
            mergedCells.add(range);
        }
        
        public void setGoupInfo(String groupInfo){
            //取得组名
            this.name = getInfo(groupInfo, REGEX_GROUP_NAME);
            this.name = this.name.substring(1, this.name.length() - 1);
            log.debug(" Sub group name: " 
                    + this.name);
            
            //取得组数据源配置信息.
            String groupDataSource = getInfo(groupInfo, REGEX_GROUP_DS_INFO);
            groupDataSource = groupDataSource.substring(1, groupDataSource.length() - 1);
            log.debug(" Sub group source : " 
                    + groupDataSource);
            
            //取得数据源名称 
            this.dataSourceName = getInfo(groupDataSource, REGEX_GROUP_DS_NAME);
            this.dataSourceName = this.dataSourceName.substring(3, this.dataSourceName.length() - 1);
            log.debug(" Sub group data source name: " 
                    + this.dataSourceName);
            
            //取得参数配置信息.
            String dsParamMap = getInfo(groupDataSource, REGEX_GROUP_DS_PARAM);
            dsParamMap = dsParamMap.substring(1, dsParamMap.length()-1);
            log.debug(" Sub group paramater config: " 
                    + dsParamMap);
            
            //取得goup中设置的参数映射信息.
            String [] dsParams = null/*dsParamMap.split(",")*/;
            StringBuffer value = new StringBuffer(), 
            			 name = new StringBuffer();
            for (int i = 0; i < dsParams.length; i++) {
                name.delete(0, name.length());
                name.append(dsParams[i]);
                if(splitString(name, value, "=")){
                    String paramName = name.toString();
                    int index = paramName.indexOf(".");
                    if(index > 0){
                        paramName = paramName.substring(index + 1);
                        paramsMap.put(paramName, value.toString());
                    }else{
                        log.error("Group.setGoupInfo() Error: Parameter's format must be (parametername.arg)!");
                    }
                }
            }
        }
        
        public Map getParamsMap(){
            return paramsMap;
        }
        
        public String getInfo(String msg, String searchRegex){
            Matcher matcher = Pattern.compile(searchRegex).matcher(msg);
            if(matcher.find()){
                //return matcher.toMatchResult().group();
                return msg.substring(matcher.start(), matcher.end());
            }
            return "";
        }
        
        private ArrayList mergedCells = null;
        private Map paramsMap = null;
        private String name = null;
        private String dataSourceName = null;
    }
    
    public static String FLAG_GROUP_BEGIN = "[band.group.begin.";
    public static String FLAG_GROUP_END = "[band.group.end.";
    public static String REGEX_GROUP_NAME = "\\.[^\\.]*\\{";
    
    public static String REGEX_GROUP_DS_INFO = "\\{.*\\}";
    public static String REGEX_GROUP_DS_NAME = "ds=.*\\(";
    public static String REGEX_GROUP_DS_PARAM = "\\(.*\\)";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -