qq_37985878
qq_37985878
2017-08-24 02:07

java用jacob生成word文件

10
  • jacob

在生成word文件的时候,单个可以生成,批量生成的时候,就会报错表格需要的集合不存在,我的for循环是正常的。

    public ActionForward exportWord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

            //这部分主要是获取前台数据
            String bpId = request.getParameter("bpId") == null ? "" : request.getParameter("bpId");
            String[] ids = request.getParameterValues("radio");//获取要申请id
            String zt = "仿宋_GB2312";
            request.getSession().setAttribute("proName", false);
            StringBuffer qiId = new StringBuffer();
            try {
                qiId.append(" from QualityInfo where 1=1");
                qiId.append(" and id in ( ");
                // 查询施工人员数据
                for(int i = 0 ;i<ids.length ; i++){
                    String workid = ids[i];
                    if( i != ids.length -1){
                        qiId.append("'"+workid+"' , ");
                    }else{
                        qiId.append("'"+workid+"' )");
                    }
                }
                //这是生成前八行需要的数据,后边的行数是动态生成的
                List<QualityInfo> qualityInfo = (List<QualityInfo> )commonService.find(qiId.toString());//获取检修计划单个信息


                // 从这里开始创建表格
                WordOperator wo = new WordOperator(false);
                wo.createNewDocument();
                wo.setParagraphsProperties(1, 0, 0, 0, 0);//设置段落格式,不设置 自动左对齐,现在居中对齐。
                wo.setFont(true, false, false, null, "16", zt);// 红1,0,0,0白16,77,72,15,这是设置字体样式的。
                wo.insertText("泉州石化检修质量验收记录表");
                wo.nextLine();
                wo.moveDown(1);


                //获取每个表的设备名称
                //获取生产运行部
                List<Department> dyList = (List<Department>)PublicTools.getAllDYList();
                String dyName="";

                //获取装置
                List<Department> zzList = (List<Department>)PublicTools.getAllZz();
                String zzName="";

                //开始往word表格内填写数据
                if(qualityInfo != null && qualityInfo.size()!=0){
                    for(int i=0;i<qualityInfo.size();i++){
                        //获取每个表格的检查标准个数,为自动生成列做准备
                        String countSql="select count(id) cou from psjx_qualityinspectionplan where qfid='"+qualityInfo.get(i).getId()+"'";
                        List countList = commonService.getJDBC(countSql);
                        Map countMap = (Map)countList.get(0);
                        String countQl = countMap.get("cou").toString();
                        int countTotal = Integer.parseInt(countQl);

                        String deviceName=PublicTool.deviceType(qualityInfo.get(i).getDeviceName());
                        if(zzList!=null && zzList.size()!=0){
                            for(int d=0;d<zzList.size();d++){
                                if(zzList.get(d).getDepartmentId().equals(qualityInfo.get(i).getProcessUnits())){
                                    zzName=zzList.get(d).getDepartmentName();
                                }
                            }
                        }
                        if(dyList!=null && dyList.size()!=0){
                            for(int d=0;d<dyList.size();d++){
                                if(dyList.get(d).getDepartmentId().equals(qualityInfo.get(i).getBuildingUser())){
                                    dyName=dyList.get(d).getDepartmentName();
                                }
                            }
                        }

                        //表格
                        //第一行
                        wo.setFont(false, false, false, null, "10", zt);//设置当前选定内容的字体,设置表格内的文字样式
                        wo.createTable("table" + i + 1, 12, 8 +(countTotal));//创建表格,位置,列数,行数
                        wo.mergeCell(i+1, 1, 1, 1, 2);//动态生成的时候,每次到这里就会报错。
                        wo.mergeCell(i+1, 1, 2, 1, 11);
                        wo.putTxtToCell(i+1, 1, 2, "中化泉州石化有限公司管理体系");
                        wo.moveDown(1);//换行

                        //第二行
                        wo.mergeCell(i+1, 2, 1, 2, 2);
                        wo.mergeCell(i+1, 2, 1, 1, 1);
                        wo.mergeCell(i+1, 2, 2, 2, 11);
                        wo.putTxtToCell(i+1, 2, 2, deviceName+"质量验收记录表");
                        wo.moveDown(1);//换行

                        //第三行
                        wo.mergeCell(i+1, 3, 1, 3, 2);//第三行的第一列和第二列合并,
                        wo.mergeCell(i+1, 3, 1, 1, 1);
                        wo.putTxtToCell(i+1, 3, 2, "记录编号");//第三行的第二列写入
                        wo.mergeCell(i+1, 3, 3, 3, 5);//第三行的第三列到第五列合并
                        wo.putTxtToCell(i+1, 3, 3, qualityInfo.get(i).getRecordNumber()== null?"":qualityInfo.get(i).getRecordNumber());//第三行的第三列写入
                        wo.mergeCell(i+1, 3, 4, 3, 5);
                        wo.putTxtToCell(i+1, 3, 4, "使用单位");
                        wo.mergeCell(i+1, 3, 5, 3, 8);
                        wo.putTxtToCell(i+1, 3, 5, dyName);
                        wo.moveDown(1);//换行

                        //第四行
                        wo.mergeCell(i+1, 4, 1, 4, 2);
                        wo.putTxtToCell(i+1, 4, 1, "生产装置");
                        wo.putTxtToCell(i+1, 4, 2, zzName);
                        wo.putTxtToCell(i+1, 4, 3, "设备位号/名称");
                        wo.mergeCell(i+1, 4, 4, 4, 7);
                        wo.putTxtToCell(i+1, 4, 4, qualityInfo.get(i).getSbmcs() == null ? "" :qualityInfo.get(i).getSbmcs() );
                        wo.mergeCell(i+1, 4, 5, 4, 6);
                        wo.putTxtToCell(i+1, 4, 5, "设备标识");
                        wo.mergeCell(i+1, 4, 6, 4, 7);
                        wo.putTxtToCell(i+1, 4, 6, PublicTool.deviceType(qualityInfo.get(i).getDeviceNumber() == null?"":qualityInfo.get(i).getDeviceNumber()));
                        wo.moveDown(1);//换行

                        //第五行
                        wo.mergeCell(i+1, 5, 1, 5, 2);
                        wo.putTxtToCell(i+1, 5, 1, "检修内容简述");
                        wo.mergeCell(i+1, 5, 2, 5, 7);
                        wo.putTxtToCell(i+1, 5, 2, qualityInfo.get(i).getSketch() == null ? "": qualityInfo.get(i).getSketch());
                        wo.mergeCell(i+1, 5, 3, 5, 4);
                        wo.putTxtToCell(i+1, 5, 3, "检修类别");
                        wo.mergeCell(i+1, 5, 4, 5, 5);
                        wo.putTxtToCell(i+1, 5, 4, PublicTool.deviceType(qualityInfo.get(i).getClassification() == null ? "" : qualityInfo.get(i).getClassification()));
                        wo.moveDown(1);//换行
                        //第六行
                        wo.mergeCell(i+1, 6, 1, 6, 2);
                        wo.putTxtToCell(i+1, 6, 1, "检修通知单号");
                        wo.putTxtToCell(i+1, 6, 2, qualityInfo.get(i).getInform() == null ?"": qualityInfo.get(i).getInform());
                        wo.putTxtToCell(i+1, 6, 3, "检修工单号");
                        wo.mergeCell(i+1, 6, 4, 6, 5);
                        wo.putTxtToCell(i+1, 6, 4, qualityInfo.get(i).getWorkNumber() == null ?"": qualityInfo.get(i).getWorkNumber());
                        wo.mergeCell(i+1, 6, 5, 6, 7);
                        wo.putTxtToCell(i+1, 6, 5, "检修开始结束时间");
                        wo.mergeCell(i+1, 6, 6, 6, 8);
                        wo.putTxtToCell(i+1, 6, 6, DateUtil.getDate(DateUtil.getDateFromTs( qualityInfo.get(i).getBeginTime()))+"~"+DateUtil.getDate(DateUtil.getDateFromTs( qualityInfo.get(i).getEndTime())));
                        wo.moveDown(1);//换行

                        //第七行
                        wo.putTxtToCell(i+1, 7, 1, "序号");
                        wo.putTxtToCell(i+1, 7, 2, "检查项目");
                        wo.mergeCell(i+1, 7, 3, 7, 4);
                        wo.putTxtToCell(i+1, 7, 3, "检查标准");
                        wo.putTxtToCell(i+1, 7, 4, "控制级别");
                        wo.putTxtToCell(i+1, 7, 5, "检查结果");
                        wo.mergeCell(i+1, 7, 6, 7, 9);
                        wo.putTxtToCell(i+1, 7, 6, "检查确认人");
                        wo.putTxtToCell(i+1, 7, 7, "检查时间");
                        wo.putTxtToCell(i+1, 7, 8, "备注");
                        wo.moveDown(1);//换行

                        //第八行
                        wo.mergeCell(i+1, 8, 1, 7, 1);
                        wo.mergeCell(i+1, 8, 2, 7, 2);
                        wo.mergeCell(i+1, 8, 3, 8, 4);
                        wo.mergeCell(i+1, 8, 3, 7, 3);
                        wo.mergeCell(i+1, 8, 4, 7, 4);
                        wo.mergeCell(i+1, 8, 5, 7, 5);
                        wo.putTxtToCell(i+1, 8, 6, "施工单位");
                        wo.mergeCell(i+1, 8, 7, 8, 8);
                        wo.putTxtToCell(i+1, 8, 7, "生产单元");
                        wo.putTxtToCell(i+1, 8, 8, "设备工程部");
                        wo.mergeCell(i+1, 8, 9, 7, 7);
                        wo.mergeCell(i+1, 8, 10, 7, 8);
                        wo.moveDown(1);//换行



                        //这里是自动生成的行
                        List Kzjblist = SysCodeConstant.getCodeList(SysCodeConstant.SBZLJCKZJB);//控制级别
                        String kzjb="";
                        List<QualityInspectionPlan> QualityInspectionPlanList = (List<QualityInspectionPlan>)commonService.find(" from QualityInspectionPlan where qfId = '"+qualityInfo.get(i).getId()+"' order by inspectionItem");//获取质量检修标准id集合
                        List<String> qaaIdList= new ArrayList<String>();
                        if(QualityInspectionPlanList!= null && QualityInspectionPlanList.size()!=0){
                            for(int q=0; q<QualityInspectionPlanList.size();q++){
                                wo.putTxtToCell(i+1, 8+q+1, 1, q+1+"");
                                wo.putTxtToCell(i+1, 8+q+1, 2, QualityInspectionPlanList.get(q).getInspectionItem()==null ? "":QualityInspectionPlanList.get(q).getInspectionItem() );
                                wo.mergeCell(i+1, 8+q+1,3 , 8+q+1, 4);
                                wo.putTxtToCell(i+1, 8+q+1, 3, QualityInspectionPlanList.get(q).getInspectionStandard()==null?"":QualityInspectionPlanList.get(q).getInspectionStandard());
                                 if( Kzjblist!=null && Kzjblist.size()>0 ){
                                        for(int j=0;j<Kzjblist.size();j++){
                                            Map code=(Map)Kzjblist.get(j);
                                            String name  = code.get("name").toString();
                                            String value = code.get("value").toString();
                                            if(QualityInspectionPlanList.get(q).getControlHierarchy() != null){
                                                if(QualityInspectionPlanList.get(q).getControlHierarchy().equals(value)){
                                                    kzjb = name;
                                                }
                                            }

                                        }
                                    }
                                 wo.putTxtToCell(i+1, 8+q+1, 4, kzjb);
                                 wo.putTxtToCell(i+1, 8+q+1, 5, QualityInspectionPlanList.get(q).getCheckResult()==""?"":QualityInspectionPlanList.get(q).getCheckResult());
                                 wo.putTxtToCell(i+1, 8+q+1, 6, QualityInspectionPlanList.get(q).getWorkUnit()==null?"":QualityInspectionPlanList.get(q).getWorkUnit());
                                 wo.mergeCell(i+1, 8+q+1,7 , 8+q+1, 8);
                                 wo.putTxtToCell(i+1, 8+q+1, 7, QualityInspectionPlanList.get(q).getProcessUnits()==null?"":QualityInspectionPlanList.get(q).getProcessUnits());
                                 wo.putTxtToCell(i+1, 8+q+1, 8, QualityInspectionPlanList.get(q).getProductionDepartment()==null ?"":QualityInspectionPlanList.get(q).getProductionDepartment());
                                if(QualityInspectionPlanList.get(q).getCheckTime()!=null){
                                     wo.putTxtToCell(i+1, 8+q+1, 9, DateUtil.getDate(DateUtil.getDateFromTs( QualityInspectionPlanList.get(q).getCheckTime())));
                                }else{
                                    wo.putTxtToCell(i+1, 8+q+1, 9, "");
                                }
                                 wo.putTxtToCell(i+1, 8+q+1, 10, QualityInspectionPlanList.get(q).getRemark()==null?"":QualityInspectionPlanList.get(q).getRemark());
                                 wo.moveDown(1);//换行
                            }
                        }
                        wo.nextLine();
                    }
                }

                //下载
                String path = ConfigUtil.getPropertyValueByName("FILE_PATH") + "\\泉州石化质量检修表.doc";
                System.out.println("path="+path);
                wo.save(path);
                wo.close();

                // path是指欲下载的文件的路径。
                File file = new File(path);
                // 取得文件名。
                String filename = file.getName();
                // 取得文件的后缀名。
                String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(path));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                // 清空response
                response.reset();
                // 设置response的Header
                String fname = new String((filename ).getBytes(), "ISO-8859-1");
                response.addHeader("Content-Disposition", "attachment;filename=" + fname);
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
                System.gc();
                file.delete();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                request.getSession().setAttribute("proName", true);
            }
            return null;
        }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答