32f641b5bcaacbbc7762e7e9288cb649ba049c37.svn-base 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.sinosoft.common.word;
  2. import java.awt.Color;
  3. import java.io.FileOutputStream;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.List;
  8. import org.apache.log4j.Logger;
  9. import org.apache.poi.xwpf.usermodel.Borders;
  10. import org.apache.poi.xwpf.usermodel.BreakType;
  11. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  12. import org.apache.poi.xwpf.usermodel.TextAlignment;
  13. import org.apache.poi.xwpf.usermodel.VerticalAlign;
  14. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  15. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  16. import org.apache.poi.xwpf.usermodel.XWPFRun;
  17. import com.lowagie.text.BadElementException;
  18. import com.lowagie.text.Cell;
  19. import com.lowagie.text.Element;
  20. import com.lowagie.text.Table;
  21. import com.persistence.service.PersistenceFactory;
  22. import com.persistence.service.SysPersistence;
  23. import com.persistence.service.exception.PersistenceException;
  24. import com.sinosoft.em.baobiao.xianlutingyun.dao.BaoBiaoDao;
  25. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  26. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  27. //java生成word的简单示例
  28. public class WordUtilPOI {
  29. private final Logger log = Logger.getLogger(getClass());
  30. public String getWord(String filePath,String projectDeployPath,String deptId, String role_Id,String eventId) {
  31. //filePath="d:\\test";
  32. String startDate = "";
  33. String endDate = "";
  34. try{
  35. XWPFDocument doc = new XWPFDocument();
  36. /* 设置标题*/
  37. XWPFParagraph p1 = doc.createParagraph();
  38. p1.setAlignment(ParagraphAlignment.CENTER);
  39. p1.setVerticalAlignment(TextAlignment.CENTER);
  40. XWPFRun r1 = p1.createRun();
  41. r1.setBold(true);
  42. r1.setText("应 急 工 作 专 报");
  43. r1.setFontSize(26);
  44. r1.setBold(true);
  45. // r1.setColor("red");
  46. /*副标题*/
  47. String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  48. String year=date.split("-")[0];
  49. String month=date.split("-")[1];
  50. String day=date.split("-")[2];
  51. XWPFParagraph p2 = doc.createParagraph();
  52. p2.setAlignment(ParagraphAlignment.CENTER);
  53. XWPFRun r2 = p2.createRun();
  54. r2.setText("("+year+"年第XX期)");
  55. // r2.setStrike(true);
  56. r2.setFontSize(18);
  57. XWPFParagraph p3 = doc.createParagraph();
  58. p3.setAlignment(ParagraphAlignment.RIGHT);
  59. XWPFRun r3 = p3.createRun();
  60. r3.setText("编制:"+new BaoBiaoDao().getNameByCompId(deptId)+"应急办公室 "+year+"年"+month+"月"+day+"日");
  61. // r3.setStrike(true);
  62. r3.setFontSize(18);
  63. r3.setSubscript(VerticalAlign.SUPERSCRIPT);
  64. /**
  65. * 正文
  66. */
  67. /*
  68. * 第一段:
  69. * 标题:风雨情
  70. * 正文:内容
  71. */
  72. XWPFParagraph bp1 = doc.createParagraph();
  73. XWPFRun bp1_t=bp1.createRun();
  74. bp1_t.setText("一、风雨情");
  75. bp1_t.setBold(true);
  76. bp1_t.setFontSize(16);
  77. XWPFRun bp1_c=bp1.createRun();
  78. bp1_c.setFontSize(16);
  79. bp1_c.setText("XXXXXXXX");
  80. bp1_c.addBreak();
  81. /**
  82. * 第二段:
  83. * 标题:电网受影响情况
  84. * 正文:内容
  85. */
  86. XWPFParagraph bp2=doc.createParagraph();
  87. XWPFRun bp2_t=bp2.createRun();
  88. bp2_t.setBold(true);
  89. bp2_t.setFontSize(16);
  90. bp2_t.setText("二、电网受影响情况");
  91. XWPFDocument bp2_d= bp2.getDocument();
  92. XWPFRun bp2_c=bp2.createRun();
  93. bp2_c.getDocument();
  94. bp2_c.setText(new BaoBiaoDao().getDWQQ(deptId));
  95. System.out.println("=============");
  96. System.out.println(new BaoBiaoDao().getDWQQ(deptId));
  97. bp2_c.addCarriageReturn();
  98. bp2_c.setFontSize(16);
  99. /**
  100. * 第三段:
  101. * 标题:防御工作
  102. */
  103. XWPFParagraph bp3=doc.createParagraph();
  104. XWPFRun bp3_t=bp3.createRun();
  105. bp3_t.setFontSize(16);
  106. bp3_t.setText("三、防御工作");
  107. XWPFRun bp3_c=bp3.createRun();
  108. bp3_c.setFontSize(16);
  109. bp3_c.setText(new BaoBiaoDao().getReportJBContent(deptId,role_Id));
  110. /**
  111. *第四段:
  112. *标题:预警响应
  113. */
  114. XWPFParagraph bp4=doc.createParagraph();
  115. XWPFRun bp4_t=bp4.createRun();
  116. bp4_t.setFontSize(20);
  117. bp4_t.setText("四、 响应预警");
  118. XWPFRun bp4_c=bp4.createRun();
  119. bp4_c.setFontSize(16);
  120. bp4_c.setText(new BaoBiaoDao().getAlertWord(deptId,eventId));
  121. XWPFParagraph bpb=doc.createParagraph();
  122. XWPFRun bpb_r=bpb.createRun();
  123. bpb_r.setFontSize(16);
  124. bpb_r.setText("报送至广东省人民管理政府应急办公室");
  125. /**
  126. * 红线
  127. */
  128. XWPFParagraph bph=doc.createParagraph();
  129. bph.setBorderBottom(Borders.BASIC_THIN_LINES);
  130. /**
  131. * 作者信息
  132. *
  133. */
  134. XWPFParagraph bphz=doc.createParagraph();
  135. XWPFRun bphz_author=bphz.createRun();
  136. bphz_author.setFontSize(20);
  137. bphz_author.setText("编写:XXX,XXXXXXXXXXX。");
  138. XWPFRun bphz_time=bphz.createRun();
  139. bphz_time.setFontSize(20);
  140. Calendar c = Calendar.getInstance();
  141. SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
  142. bphz_time.setText(f.format(c.getTime()));
  143. FileOutputStream out = new FileOutputStream(filePath);
  144. doc.write(out);
  145. out.close();
  146. /**第一段标题 风雨情*//*
  147. document.add(new Paragraph("一、风雨情", RtfParagraphStyle.STYLE_HEADING_1));
  148. *//**第一段内容*//*
  149. document.add(new ReportContentStyle().getCommonPara("content","XXXXXXXX"));
  150. Paragraph p2 = new Paragraph("二、电网受影响情况", RtfParagraphStyle.STYLE_HEADING_1);
  151. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  152. p2.setFont(new Font(bfChinese, 22, Font.BOLD));
  153. *//**第二段标题 电网受影响情况*//*
  154. document.add(p2);
  155. *//**第二段内容*//*
  156. document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getDWQQ(deptId)));
  157. *//**防御工作*//*
  158. document.add(new Paragraph("三、防御工作", RtfParagraphStyle.STYLE_HEADING_1));
  159. document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getReportJBContent(deptId,role_Id)));
  160. *//**第四段标题 响应预警*//*
  161. document.add(new Paragraph("四、 响应预警", RtfParagraphStyle.STYLE_HEADING_1));
  162. *//**第四段内容*//*
  163. document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getAlertWord(deptId)));
  164. document.add(reportS.getCommonPara("","报送至广东省人民管理政府应急办公室"));
  165. *//**红线段落*//*
  166. img = reportS.getLineGraph(pageWidth, projectDeployPath,"red");
  167. if (img!=null) {
  168. document.add(img);
  169. }
  170. *//**作者信息*//*
  171. document.add(reportS.getCommonPara("","编写:XXX,XXXXXXXXXXX。"));
  172. Calendar c = Calendar.getInstance();
  173. SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
  174. document.add(reportS.getCommonPara("tileText",f.format(c.getTime())));
  175. document.close();
  176. FileOutputStream fos=new FileOutputStream(filePath);
  177. fos.write(buffer.toByteArray());
  178. buffer.flush();
  179. buffer.close();
  180. fos.close();*/
  181. } catch (Exception e) {
  182. log.error(e.getMessage(), e);
  183. }
  184. return filePath;
  185. }
  186. /**
  187. * word中的表格
  188. * @param startDate
  189. * @param endDate
  190. * @return
  191. */
  192. public Table getTable(String startDate,String endDate,String deptId){
  193. Table table = null;
  194. try {
  195. table = new Table(10);
  196. table.setBorderWidth(1);
  197. table.setBorderColor(Color.black);
  198. table.setPadding(0);
  199. table.setSpacing(0);
  200. //时间,路网指数信息,备注
  201. Cell cell = getCell("姓名");
  202. cell.setHeader(true);
  203. table.addCell(cell);
  204. table.addCell(getCell("所属单位"));
  205. table.addCell(getCell("联系人手机"));
  206. table.addCell(getCell("机构类别"));
  207. table.addCell(getCell("联动部门"));
  208. table.addCell(getCell("联动联系人"));
  209. table.addCell(getCell("邮编"));
  210. table.addCell(getCell("办公OA邮箱"));
  211. table.addCell(getCell("地址"));
  212. table.addCell(getCell("网址"));
  213. table.endHeaders();
  214. SysModel sysmodel = ModelFactory.getSysmodel();
  215. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  216. String sql="select a.LIAISON_NAME,b.ORG_DEPT_NAME,a.PHONE,c.fd_description,d.ORG_NAME,e.INLINK_NAME,a.CODE,a.EMAIL,a.ORG_ADDRESS,a.SITE from EMC_AM_ORG_OUTLINK a,EMC_AM_OUT_DEPARTMENT b,BM_MAPVALUECANST c,EMC_DEPARTMENT_IN d,EMC_AM_ORG_INLINK e where a.ORG_DEPT_id=b.FD_OBJECTID and a.ORG_TYPE=c.fd_code and a.link_man=e.FD_OBJECTID and a.LINK_DEPT=d.ORG_ID and c.fd_type='BM_OUT_ORG_TYPE' and a.is_del='0' and a.ORG_DEPT_ID='"+deptId+"'";
  217. try {
  218. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  219. if(tempList!=null&&tempList.size()>0){
  220. for(int i=0;i<tempList.size();i++){
  221. String[]arr=tempList.get(i);
  222. for(int j=0;j<arr.length;j++){
  223. table.addCell(arr[j]);
  224. }
  225. }
  226. }
  227. } catch (PersistenceException e) {
  228. // TODO Auto-generated catch block
  229. e.printStackTrace();
  230. }
  231. } catch (BadElementException e) {
  232. // TODO Auto-generated catch block
  233. e.printStackTrace();
  234. }
  235. return table;
  236. }
  237. //表格的单元格
  238. public Cell getCell(String content){
  239. Cell cell = new Cell(content);
  240. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  241. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  242. return cell;
  243. }
  244. // public static void main(String[] args) {
  245. // WordUtil wu=new WordUtil();
  246. // wu.
  247. // }
  248. //
  249. }