/**
*
* @param pRun
* @param 20 间距
* @param fontSize 字体大小
* @param bold 是否加粗
* @param underLine 是否下划线
* @param underLineColor 下划线的颜色
* @param underLineStyle 下划线的样式 1
*/
private static void setCommonRunStyle(XWPFRun pRun,int Textposition,int fontSize,boolean bold,boolean underLine,String underLineColor,int underLineStyle){
pRun.setFontSize(fontSize);//设置字体大小
pRun.setFontFamily("Courier"); // 设置使用何种字体
pRun.setTextPosition(Textposition); // 设置上下两行之间的间距
pRun.setBold(bold); // 设置加粗 CTRPr pRpr = null;
if (pRun.getCTR() != null) {
pRpr = pRun.getCTR().getRPr();
if (pRpr == null) {
pRpr = pRun.getCTR().addNewRPr();
}
}
if(underLine){
CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();
u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));
if (underLineColor != null) {
u.setColor(underLineColor);
}
}
} /****
* word 中添加换行
* @param i i必须要大于0 i为几 就是中间添加几个20的高度
*/
private static void wordHeight(int j,XWPFDocument doc){
for(int i=0;i<j;i++){
XWPFParagraph kongP1= doc.createParagraph();
XWPFRun kongP1Run = kongP1.createRun();
setCommonRunStyle(kongP1Run,100,100,false,false,"666666",1);
kongP1Run.setText(" ");
}
} /**
* 下载计划书word
* @author
* @time
* @description
*/
public static String downloadPlanWord(HttpServletRequest request,HttpServletResponse response){
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Delegator delegator = (Delegator)request.getAttribute("delegator");
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
Map<String, Object> outResult = ServiceUtil.returnSuccess();
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
XWPFDocument doc = new XWPFDocument();
try {
//根据农户进行计划查询的分组 未执行的
List<EntityCondition> Conds = FastList.newInstance();
Conds.add(EntityCondition.makeCondition("executeStatusId", EntityOperator.EQUALS,"PPLECT_UNEXECUTED"));
if(UtilValidate.isNotEmpty(startTime)){
Conds.add(EntityCondition.makeCondition("planStartTime",EntityOperator.GREATER_THAN_EQUAL_TO,Timestamp.valueOf(startTime+" 00:00:00")));
}
if(UtilValidate.isNotEmpty(endTime)){
Conds.add(EntityCondition.makeCondition("planStartTime",EntityOperator.LESS_THAN_EQUAL_TO,Timestamp.valueOf(endTime+" 00:00:00")));
}
List<GenericValue> planList = delegator.findList("MaterialProductionPlanInfo",
EntityCondition.makeCondition(Conds),null, null, null, false);
if(UtilValidate.isEmpty(planList)){
XWPFParagraph noFarmer = doc.createParagraph();
noFarmer.setAlignment(ParagraphAlignment.CENTER);
noFarmer.setVerticalAlignment(TextAlignment.TOP);
XWPFRun noFarmerRun = noFarmer.createRun();
setCommonRunStyle(noFarmerRun,20,20,true,false,"666666",1);
noFarmerRun.setText("没有农户存在种植计划");
response.reset();
String fileName = "种植计划书.doc";
response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
doc.write(out);
out.flush();
doc.write(out);
out.close();
return "error";
}
Map<String,List<GenericValue>> farmerMap = FastMap.newInstance();
for(GenericValue plan:planList){
if(farmerMap.containsKey(plan.getString("farmerId"))){
farmerMap.get(plan.getString("farmerId")).add(plan);
}else{
List<GenericValue> planEntityList = FastList.newInstance();
planEntityList.add(plan);
farmerMap.put(plan.getString("farmerId"),planEntityList);
}
}
int i =0;
XWPFParagraph titleP = null;
GenericValue farmerEntity = null;
for(String key : farmerMap.keySet()){
farmerEntity = delegator.findOne("Farmer", false, UtilMisc.toMap("farmerId", key));
planList = farmerMap.get(key);
titleP = doc.createParagraph();
if(i>0){
titleP.setPageBreak(true);
}
i++;
titleP.setAlignment(ParagraphAlignment.CENTER);// 设置字体对齐方式
XWPFRun titlePRun = titleP.createRun(); // 第一页要使用p1所定义的属性
setCommonRunStyle(titlePRun,40,20,true,false,"666666",1);
titlePRun.setText("种植计划下达通知书");
// 农户
XWPFParagraph farmerP = doc.createParagraph();
XWPFRun farmerRunLabel = farmerP.createRun();// 第一页要使用p1所定义的属性
setCommonRunStyle(farmerRunLabel,20,16,false,false,"666666",1);
farmerRunLabel.setText("农户:");
XWPFRun farmerRun = farmerP.createRun();// 第一页要使用p1所定义的属性
setCommonRunStyle(farmerRun,20,16,false,true,"666666",1);
farmerRun.setText(farmerEntity.getString("farmerName"));
//计划数目
XWPFParagraph sizeP = doc.createParagraph();
XWPFRun sizeRunLabel1 = sizeP.createRun();
setCommonRunStyle(sizeRunLabel1,20,16,false,false,"666666",1);
sizeRunLabel1.setText(" 根据农场生产计划总体安排,对您提出的");
XWPFRun sizeRunValue = sizeP.createRun();
setCommonRunStyle(sizeRunValue,20,16,false,true,"666666",1);
sizeRunValue.setText(String.valueOf(planList.size()));
XWPFRun sizeRunLabel2 = sizeP.createRun();
setCommonRunStyle(sizeRunLabel2,20,16,false,false,"666666",1);
sizeRunLabel2.setText("座棚种植请求,现通知如下:");
//空行
wordHeight(1,doc);
//种植计划
int planIndex=0;
for(GenericValue plan:planList){
planIndex ++ ;
XWPFParagraph planP = doc.createParagraph();
planP.setVerticalAlignment(TextAlignment.TOP);
XWPFRun farmFieldNameRun = planP.createRun();
setCommonRunStyle(farmFieldNameRun,20,16,false,true,"666666",1);
farmFieldNameRun.setText(plan.getString("farmFieldName")); XWPFRun planRunLabel1 = planP.createRun();
setCommonRunStyle(planRunLabel1,20,16,false,false,"666666",1);
planRunLabel1.setText("棚,种植品种"); XWPFRun materialNameRun = planP.createRun();
setCommonRunStyle(materialNameRun,20,16,false,true,"666666",1);
materialNameRun.setText(plan.getString("materialName")); XWPFRun planRunLabel2 = planP.createRun();
setCommonRunStyle(planRunLabel2,20,16,false,false,"666666",1);
planRunLabel2.setText(",种植时间"); String planStartTimeStr = sdf.format(new Date(plan.getTimestamp("planStartTime").getTime()));
XWPFRun planStartTimeRun = planP.createRun();
setCommonRunStyle(planStartTimeRun,20,16,false,true,"666666",1);
planStartTimeRun.setText(planStartTimeStr);
if(planIndex==planList.size()){
wordHeight(4,doc);
}
}
//提示
XWPFParagraph tipP = doc.createParagraph();
tipP.setAlignment(ParagraphAlignment.CENTER);
tipP.setVerticalAlignment(TextAlignment.TOP);
XWPFRun tipPRun = tipP.createRun();
setCommonRunStyle(tipPRun,120,14,false,false,"666666",1);
tipPRun.setText("请务必按照通知下达的计划种植,否则公司不负责收购。");
//农户
XWPFParagraph farmerSignP = doc.createParagraph();
XWPFRun farmerSignPRun = farmerSignP.createRun();
setCommonRunStyle(farmerSignPRun,30,16,false,false,"666666",1);
farmerSignPRun.setText("农户:");
//技术员
XWPFParagraph technicianP = doc.createParagraph();
XWPFRun technicianPRun = technicianP.createRun();
setCommonRunStyle(technicianPRun,30,16,false,false,"666666",1);
technicianPRun.setText("技术员:");
//负责人
XWPFParagraph chargerP = doc.createParagraph();
XWPFRun chargerPRun = chargerP.createRun();
setCommonRunStyle(chargerPRun,30,16,false,false,"666666",1);
chargerPRun.setText("负责人:");
//通知送达时间
XWPFParagraph deliveryTimeP = doc.createParagraph();
XWPFRun deliveryTimePRun = deliveryTimeP.createRun();
setCommonRunStyle(deliveryTimePRun,30,16,false,false,"666666",1);
deliveryTimePRun.setText("通知送达时间: 年 月 日");
//苏星生态农业生产科
XWPFParagraph suxP = doc.createParagraph();
suxP.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun suxPRun = suxP.createRun();
setCommonRunStyle(suxPRun,30,16,false,false,"666666",1);
suxPRun.setText("苏星生态农业生产科");
}
response.reset();
String fileName = "种植计划书.doc";
response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
doc.write(out);
out.flush();
doc.write(out);
out.close();
} catch (GenericEntityException e) {
// TODO Auto-generated catch block
Debug.logError(e, "查询农户种植计划异常" + e.toString(), module);
CommonEvents.writeResultToResponse(ServiceUtil.returnError("查询农户种植计划异常"), response);
return "error";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}

word自定义格式 并下载的更多相关文章

  1. Excel 单元格自定义格式技巧总结

    第一部分 Excel 中的单元格格式是一个最基本但是又很高级的技能,说它基本是因为我们几乎天天都会用到它,会用它来设置一些简单的格式,比如日期,文本等等:高级是因为利用 Excel 单元格的自定义格式 ...

  2. WORD自定义宏

    自定义快捷键 折叠所有标题 Word选项—自定义功能区—自定义键盘—不在功能区内的命令—ColllapseAllHeadings 展开所有标题 Word选项—自定义功能区—自定义键盘—不在功能区内的命 ...

  3. 代码中设置excel自定义格式为[红色]的处理方法

    有时候,excel的自定义格式设置时 ,会遇到需要设置为¥#,##0;[红色]¥-#,##0的格式. 其中会带一个颜色标记,但是如果这样的一句代码,放在英文版的Office里面,就失效了,因为英文版应 ...

  4. logback自定义格式转换器

    创建自定义格式转换符有两步. 首先,必须继承ClassicConverter类.ClassicConverter对象负责从ILoggingEvent 提取信息,并产生一个字符串.例如,LoggerCo ...

  5. 去掉word冗余格式 java正则表达式

    word转换html时,会留下很多格式,有些格式并不是我们所需要的,然而这些格式比真正的文章内容还要多,严重影响页面的加载速度,因此就需要找个一个好的解决方案把这些多余的格式个去掉.网上有很多去除wo ...

  6. Objective-c 多线程操作 自定义NSOperation 模拟下载

    写在前面 使用多线程下载图片,使用内存缓存和磁盘缓存. 这里只为理解NSOperation及其派生类 真要应用到APP中 请下载成熟的第三方库 效果 下载多张图片时可控制线程并发数 分析 自定义NSO ...

  7. csv格式订单下载,完成后伴随邮件通知下载

    前言 功能开发中会遇到大量订单下载,而服务器的请求响应时间又配置的很短,导致下载时候请求超时. 这篇文章主要思路:异步查询数据,生成csv文件,放入email中并发送给用户.(异步部分本文不做介绍,配 ...

  8. Word自定义多级列表样式

    Word自定义多级列表样式: 1. 2. 3.取个名字 在这里鼠标移上时显示 : 4. 5. 定义完成,即可使用:

  9. 用 Lua 控制 MIDI 合成器来播放自定义格式乐谱

    用 Lua 控制 MIDI 合成器来播放自定义格式乐谱 作者: FreeBlues 最新: https://www.cnblogs.com/freeblues/p/9936844.html 说明: 本 ...

随机推荐

  1. php+mysql+smarty+oop

    php+mysql+smarty+oop 设计新闻系统简单的UML模 powerdesigner和diagram designer 设计所需环境.模块.模板样式 数据库uml设计和创建数据库结构 db ...

  2. jquery:closest和parents的主要区别

    closest和parents的主要区别是:1,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找:2,前者逐级向上查找,直到发现匹配的元素后就停止了,后者一直向上查找直到根元素,然后把这些元素放进 ...

  3. 最全html5 meta设置详解 (转)

    meta 详解,html5 meta 标签日常设置   <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html la ...

  4. grunt 一个目录下所有的js文件压缩 配置收藏

    module.exports = function(grunt){ // 项目配置 grunt.initConfig({ pkg: grunt.file.readJSON('package.json' ...

  5. uri不能处理结尾为点的url的问题

    最近需要和某公司进行接口对接,发现用WebClient获取URL结尾带.的资源,会出404错误.但是用IE还有其它浏览器访问此资源,还能找到它.很神奇. 于是,我百度了,找到的一堆都是说此url不规范 ...

  6. tornado 学习笔记8 模板以及UI

          Tornado 包含一个简单.快速而且灵活的模板语言.       Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...

  7. sqlserver2008 日志文件压缩的完整解决办法

    在项目中数据库创建了一个本地发布和订阅,造成日志文件飞涨,想把日志文件缩小. 1:最初使用了最常用的方法: USE [master] GO ALTER DATABASE 库名 SET RECOVERY ...

  8. Spring基础学习笔记-Bean的基础知识

    一. Bean的定义,初始化,使用和销毁 二.ref指定依赖的三种模式 三.Bean的五种自动装配模式(autowire) 四.Bean依赖检查的4种模式:配合atuowire使用,dependenc ...

  9. unity3d的GUI元素的界面坐标系统总结(有公式)

    GUIText 和GUITexture 1.GUIText 锚点(Anchor)的概念我就不介绍了.像NGUI和tookit2d还有 Cocos2d中都有这个重要的概念,对于图片我们可以认为是图片自身 ...

  10. iOS CADisplayLink 定时器的使用

    CADisplayLink 是一个能让我们以和屏幕刷新频率相同的频率将内容刻画到屏幕上的定时器,在应用中创建一个新的CADisplayLink对象,把他添加到一个runloop中,并且给他提供一个ta ...