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

📄 modeltoexcelimpl.java

📁 J2EE项目开发Excel数据导入导出操作组件源代码(附带说明文档)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
             * 在执行Workbook.getWorkbook(this.excelFile); 时报错。
             * 所以设立 isNewFileFlag = false;
             *              
             */
            boolean isNewFileFlag = false;
            if (this.excelFile!=null&&!excelFile.exists()) {
                this.excelFile.createNewFile();
                isNewFileFlag = true;
            }
            if (this.dynamicTitleMap == null) {
                System.out.println("Error:未设置动态列!");
                return null;
            }
            WritableWorkbook wbook;
            if(this.excelFile!=null){
                if(isNewFileFlag){
                    wbook = Workbook.createWorkbook(this.excelFile);
                }else{
                    Workbook book = Workbook.getWorkbook(this.excelFile);
                    wbook = Workbook.createWorkbook(this.excelFile,book);  
                }
            }else{                
                wbook = Workbook.createWorkbook(this.excelOutputStream);
            }
            
            if(wbook.getSheets().length > this.sheetNum){
                wbook.removeSheet(this.sheetNum);
            }   
            WritableSheet wsheet = wbook.createSheet(this.sheetName, this.sheetNum);
            System.out.println(this.sheetName + this.sheetNum);
            
            int columns = this.dynamicTitleMap.size();
            // 加上表头2行,标题1行
            int rows = modelList.size() + 3;
            System.out.println("columns = " + columns);
            
            
            
            // 设置Excel表头
            wsheet.mergeCells(0, 0, columns - 1, 1);
            Label cellHeader = new Label(0, 0, this.getHeader(), this.getHeaderFormat());
            wsheet.addCell(cellHeader);

            if (intHeight > 0) {
                wsheet.setRowView(0, intHeight);
                wsheet.setRowView(1, intHeight);
            }

            // String[] propertyName = new String[columns];
            String[] propertyDataType = new String[columns];
            for (int j = 0; j < columns; j++) {
                RuturnPropertyParam propertyBean = (RuturnPropertyParam) excelConfig.getColumnMap().get(String.valueOf(j + 1));
                // 每列 Model中对应的属性                
                propertyDataType[j] = propertyBean.getDataType();
            }
            
            
            String[][] title = this.getDynamicTitle();
            String[] propertyName = title[0];
            String[] columnWidth = title[1];
            if (StringUtils.isBlank(propertyName[propertyName.length - 1])) {
                System.out.println("ERROR : 动态表头属性值未在配制文件中配制完全!");
            }
            
            // 设置Excel标题
            for (int j = 0; j < propertyName.length; j++) {
                String excelTitleName = (String) this.dynamicTitleMap.get(propertyName[j]);
                Label cellTitle = new Label(j, 2, excelTitleName, this.getTitleFormat());
                wsheet.addCell(cellTitle);
                if (StringUtils.isNotBlank(columnWidth[j])) {
                    wsheet.setColumnView(j, Integer.parseInt(columnWidth[j]));
                }
            }
            if (intHeight > 0) {
                wsheet.setRowView(2, intHeight);
            }

//          判断当前 list 存储的是 from 还是 Map
            
            boolean isMap = false;
            if (modelList.size()>0) {
                Object tmpObj = modelList.get(0);
                if(tmpObj instanceof Map){
                    isMap = true;
                }
            }
            for (int i = 3; i < rows; i++) {
//              BeanWrapper bw = new BeanWrapperImpl(modelList.get(i - 3));
                Object modelObj = modelList.get(i - 3);
                BeanWrapper bw = null;
                if(isMap!=true){   
                     bw= new BeanWrapperImpl(modelObj);
                }
                for (int j = 0; j < columns; j++) {
                    
                    String strCell = "";
//                    Object obj = null;
//                    if (StringUtils.isNotBlank(propertyName[j])) {
//                        obj = bw.getPropertyValue(propertyName[j]);
//                    }
                    Object obj;
                    if(isMap ==true){
                        obj =((Map) modelObj).get(propertyName[j]);
                    }else{
                         obj= bw.getPropertyValue(propertyName[j]);
                    }  
                    if (obj != null) {
                        strCell = obj.toString().trim();
                    }

                    Label cellNormol = new Label(j, i, strCell, this.getNormolFormat());
                    //wsheet.addCell(cellNormol);
                    if(NumberUtils.isNumber(strCell)&&(propertyDataType[j].indexOf("Integer")>=0)){
                        Number ncellNormol;
                        if(strCell.indexOf(".")>=0){                        	
                            ncellNormol = new Number(this.startColumn + j, i, Double.parseDouble(strCell), this.getNormolFormat()); 
                        }else{
                            ncellNormol = new Number(this.startColumn + j, i, Long.parseLong(strCell), this.getNormolFormat());                            
                        }   
                        wsheet.addCell(ncellNormol);
                    }else{
                        wsheet.addCell(cellNormol);
                    }  
                }
                if (intHeight > 0) {
                    wsheet.setRowView(i, intHeight);
                }
            }
            wbook.write();
            wbook.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BiffException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return excelFile;
    }

    public File getExcelfileByConfig() {
        // TODO Auto-generated method stub

        try {
            
            /*
             * 在支持多页的过程中,对于每个工作薄不能每次都要从新建立 Workbook.createWorkbook(this.excelFile);
             * 要从传进入的文件的基础上进行修改
             * Workbook book = Workbook.getWorkbook(this.excelFile);  
             * wbook = Workbook.createWorkbook(this.excelFile,book); 
             * 但是对于刚刚建立的文件,this.excelFile.createNewFile();
             * 在执行Workbook.getWorkbook(this.excelFile); 时报错。
             * 所以设立 isNewFileFlag = false;
             *              
             */
            
            boolean isNewFileFlag = false;
            if (this.excelFile!=null&&!excelFile.exists()) {
                this.excelFile.createNewFile(); 
                isNewFileFlag = true;
            }

            //WritableWorkbook wbook = Workbook.createWorkbook(this.excelFile);
            WritableWorkbook wbook;
            if(this.excelFile!=null){
                if(isNewFileFlag){
                    wbook = Workbook.createWorkbook(this.excelFile);
                }else{
                    Workbook book = Workbook.getWorkbook(this.excelFile);                
                    wbook = Workbook.createWorkbook(this.excelFile,book);  
                }
                              
            }else{                
                wbook = Workbook.createWorkbook(this.excelOutputStream);
            }
            
            if(wbook.getSheets().length > this.sheetNum){
                wbook.removeSheet(this.sheetNum);
            }            
            WritableSheet wsheet = wbook.createSheet(this.sheetName, this.sheetNum);
            //System.out.println(this.sheetName + this.sheetNum);
            
            
            int columns = excelConfig.getColumnMap().size();
            // 加上表头2行,标题1行
            int rows = modelList.size() + 3;
            System.out.println("columns = " + columns);
            // 设置Excel表头
            wsheet.mergeCells(0, 0, columns - 1, 1);
            Label cellHeader = new Label(0, 0, this.getHeader(), this.getHeaderFormat());
            wsheet.addCell(cellHeader);

            if (intHeight > 0) {
                wsheet.setRowView(0, intHeight);
                wsheet.setRowView(1, intHeight);
            }

            String[] propertyName = new String[columns];
            String[] propertyDataType = new String[columns];
            // 设置Excel标题
            for (int j = 0; j < columns; j++) {
                RuturnPropertyParam propertyBean = (RuturnPropertyParam) excelConfig.getColumnMap().get(String.valueOf(j + 1));
                String excelTitleName = propertyBean.getExcelTitleName();
                String name = propertyBean.getName().trim();

                // 如果动态表头含有记录属性值,取动态传入的表头的值,但是传入的值不能为空,如果为空,取配制文件的值
                if (this.dynamicTitleMap.containsKey(name)) {
                    String strTitle = (String) this.dynamicTitleMap.get(name);
                    if (StringUtils.isNotBlank(strTitle)) {
                        excelTitleName = strTitle;
                    }
                }
                Label cellTitle = new Label(j, 2, excelTitleName, this.getTitleFormat());
                wsheet.addCell(cellTitle);
                // 每列 Model中对应的属性
                propertyName[j] = propertyBean.getName();
                propertyDataType[j] = propertyBean.getDataType();
                // 设置每列的宽度
                if (StringUtils.isNotBlank(propertyBean.getColumnWidth())) {
                    wsheet.setColumnView(j, Integer.parseInt(propertyBean.getColumnWidth()));
                }
            }

            if (intHeight > 0) {
                wsheet.setRowView(2, intHeight);
            }
//          判断当前 list 存储的是 from 还是 Map
            
            boolean isMap = false;
            if (modelList.size()>0) {
                Object tmpObj = modelList.get(0);
                if(tmpObj instanceof Map){
                    isMap = true;
                }
            }
            for (int i = 3; i < rows; i++) {
                //BeanWrapper bw = new BeanWrapperImpl(modelList.get(i - 3));
                Object modelObj = modelList.get(i - 3);
                BeanWrapper bw = null;
                if(isMap!=true){   
                     bw= new BeanWrapperImpl(modelObj);
                }
                for (int j = 0; j < columns; j++) {
                    
                    String strCell = "";
                    //Object obj = bw.getPropertyValue(propertyName[j]);
                    Object obj;
                    if(isMap ==true){
                        obj =((Map) modelObj).get(propertyName[j]);
                    }else{
                         obj= bw.getPropertyValue(propertyName[j]);
                    }  
                    if (obj != null) {
                        strCell = obj.toString().trim();
                    }
                    Label cellNormol = new Label(j, i, strCell, this.getNormolFormat());
                    //wsheet.addCell(cellNormol);
                    if(NumberUtils.isNumber(strCell)&&(propertyDataType[j].indexOf("Integer")>=0)){
                        Number ncellNormol;
                        if(strCell.indexOf(".")>=0){                        	
                            ncellNormol = new Number(this.startColumn + j, i, Double.parseDouble(strCell), this.getNormolFormat()); 
                        }else{
                            ncellNormol = new Number(this.startColumn + j, i, Long.parseLong(strCell), this.getNormolFormat());                            
                        }   
                        wsheet.addCell(ncellNormol);
                    }else{
                        wsheet.addCell(cellNormol);
                    }  
                }
                if (intHeight > 0) {
                    wsheet.setRowView(i, intHeight);
                }
            }
            wbook.write();

⌨️ 快捷键说明

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