码迷,mamicode.com
首页 > 其他好文 > 详细

POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

时间:2018-10-25 14:04:59      阅读:1255      评论:0      收藏:0      [点我收藏+]

标签:rap   main   osi   idt   pat   print   exception   run   except   

 

POI 生成word 文档 一般有两种方法:

① word模板 生成word 文档 ;

② 写代码直接生成 word 文档;

我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接代码;

 

/**
	 * 镇街日报导出word
	 */
	@RequestMapping(params = "exportWordForTownDaily")
	public void exportWordForTownDaily(HttpServletResponse response ,String selectDate){
		if(StringUtils.isNullOrEmpty(selectDate)){
			return;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			sdf.parse(selectDate);
		}catch (Exception e){
			e.printStackTrace();
			return;
		}
		CaseEntity entity = new CaseEntity();
		entity.setRepotingTime_begin(selectDate + " 00:00:00");
		entity.setRepotingTime_end(selectDate + " 23:59:59");
		//获取镇街实际扣分
		List<CaseEntity> caseEntityList = statisticsService.getTownActualScore(entity);
		//获取各镇街情况
		List<Map<String, Object>> townDaily = statisticsService.townDaily(entity);
		//格式化查询日期(年月日)
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
		try {
			selectDate = sdf2.format(sdf.parse(selectDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}

//------------------------------------生成word文档------------------------------------------------
		//创建document对象
		XWPFDocumentUtil document = new XWPFDocumentUtil();
		//创建标题段落
		XWPFParagraph titleParag = document.createParagraph();
		XWPFRun titleRun = titleParag.createRun();
		titleRun.setText("镇街日报");
		titleRun.setFontSize(20);
		titleRun.setBold(true);//字体是否加粗
		titleParag.setAlignment(ParagraphAlignment.LEFT);//段落居左

		//换行
        //XWPFParagraph brParagraph1 = document.createParagraph();
        //XWPFRun brRun = brParagraph1.createRun();
        //brRun.setText("\n");
        //创建一个段落
		XWPFParagraph p = document.createParagraph();
		int rows = caseEntityList.size()+1;
		int cols = 3;
		//创建一个表格
		XWPFTable table= document.createTable(rows, cols);
		//表格属性
		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
		//表格宽度
		CTTblWidth width = tablePr.addNewTblW();
		width.setW(BigInteger.valueOf(5000));
		//设置表格宽度为非自动
		width.setType(STTblWidth.DXA);

		//表头行
		XWPFTableRow headRow = table.getRow(0);
		XWPFTableCell headCell0 = headRow.getCell(0);
		XWPFTableCell headCell1 = headRow.getCell(1);
		XWPFTableCell headCell2 = headRow.getCell(2);

		p = headCell0.addParagraph();
		XWPFRun headRun0 = p.createRun();
		headRun0.setText("序号");
        headRun0.setFontSize(12);
		headRun0.setBold(true);//是否粗体
		headCell0.setColor("DEDEDE");
		//垂直居中
		p.setVerticalAlignment(TextAlignment.CENTER);
		//水平居中
		p.setAlignment(ParagraphAlignment.CENTER);

		p = headCell1.addParagraph();
		XWPFRun headRun1 = p.createRun();
		headRun1.setText("镇街");
        headRun1.setFontSize(12);
		headRun1.setBold(true);
		headCell1.setColor("DEDEDE");
		//垂直居中
		p.setVerticalAlignment(TextAlignment.CENTER);
		//水平居中
		p.setAlignment(ParagraphAlignment.CENTER);

		p = headCell2.addParagraph();
		XWPFRun headRun2 = p.createRun();
        headRun2.setFontSize(12);
		headRun2.setText("总扣分");
		headRun2.setBold(true);
		headCell2.setColor("DEDEDE");
		//垂直居中
		p.setVerticalAlignment(TextAlignment.CENTER);
		//水平居中
		p.setAlignment(ParagraphAlignment.CENTER);

		//表主体行
		for(int i=0 ;i<caseEntityList.size();i++){
			CaseEntity caseEntity = caseEntityList.get(i);
			XWPFTableRow contentRow = table.getRow(i+1);
			XWPFTableCell cell0 = contentRow.getCell(0);
			XWPFTableCell cell1 = contentRow.getCell(1);
			XWPFTableCell cell2 = contentRow.getCell(2);

			p = cell0.addParagraph();
			XWPFRun pRun0=p.createRun();
			pRun0.setText(i+1+"");
			//垂直居中
			cell0.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
			//水平居中
			p.setAlignment(ParagraphAlignment.CENTER);

			p = cell1.addParagraph();
			XWPFRun pRun1=p.createRun();
			pRun1.setText(caseEntity.getTownName());
			//垂直居中
			cell1.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
			//水平居中
			p.setAlignment(ParagraphAlignment.CENTER);

			p = cell2.addParagraph();
			XWPFRun pRun2=p.createRun();
			pRun2.setText(caseEntity.getTownActualScore().toString());
			//垂直居中
			cell2.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
			//水平居中
			p.setAlignment(ParagraphAlignment.CENTER);
		}
        //-----------------------------各镇街情况------------------------------
		//创建标题段落
		XWPFParagraph title1 = document.createParagraph();
		XWPFRun title1Run = title1.createRun();
		title1Run.setText("\r各镇街比较严重的问题");
		title1Run.setFontSize(20);
		title1Run.setBold(true);

		try {
			for(Map<String,Object> townMap:townDaily){
				CaseEntity townCase = (CaseEntity)townMap.get("townCase");
				XWPFParagraph xwpfParagraph = document.createParagraph();
				XWPFRun xwpfRun_town = xwpfParagraph.createRun();
				XWPFRun xwpfRun = xwpfParagraph.createRun();
				xwpfRun_town.setText(townCase.getTownName());
				xwpfRun_town.setFontSize(14);
				xwpfRun_town.setBold(true);
				xwpfRun_town.setColor("00cc44");
				xwpfRun.setText(","+selectDate+"当天扣分较多的问题有:");
				xwpfRun.setFontSize(14);
				xwpfRun.setBold(true);
				List<Map<String,Object>> caseInfoList = (List<Map<String,Object>>)townMap.get("caseInfoList");
                Integer no = 0;
				for(Map<String,Object> map:caseInfoList){
					CaseEntity caseInfo = (CaseEntity)map.get("caseInfo");
					XWPFParagraph xwpfParagraph1 = document.createParagraph();
					XWPFRun xwpfRun1 =xwpfParagraph1.createRun();
					XWPFRun xwpfRun2 = xwpfParagraph1.createRun();
					XWPFRun xwpfRun3 = xwpfParagraph1.createRun();
					xwpfRun1.setText((++no)+"、");
					xwpfRun2.setText(caseInfo.getProblemStreet());
					xwpfRun3.setText(","+caseInfo.getProblemNotes());
					xwpfRun1.setFontSize(14);
					xwpfRun2.setFontSize(14);
					xwpfRun2.setColor("4747d1");
					xwpfRun3.setFontSize(14);
					List<AttachmentEntity> fileList = (List<AttachmentEntity>)map.get("fileList");
					for(AttachmentEntity at:fileList){
						String mainPath = at.getMainPath();
						String path = at.getPath();
						if(!StringUtils.isNullOrEmpty(mainPath) && !StringUtils.isNullOrEmpty(path)){
							//第一张图片
							document.addPictureData(new FileInputStream(mainPath+path), XWPFDocumentUtil.PICTURE_TYPE_JPEG);
							document.createPicture(document.getAllPictures().size() - 1, 300, 400, document.createParagraph());
						}
					}
				}
			}

			OutputStream output = response.getOutputStream();
			//生成word文件的文件名
			String fileName= new String(("镇街日报.docx").getBytes("UTF-8"),"iso-8859-1");
			response.setHeader("Content-disposition", "attachment; filename=" + fileName);
			//把word文档写到输出流
			document.write(output);
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//--------------------------------word文档结束-----------------------------------------------------
	}

  

   效果如下图所示:

技术分享图片

 

技术分享图片

 

 

 

 

POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

标签:rap   main   osi   idt   pat   print   exception   run   except   

原文地址:https://www.cnblogs.com/linhuaming/p/9849052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!