• jxl导出Excel


    运用jxl导出Excel

    开发时出现的问题:

    1、尽量减少数据库的访问;

    2、注意一些细节问题(变量方法的命名问题等)

    jxl导出excel还是很简单的,设置表单样式,设置表头,插入数据即可。不再过多赘述

    public ActionForward exportBudgetList(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
            final int deptId = NumberUtils.toInt(request.getParameter("deptId"));
            final int feeTypeId = NumberUtils.toInt(request.getParameter("feeTypeId"));
            final int purposeId = NumberUtils.toInt(request.getParameter("purposeId"));
            int year=NumberUtils.toInt(request.getParameter("year"));
            final int projId =  NumberUtils.toInt(request.getParameter("projectId"));
            final String dayType = StringUtils.trimToEmpty(request.getParameter("feeDayTypeId"));
            
            FeeDayType feeTimeTypeEnum = null;
            
            if(StringUtils.isNotBlank(dayType)){
                feeTimeTypeEnum  = FeeDayType.valueOf(dayType);
            }
            String sheetName =year+"Budget";
            response.setHeader("Content-disposition", "attachment; filename=budgetList.xls");  
            response.setContentType("application/msexcel");
            ServletOutputStream out = response.getOutputStream();
            
            WritableSheet  sheet = null;
            
            WritableWorkbook wwb = null;
            wwb = Workbook.createWorkbook(out);
            
            sheet = wwb.createSheet(sheetName, 0);
            
            //创建表头样式
            WritableFont wfc = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);  
            WritableCellFormat headerFmt =  new WritableCellFormat(wfc);  
            headerFmt.setAlignment(Alignment.CENTRE);
            headerFmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            headerFmt.setBackground(Colour.BRIGHT_GREEN);        
            
            WritableCellFormat bodyFmt =  new WritableCellFormat(wfc);  
            bodyFmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            bodyFmt.setFont(new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK));
            
            
            //填写表头
            int colNum=0;
            int rowNum=0;
            
            String[] headers={"部门名称","费用类型","项目","用途","发生时间"};
            for(int i=0;i<headers.length;i++){
                sheet.addCell(new Label(colNum,rowNum,headers[i],headerFmt));
                sheet.setColumnView(colNum, 12);
                colNum++;
            }
            
            for(int mount=1;mount<13;mount++) {
                String label=mount+"月";
                sheet.addCell(new Label(colNum,rowNum,label,headerFmt));
                sheet.setColumnView(colNum, 12);
                colNum++;
            }
            
            List<DeptBudget> deptBudgetList = applyService.findDeptBudgets(deptId, feeTypeId, projId, purposeId, feeTimeTypeEnum, year);
            //插入数据
            if(deptBudgetList!=null && !deptBudgetList.isEmpty()){
                for(DeptBudget budget: deptBudgetList){
                    rowNum++;
                    sheet.setRowView(rowNum, 400);
                    int colIndex= 0;
                    sheet.addCell(new Label(colIndex++,rowNum,String.valueOf(deptService.getDepartmentById(budget.getDeptId()).getDeptName()),bodyFmt));
                    sheet.addCell(new Label(colIndex++,rowNum,String.valueOf(applyService.getFeeTypeById(budget.getFeeTypeId()).getTypeName()),bodyFmt));
                    
                    Project proj = applyService.getProjectById(budget.getProjId());
                    
                    if(proj==null){
                        sheet.addCell(new Label(colIndex++,rowNum,StringUtils.EMPTY,bodyFmt));
                    }else{
                        sheet.addCell(new Label(colIndex++,rowNum,String.valueOf(proj.getPrjtName()),bodyFmt));
                    }
                    
                    FeePurpose purpose = applyService.getFeePurposeById(budget.getPurposeId());
                    if(purpose ==null){
                        sheet.addCell(new Label(colIndex++,rowNum,StringUtils.EMPTY,bodyFmt));
                    }else{
                        sheet.addCell(new Label(colIndex++,rowNum,String.valueOf(purpose.getFeePurposeName()),bodyFmt));
                    }
                    
                    if(budget.getDayType()==null){
                        sheet.addCell(new Label(colIndex++,rowNum,StringUtils.EMPTY,bodyFmt));
                    }else{
                        sheet.addCell(new Label(colIndex++,rowNum,String.valueOf(budget.getDayType().getStateName()),bodyFmt));
                    }                
                    
                    
                    //填写预算
                     List<MonthBudget>  mbList = budget.getBudgetList();
                     if(mbList!=null && !mbList.isEmpty()) {
                         int startColNum  = headers.length;
                         for(MonthBudget  mBudget : mbList) {
                             double amt  =  NumberUtils.toDouble(CustomFunctions.renderSalaryAmount(mBudget.getBudget()));
                             sheet.addCell(new Label(startColNum++,rowNum,String.valueOf(amt),bodyFmt));
                         }
                     }
                }
            }
            wwb.write();
            wwb.close();        
            return null;
        }

  • 相关阅读:
    vimrc之fileformat
    std::copy ( myvector.begin(), myvector.end(), out_it )
    backtrace、backtrace_symbols
    mysql之replicate_do_table/replicate_ingore_table/replicate_wide_do_table/replicate_wide_ingore_table
    symbol lookup error
    mysql之select into outfile
    flex与bison
    运行maven打出来的jar包报错:Unable to locate Spring NamespaceHandler for XML schema namespace
    让maven生成可运行jar包
    windows下通过Git Bash使用Git常用命令
  • 原文地址:https://www.cnblogs.com/wyhailjn/p/3580031.html
Copyright © 2020-2023  润新知