word自定义格式 并下载
/**
*
* @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自定义格式 并下载的更多相关文章
- Excel 单元格自定义格式技巧总结
第一部分 Excel 中的单元格格式是一个最基本但是又很高级的技能,说它基本是因为我们几乎天天都会用到它,会用它来设置一些简单的格式,比如日期,文本等等:高级是因为利用 Excel 单元格的自定义格式 ...
- WORD自定义宏
自定义快捷键 折叠所有标题 Word选项—自定义功能区—自定义键盘—不在功能区内的命令—ColllapseAllHeadings 展开所有标题 Word选项—自定义功能区—自定义键盘—不在功能区内的命 ...
- 代码中设置excel自定义格式为[红色]的处理方法
有时候,excel的自定义格式设置时 ,会遇到需要设置为¥#,##0;[红色]¥-#,##0的格式. 其中会带一个颜色标记,但是如果这样的一句代码,放在英文版的Office里面,就失效了,因为英文版应 ...
- logback自定义格式转换器
创建自定义格式转换符有两步. 首先,必须继承ClassicConverter类.ClassicConverter对象负责从ILoggingEvent 提取信息,并产生一个字符串.例如,LoggerCo ...
- 去掉word冗余格式 java正则表达式
word转换html时,会留下很多格式,有些格式并不是我们所需要的,然而这些格式比真正的文章内容还要多,严重影响页面的加载速度,因此就需要找个一个好的解决方案把这些多余的格式个去掉.网上有很多去除wo ...
- Objective-c 多线程操作 自定义NSOperation 模拟下载
写在前面 使用多线程下载图片,使用内存缓存和磁盘缓存. 这里只为理解NSOperation及其派生类 真要应用到APP中 请下载成熟的第三方库 效果 下载多张图片时可控制线程并发数 分析 自定义NSO ...
- csv格式订单下载,完成后伴随邮件通知下载
前言 功能开发中会遇到大量订单下载,而服务器的请求响应时间又配置的很短,导致下载时候请求超时. 这篇文章主要思路:异步查询数据,生成csv文件,放入email中并发送给用户.(异步部分本文不做介绍,配 ...
- Word自定义多级列表样式
Word自定义多级列表样式: 1. 2. 3.取个名字 在这里鼠标移上时显示 : 4. 5. 定义完成,即可使用:
- 用 Lua 控制 MIDI 合成器来播放自定义格式乐谱
用 Lua 控制 MIDI 合成器来播放自定义格式乐谱 作者: FreeBlues 最新: https://www.cnblogs.com/freeblues/p/9936844.html 说明: 本 ...
随机推荐
- 转载:持续集成之解决jenkins内存溢出问题
在jenkins master-slave配置中,总是出现内存溢出问题,更换了机器设备仍然跑不起来: 问题如下: Status Code: 500 Exception: org.apache.c ...
- CodeForces 279D The Minimum Number of Variables 题解
题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...
- PHP-Redis扩展使用手册(一)
//初始化redis实例 $redis = new Redis(); /* connect . open 链接redis * @param string host redis服务器地址 * @para ...
- 使用TFHelp解析Html
似乎是第一次使用TFHelp解析Html,也是第一次解析Html遇到挺多的难题,现在这里简单的总结一下,慢慢补充TFHelp的使用! https://github.com/topfunky/hpple ...
- Delphi XE6 原生解析json
Delphi XE5带了system.json单元,原生提供了json支持类.下面是解析json用法说明: 最简单的JSON大致像这样 { "date":"周二(今天, ...
- 实战Java虚拟机之四:提升性能,禁用System.gc() ?
今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...
- px与rem关系及转换
PX特点 1. IE无法调整那些使用px作为单位的字体大小:2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位:3. Firefox能够调整px和em,rem,但是96%以上的中 ...
- [转]Linux学习笔记——例说makefile 头文件查找路径
0.前言 从学习C语言开始就慢慢开始接触makefile,查阅了很多的makefile的资料但总感觉没有真正掌握makefile,如果自己动手写一个makefile总觉得非常吃力.所以特意借助 ...
- WPF重写Image实现动态图片--未测试
WPF很强大,但是当WPF的image控件遇到gif时就只读了图片的第一帧,很好很强大! WPF不屑于gif的简单动画! 幸好WPF里有MediaElement这个东西,它是对MediaPlyer的一 ...
- 非常适用的Sourceinsight插件,提高效率事半功倍
一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用的编辑工具可以任意定位,跳转,回退,本人一直 使用该工具做C/C++开发,sourceinsight能够满 ...