POI 生成 word 文檔 簡單版(包括文字、表格、圖片、字體樣式設置等)

 

POI 生成word 文檔 通常有兩種方法:java

① word模板 生成word 文檔 ;app

② 寫代碼直接生成 word 文檔;字體

我這裏演示的是第二種方法,即寫代碼生成 word文檔,很少說廢話,直接代碼;spa

 

/**
	 * 鎮街日報導出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文檔結束-----------------------------------------------------
	}

  

   效果以下圖所示:3d

 

相關文章
相關標籤/搜索