HSSFWorkbook导出EXCEL

来源:互联网 发布:剑网3dbm数据 编辑:程序博客网 时间:2024/05/21 14:01
/**
     * @description: 导出工时EXCEL
     * @author yunhui.tang
     * @param request
     * @param response
     * @param pager
     * @date 2016-5-16 上午11:13:53
     */
    @RequestMapping(params = "method=exportWorkExcel", method = RequestMethod.GET)
    public void exportWorkExcel(HttpServletRequest request, HttpServletResponse response, Pager pager) {
        String selMoldNo = RequestUtil.getParameter(request, "selMoldNo");
        String queryKey = RequestUtil.getParameter(request, "queryKey");
        String startDate = request.getParameter("startDate"); // 工艺编制时间
        String endDate = request.getParameter("endDate");
        List<CommSection> commSections = commSectionManager.findToolingSectionOrderby(); // 统计工段
        String drawingType = request.getParameter("drawingType");//图纸类型
        String selWorkOrderNo = request.getParameter("selWorkOrderNo");
        
        String moldSortKey = request.getParameter("moldSortKey");// 模具类型
        String esi = request.getParameter("esi");// 预估
        String real = request.getParameter("real");// 实际
        String onlyFinished = request.getParameter("onlyFinished");// 只显示完成
        
        if(StringUtil.isNullOrEmpty(real)&&StringUtil.isNullOrEmpty(esi)){
            real = "on";
            esi = "on";
        }
        
        Map<Object,Object> queryMap = new HashMap<Object, Object>();
        queryMap.put("startDate", startDate);
        queryMap.put("endDate", endDate);
        queryMap.put("selMoldNo", selMoldNo);
        queryMap.put("selWorkOrderNo", selWorkOrderNo);
        queryMap.put("queryKey", queryKey);
        queryMap.put("drawingType", drawingType);
        queryMap.put("moldSortKey", moldSortKey);
        queryMap.put("esi", esi);
        queryMap.put("real", real);
        queryMap.put("onlyFinished", onlyFinished);
        queryMap.put("onlyFinished", onlyFinished);
        queryMap.put("manHour", false);
        

        List<SchtCostVo> schtCostVos = schtCostManager.querySchtCostVos(null, commSections, queryMap);
        
        try {

            HSSFWorkbook workBook = new HSSFWorkbook();
            HSSFSheet sheet = workBook.createSheet();// 創建表格
            sheet.createFreezePane(0, 1, 0, 1);// 冻结第一行
            sheet.setColumnWidth(17, 20 * 256); // 设置 列宽度
            sheet.setColumnWidth(18, 20 * 256);
            HSSFRow row2 = sheet.createRow(0); // 表格列

            row2.setHeight((short) 500);// 列高
            HSSFCellStyle cellStyle = HSSFWorkbookUtil.getStyle(workBook); // 数据样式
            HSSFCellStyle titleStyle = HSSFWorkbookUtil.getTitleStyle(workBook);// 表头样式

            // 表头数据
            HSSFCell cell = row2.createCell(0); // 第一行
            cell.setCellStyle(titleStyle); //设置 表头样式
            cell.setCellValue("序号");
            
            HSSFCellStyle redStyle = HSSFWorkbookUtil.getSchStyle(workBook, HSSFColor.RED.index);

            cell = row2.createCell(1);
            cell.setCellStyle(titleStyle);
            cell.setCellValue("监控号");
            
            cell = row2.createCell(2);
            cell.setCellStyle(titleStyle);
            cell.setCellValue("模具类型");

            cell = row2.createCell(3);
            cell.setCellStyle(titleStyle);
            cell.setCellValue("模号/型号");

            
            cell = row2.createCell(27);
            cell.setCellStyle(titleStyle);
            cell.setCellValue("工时(分钟)");

            String sectionName = "";
            for (int i = 0; i < commSections.size(); i++) {
                sectionName = commSections.get(i).getSectionChName();
                cell = row2.createCell(28 + i);
                cell.setCellStyle(titleStyle);
                cell.setCellValue(sectionName);
            }

            /*
             * if (biSchtCosts.size() < 0) { // 合併單元格(起始行号,终止行号, 起始列号,终止列号) sheet.addMergedRegion(new CellRangeAddress(0, 0, 9, 9)); }
             */
            // 设置表体数据

            int mergeNum = 1; // 合并的单元格 行数
            
            for (int i = 0; i < schtCostVos.size(); i++) {

                for (int j = 0; j < 2; j++) {

                    SchtCostVo schtCostVo = schtCostVos.get(i);
                    BomParts bomParts = schtCostVo.getBomparts();
                    LinkedHashMap<Long, String> sectionAndRealTimeMap = (LinkedHashMap<Long, String>) schtCostVo.getSectionAndRealTimeMap();
                    LinkedHashMap<Long, String> sectionAndEsiTimeMap = (LinkedHashMap<Long, String>) schtCostVo.getSectionAndEsiTimeMap();

                    HSSFRow row3 = null;
                    try{
                        row3 = sheet.createRow(mergeNum); // 第N行,由於表頭一行,從起
                    }catch(IllegalArgumentException ex){
                        throw new BusinessException("EXCEL 导出数据量太大,请导出2万条以内");
                    }
                    

                    HSSFCell cell3 = row3.createCell(0);// 表数据对象
                    cell3.setCellValue(i + 1); // 数据值
                    cell3.setCellStyle(cellStyle); // 数据样式

                    cell3 = row3.createCell(1);
                    if (StringUtils.isNotBlank(bomParts.getPartNo())&&BiUtil.checkNumber(bomParts.getPartNo())){
                        //cell3.setCellValue(new BigDecimal(bomParts.getPartNo()).longValue());
                        cell3.setCellValue(new BigDecimal(schtCostVo.getWpId()).longValue());
                    }else{
                        cell3.setCellValue(bomParts.getPartNo());
                    }
                    if(bomParts.getValidStatus() != 1L){
                        cell3.setCellStyle(redStyle);
                    }else{
                        cell3.setCellStyle(cellStyle);
                    }
                        
                    cell3 = row3.createCell(2); // 模具类型
                    cell3.setCellValue(schtCostVo.getMoldType());
                    cell3.setCellStyle(cellStyle);

                    cell3 = row3.createCell(3); // 列
                    cell3.setCellValue(schtCostVo.getBomparts().getMoldNo());
                    cell3.setCellStyle(cellStyle);

                    
                    // 下单部门
                    cell3 = row3.createCell(17);
                    String customerName = bomParts.getPmTerminalCustomerInfo().getTerminalCustomerName();
                    if (StringUtils.isNotBlank(customerName)&&BiUtil.checkNumber(customerName)){
                        cell3.setCellValue(new BigDecimal(customerName).longValue());
                    }else{
                        cell3.setCellValue(customerName);
                    }
                    cell3.setCellStyle(cellStyle);
                    

                    if (j == 0) {
                        cell3 = row3.createCell(27);
                        cell3.setCellValue("预估");
                        cell3.setCellStyle(cellStyle);

                        for (int k = 0; k < commSections.size(); k++) {
                            cell3 = row3.createCell(28 + k);
                            
                            String esiTime   = sectionAndEsiTimeMap.get(commSections.get(k).getSectionId());
                            if (StringUtils.isNotBlank(esiTime)&&BiUtil.checkNumber(esiTime)){
                                cell3.setCellValue(new BigDecimal(esiTime).doubleValue());
                            }else{
                                cell3.setCellValue(esiTime);
                            }
                            cell3.setCellStyle(cellStyle);
                        }

                    } else {
                        cell3 = row3.createCell(27);
                        cell3.setCellValue("实际");
                        cell3.setCellStyle(cellStyle);

                        for (int k = 0; k < commSections.size(); k++) {
                            cell3 = row3.createCell(28 + k);
                            String realTime = sectionAndRealTimeMap.get(commSections.get(k).getSectionId());
                            if (StringUtils.isNotBlank(realTime)&&BiUtil.checkNumber(realTime)){
                                cell3.setCellValue(new BigDecimal(realTime).doubleValue());
                            }else{
                                cell3.setCellValue(realTime);
                            }
                            cell3.setCellStyle(cellStyle);
                        }
                    }

                    mergeNum++;

                }

                // 合并单元格
                sheet.addMergedRegion(new CellRangeAddress(mergeNum - 2, mergeNum - 1, 0, 0));
                sheet.addMergedRegion(new CellRangeAddress(mergeNum - 2, mergeNum - 1, 1, 1));
                sheet.addMergedRegion(new CellRangeAddress(mergeNum - 2, mergeNum - 1, 2, 2));
                sheet.addMergedRegion(new CellRangeAddress(mergeNum - 2, mergeNum - 1, 3, 3));            

            }

            String downloadFileName = new String("工时统计清单.xls".getBytes("gb2312"), "iso8859-1");

            //long b = System.currentTimeMillis();
            //System.out.println("t:"+ (b-a));
            
            response.setCharacterEncoding("utf8");
            response.setHeader("Connection", "close");
            response.setHeader("Context-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFileName);
            OutputStream fout = response.getOutputStream();
            //  FileOutputStream fileOut=new FileOutputStream("工装成本统计.xls");
            try{
                workBook.write(fout);
            }catch(Exception e){
                throw new BusinessException("EXCEL 导出数据量太大,请导出2万条以内");
            }
            
            fout.flush();
            fout.close();

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("系统错误", e);
            throw new BusinessException(e);
        }
    }
0 0
原创粉丝点击