首页 > 代码库 > word自定义格式 并下载
word自定义格式 并下载
1 /** 2 * 3 * @param pRun 4 * @param 20 间距 5 * @param fontSize 字体大小 6 * @param bold 是否加粗 7 * @param underLine 是否下划线 8 * @param underLineColor 下划线的颜色 9 * @param underLineStyle 下划线的样式 1 10 */ 11 private static void setCommonRunStyle(XWPFRun pRun,int Textposition,int fontSize,boolean bold,boolean underLine,String underLineColor,int underLineStyle){ 12 pRun.setFontSize(fontSize);//设置字体大小 13 pRun.setFontFamily("Courier"); // 设置使用何种字体 14 pRun.setTextPosition(Textposition); // 设置上下两行之间的间距 15 pRun.setBold(bold); // 设置加粗 16 17 CTRPr pRpr = null; 18 if (pRun.getCTR() != null) { 19 pRpr = pRun.getCTR().getRPr(); 20 if (pRpr == null) { 21 pRpr = pRun.getCTR().addNewRPr(); 22 } 23 } 24 if(underLine){ 25 CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU(); 26 u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19))); 27 if (underLineColor != null) { 28 u.setColor(underLineColor); 29 } 30 } 31 } 32 33 /**** 34 * word 中添加换行 35 * @param i i必须要大于0 i为几 就是中间添加几个20的高度 36 */ 37 private static void wordHeight(int j,XWPFDocument doc){ 38 for(int i=0;i<j;i++){ 39 XWPFParagraph kongP1= doc.createParagraph(); 40 XWPFRun kongP1Run = kongP1.createRun(); 41 setCommonRunStyle(kongP1Run,100,100,false,false,"666666",1); 42 kongP1Run.setText(" "); 43 } 44 } 45 46 47 /** 48 * 下载计划书word 49 * @author 50 * @time 51 * @description 52 */ 53 public static String downloadPlanWord(HttpServletRequest request,HttpServletResponse response){ 54 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 55 Delegator delegator = (Delegator)request.getAttribute("delegator"); 56 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 57 Map<String, Object> outResult = ServiceUtil.returnSuccess(); 58 String startTime = request.getParameter("startTime"); 59 String endTime = request.getParameter("endTime"); 60 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 61 XWPFDocument doc = new XWPFDocument(); 62 try { 63 //根据农户进行计划查询的分组 未执行的 64 List<EntityCondition> Conds = FastList.newInstance(); 65 Conds.add(EntityCondition.makeCondition("executeStatusId", EntityOperator.EQUALS,"PPLECT_UNEXECUTED")); 66 if(UtilValidate.isNotEmpty(startTime)){ 67 Conds.add(EntityCondition.makeCondition("planStartTime",EntityOperator.GREATER_THAN_EQUAL_TO,Timestamp.valueOf(startTime+" 00:00:00"))); 68 } 69 if(UtilValidate.isNotEmpty(endTime)){ 70 Conds.add(EntityCondition.makeCondition("planStartTime",EntityOperator.LESS_THAN_EQUAL_TO,Timestamp.valueOf(endTime+" 00:00:00"))); 71 } 72 List<GenericValue> planList = delegator.findList("MaterialProductionPlanInfo", 73 EntityCondition.makeCondition(Conds),null, null, null, false); 74 if(UtilValidate.isEmpty(planList)){ 75 XWPFParagraph noFarmer = doc.createParagraph(); 76 noFarmer.setAlignment(ParagraphAlignment.CENTER); 77 noFarmer.setVerticalAlignment(TextAlignment.TOP); 78 XWPFRun noFarmerRun = noFarmer.createRun(); 79 setCommonRunStyle(noFarmerRun,20,20,true,false,"666666",1); 80 noFarmerRun.setText("没有农户存在种植计划"); 81 response.reset(); 82 String fileName = "种植计划书.doc"; 83 response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8"); 84 response.setHeader("Content-Disposition","attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8")); 85 OutputStream out = response.getOutputStream(); 86 doc.write(out); 87 out.flush(); 88 doc.write(out); 89 out.close(); 90 return "error"; 91 } 92 Map<String,List<GenericValue>> farmerMap = FastMap.newInstance(); 93 for(GenericValue plan:planList){ 94 if(farmerMap.containsKey(plan.getString("farmerId"))){ 95 farmerMap.get(plan.getString("farmerId")).add(plan); 96 }else{ 97 List<GenericValue> planEntityList = FastList.newInstance(); 98 planEntityList.add(plan); 99 farmerMap.put(plan.getString("farmerId"),planEntityList);100 }101 }102 int i =0;103 XWPFParagraph titleP = null;104 GenericValue farmerEntity = null;105 for(String key : farmerMap.keySet()){106 farmerEntity = delegator.findOne("Farmer", false, UtilMisc.toMap("farmerId", key));107 planList = farmerMap.get(key);108 titleP = doc.createParagraph();109 if(i>0){110 titleP.setPageBreak(true);111 }112 i++;113 titleP.setAlignment(ParagraphAlignment.CENTER);// 设置字体对齐方式114 XWPFRun titlePRun = titleP.createRun(); // 第一页要使用p1所定义的属性115 setCommonRunStyle(titlePRun,40,20,true,false,"666666",1);116 titlePRun.setText("种植计划下达通知书");117 // 农户118 XWPFParagraph farmerP = doc.createParagraph();119 XWPFRun farmerRunLabel = farmerP.createRun();// 第一页要使用p1所定义的属性120 setCommonRunStyle(farmerRunLabel,20,16,false,false,"666666",1);121 farmerRunLabel.setText("农户:");122 XWPFRun farmerRun = farmerP.createRun();// 第一页要使用p1所定义的属性123 setCommonRunStyle(farmerRun,20,16,false,true,"666666",1);124 farmerRun.setText(farmerEntity.getString("farmerName"));125 //计划数目126 XWPFParagraph sizeP = doc.createParagraph();127 XWPFRun sizeRunLabel1 = sizeP.createRun();128 setCommonRunStyle(sizeRunLabel1,20,16,false,false,"666666",1);129 sizeRunLabel1.setText(" 根据农场生产计划总体安排,对您提出的");130 XWPFRun sizeRunValue =http://www.mamicode.com/ sizeP.createRun();131 setCommonRunStyle(sizeRunValue,20,16,false,true,"666666",1);132 sizeRunValue.setText(String.valueOf(planList.size()));133 XWPFRun sizeRunLabel2 = sizeP.createRun();134 setCommonRunStyle(sizeRunLabel2,20,16,false,false,"666666",1);135 sizeRunLabel2.setText("座棚种植请求,现通知如下:");136 //空行137 wordHeight(1,doc);138 //种植计划139 int planIndex=0;140 for(GenericValue plan:planList){141 planIndex ++ ;142 XWPFParagraph planP = doc.createParagraph();143 planP.setVerticalAlignment(TextAlignment.TOP);144 XWPFRun farmFieldNameRun = planP.createRun();145 setCommonRunStyle(farmFieldNameRun,20,16,false,true,"666666",1);146 farmFieldNameRun.setText(plan.getString("farmFieldName"));147 148 XWPFRun planRunLabel1 = planP.createRun();149 setCommonRunStyle(planRunLabel1,20,16,false,false,"666666",1);150 planRunLabel1.setText("棚,种植品种");151 152 XWPFRun materialNameRun = planP.createRun();153 setCommonRunStyle(materialNameRun,20,16,false,true,"666666",1);154 materialNameRun.setText(plan.getString("materialName"));155 156 XWPFRun planRunLabel2 = planP.createRun();157 setCommonRunStyle(planRunLabel2,20,16,false,false,"666666",1);158 planRunLabel2.setText(",种植时间");159 160 String planStartTimeStr = sdf.format(new Date(plan.getTimestamp("planStartTime").getTime()));161 XWPFRun planStartTimeRun = planP.createRun();162 setCommonRunStyle(planStartTimeRun,20,16,false,true,"666666",1);163 planStartTimeRun.setText(planStartTimeStr);164 if(planIndex==planList.size()){165 wordHeight(4,doc);166 }167 }168 //提示169 XWPFParagraph tipP = doc.createParagraph();170 tipP.setAlignment(ParagraphAlignment.CENTER);171 tipP.setVerticalAlignment(TextAlignment.TOP);172 XWPFRun tipPRun = tipP.createRun();173 setCommonRunStyle(tipPRun,120,14,false,false,"666666",1);174 tipPRun.setText("请务必按照通知下达的计划种植,否则公司不负责收购。");175 //农户176 XWPFParagraph farmerSignP = doc.createParagraph();177 XWPFRun farmerSignPRun = farmerSignP.createRun();178 setCommonRunStyle(farmerSignPRun,30,16,false,false,"666666",1);179 farmerSignPRun.setText("农户:");180 //技术员181 XWPFParagraph technicianP = doc.createParagraph();182 XWPFRun technicianPRun = technicianP.createRun();183 setCommonRunStyle(technicianPRun,30,16,false,false,"666666",1);184 technicianPRun.setText("技术员:");185 //负责人186 XWPFParagraph chargerP = doc.createParagraph();187 XWPFRun chargerPRun = chargerP.createRun();188 setCommonRunStyle(chargerPRun,30,16,false,false,"666666",1);189 chargerPRun.setText("负责人:");190 //通知送达时间191 XWPFParagraph deliveryTimeP = doc.createParagraph();192 XWPFRun deliveryTimePRun = deliveryTimeP.createRun();193 setCommonRunStyle(deliveryTimePRun,30,16,false,false,"666666",1);194 deliveryTimePRun.setText("通知送达时间: 年 月 日");195 //苏星生态农业生产科196 XWPFParagraph suxP = doc.createParagraph();197 suxP.setAlignment(ParagraphAlignment.RIGHT);198 XWPFRun suxPRun = suxP.createRun();199 setCommonRunStyle(suxPRun,30,16,false,false,"666666",1);200 suxPRun.setText("苏星生态农业生产科");201 }202 response.reset();203 String fileName = "种植计划书.doc";204 response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");205 response.setHeader("Content-Disposition","attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));206 OutputStream out = response.getOutputStream();207 doc.write(out);208 out.flush();209 doc.write(out);210 out.close();211 } catch (GenericEntityException e) {212 // TODO Auto-generated catch block213 Debug.logError(e, "查询农户种植计划异常" + e.toString(), module);214 CommonEvents.writeResultToResponse(ServiceUtil.returnError("查询农户种植计划异常"), response);215 return "error";216 } catch (IOException e) {217 // TODO Auto-generated catch block218 e.printStackTrace();219 }220 return "success";221 }
word自定义格式 并下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。