123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package com.sinosoft.common.word;
- import java.awt.Color;
- import java.io.FileOutputStream;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import org.apache.log4j.Logger;
- import org.apache.poi.xwpf.usermodel.Borders;
- import org.apache.poi.xwpf.usermodel.BreakType;
- import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
- import org.apache.poi.xwpf.usermodel.TextAlignment;
- import org.apache.poi.xwpf.usermodel.VerticalAlign;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFParagraph;
- import org.apache.poi.xwpf.usermodel.XWPFRun;
- import com.lowagie.text.BadElementException;
- import com.lowagie.text.Cell;
- import com.lowagie.text.Element;
- import com.lowagie.text.Table;
- import com.persistence.service.PersistenceFactory;
- import com.persistence.service.SysPersistence;
- import com.persistence.service.exception.PersistenceException;
- import com.sinosoft.em.baobiao.xianlutingyun.dao.BaoBiaoDao;
- import com.sysmodel.datamodel.xmlmodel.ModelFactory;
- import com.sysmodel.datamodel.xmlmodel.able.SysModel;
- //java生成word的简单示例
- public class WordUtilPOI {
- private final Logger log = Logger.getLogger(getClass());
- public String getWord(String filePath,String projectDeployPath,String deptId, String role_Id,String eventId) {
- //filePath="d:\\test";
- String startDate = "";
- String endDate = "";
-
- try{
- XWPFDocument doc = new XWPFDocument();
-
-
- /* 设置标题*/
- XWPFParagraph p1 = doc.createParagraph();
- p1.setAlignment(ParagraphAlignment.CENTER);
- p1.setVerticalAlignment(TextAlignment.CENTER);
- XWPFRun r1 = p1.createRun();
- r1.setBold(true);
- r1.setText("应 急 工 作 专 报");
- r1.setFontSize(26);
- r1.setBold(true);
- // r1.setColor("red");
- /*副标题*/
- String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- String year=date.split("-")[0];
- String month=date.split("-")[1];
- String day=date.split("-")[2];
- XWPFParagraph p2 = doc.createParagraph();
- p2.setAlignment(ParagraphAlignment.CENTER);
- XWPFRun r2 = p2.createRun();
- r2.setText("("+year+"年第XX期)");
- // r2.setStrike(true);
- r2.setFontSize(18);
-
- XWPFParagraph p3 = doc.createParagraph();
- p3.setAlignment(ParagraphAlignment.RIGHT);
- XWPFRun r3 = p3.createRun();
- r3.setText("编制:"+new BaoBiaoDao().getNameByCompId(deptId)+"应急办公室 "+year+"年"+month+"月"+day+"日");
- // r3.setStrike(true);
- r3.setFontSize(18);
- r3.setSubscript(VerticalAlign.SUPERSCRIPT);
-
-
- /**
- * 正文
- */
- /*
- * 第一段:
- * 标题:风雨情
- * 正文:内容
- */
- XWPFParagraph bp1 = doc.createParagraph();
- XWPFRun bp1_t=bp1.createRun();
- bp1_t.setText("一、风雨情");
- bp1_t.setBold(true);
- bp1_t.setFontSize(16);
-
- XWPFRun bp1_c=bp1.createRun();
- bp1_c.setFontSize(16);
- bp1_c.setText("XXXXXXXX");
- bp1_c.addBreak();
-
- /**
- * 第二段:
- * 标题:电网受影响情况
- * 正文:内容
- */
- XWPFParagraph bp2=doc.createParagraph();
- XWPFRun bp2_t=bp2.createRun();
- bp2_t.setBold(true);
- bp2_t.setFontSize(16);
- bp2_t.setText("二、电网受影响情况");
-
-
- XWPFDocument bp2_d= bp2.getDocument();
- XWPFRun bp2_c=bp2.createRun();
- bp2_c.getDocument();
- bp2_c.setText(new BaoBiaoDao().getDWQQ(deptId));
- System.out.println("=============");
- System.out.println(new BaoBiaoDao().getDWQQ(deptId));
- bp2_c.addCarriageReturn();
- bp2_c.setFontSize(16);
-
- /**
- * 第三段:
- * 标题:防御工作
- */
- XWPFParagraph bp3=doc.createParagraph();
- XWPFRun bp3_t=bp3.createRun();
- bp3_t.setFontSize(16);
- bp3_t.setText("三、防御工作");
-
- XWPFRun bp3_c=bp3.createRun();
- bp3_c.setFontSize(16);
- bp3_c.setText(new BaoBiaoDao().getReportJBContent(deptId,role_Id));
-
- /**
- *第四段:
- *标题:预警响应
- */
- XWPFParagraph bp4=doc.createParagraph();
- XWPFRun bp4_t=bp4.createRun();
- bp4_t.setFontSize(20);
- bp4_t.setText("四、 响应预警");
-
- XWPFRun bp4_c=bp4.createRun();
- bp4_c.setFontSize(16);
- bp4_c.setText(new BaoBiaoDao().getAlertWord(deptId,eventId));
-
- XWPFParagraph bpb=doc.createParagraph();
- XWPFRun bpb_r=bpb.createRun();
- bpb_r.setFontSize(16);
- bpb_r.setText("报送至广东省人民管理政府应急办公室");
-
-
- /**
- * 红线
- */
- XWPFParagraph bph=doc.createParagraph();
- bph.setBorderBottom(Borders.BASIC_THIN_LINES);
-
- /**
- * 作者信息
- *
- */
- XWPFParagraph bphz=doc.createParagraph();
- XWPFRun bphz_author=bphz.createRun();
- bphz_author.setFontSize(20);
- bphz_author.setText("编写:XXX,XXXXXXXXXXX。");
-
- XWPFRun bphz_time=bphz.createRun();
- bphz_time.setFontSize(20);
- Calendar c = Calendar.getInstance();
- SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
- bphz_time.setText(f.format(c.getTime()));
-
- FileOutputStream out = new FileOutputStream(filePath);
- doc.write(out);
- out.close();
-
- /**第一段标题 风雨情*//*
- document.add(new Paragraph("一、风雨情", RtfParagraphStyle.STYLE_HEADING_1));
-
- *//**第一段内容*//*
- document.add(new ReportContentStyle().getCommonPara("content","XXXXXXXX"));
-
- Paragraph p2 = new Paragraph("二、电网受影响情况", RtfParagraphStyle.STYLE_HEADING_1);
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- p2.setFont(new Font(bfChinese, 22, Font.BOLD));
- *//**第二段标题 电网受影响情况*//*
- document.add(p2);
- *//**第二段内容*//*
- document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getDWQQ(deptId)));
-
- *//**防御工作*//*
- document.add(new Paragraph("三、防御工作", RtfParagraphStyle.STYLE_HEADING_1));
- document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getReportJBContent(deptId,role_Id)));
-
- *//**第四段标题 响应预警*//*
- document.add(new Paragraph("四、 响应预警", RtfParagraphStyle.STYLE_HEADING_1));
- *//**第四段内容*//*
- document.add(new ReportContentStyle().getCommonPara("content",new BaoBiaoDao().getAlertWord(deptId)));
-
- document.add(reportS.getCommonPara("","报送至广东省人民管理政府应急办公室"));
- *//**红线段落*//*
- img = reportS.getLineGraph(pageWidth, projectDeployPath,"red");
- if (img!=null) {
- document.add(img);
- }
- *//**作者信息*//*
- document.add(reportS.getCommonPara("","编写:XXX,XXXXXXXXXXX。"));
-
- Calendar c = Calendar.getInstance();
- SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
- document.add(reportS.getCommonPara("tileText",f.format(c.getTime())));
- document.close();
- FileOutputStream fos=new FileOutputStream(filePath);
- fos.write(buffer.toByteArray());
- buffer.flush();
- buffer.close();
- fos.close();*/
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }
- return filePath;
- }
- /**
- * word中的表格
- * @param startDate
- * @param endDate
- * @return
- */
- public Table getTable(String startDate,String endDate,String deptId){
- Table table = null;
- try {
- table = new Table(10);
- table.setBorderWidth(1);
- table.setBorderColor(Color.black);
- table.setPadding(0);
- table.setSpacing(0);
-
- //时间,路网指数信息,备注
- Cell cell = getCell("姓名");
- cell.setHeader(true);
- table.addCell(cell);
- table.addCell(getCell("所属单位"));
- table.addCell(getCell("联系人手机"));
- table.addCell(getCell("机构类别"));
- table.addCell(getCell("联动部门"));
- table.addCell(getCell("联动联系人"));
- table.addCell(getCell("邮编"));
- table.addCell(getCell("办公OA邮箱"));
- table.addCell(getCell("地址"));
- table.addCell(getCell("网址"));
- table.endHeaders();
- SysModel sysmodel = ModelFactory.getSysmodel();
- SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
- 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+"'";
- try {
- List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
- if(tempList!=null&&tempList.size()>0){
- for(int i=0;i<tempList.size();i++){
- String[]arr=tempList.get(i);
- for(int j=0;j<arr.length;j++){
- table.addCell(arr[j]);
- }
- }
- }
- } catch (PersistenceException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (BadElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return table;
- }
- //表格的单元格
- public Cell getCell(String content){
- Cell cell = new Cell(content);
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- return cell;
- }
-
- // public static void main(String[] args) {
- // WordUtil wu=new WordUtil();
- // wu.
- // }
- //
- }
|