使用poi進行excel導出複雜表頭的設計

首先頁面請求進行導出服務層進行處理:

@ResponseBody     
	@RequestMapping(value="/exportCollegeDetailExcel.action")
	public String exportCollegeDetailExcel(String school_year,String batch_id,String stu_type,String college,String origin_id) {
		List pageList =  newstudentsCollegeStatisticsManager.collegeStatisticsDeatil_new(school_year,batch_id,stu_type,origin_id,college);
		try {
			String[] keys = new String[] {"college_name", "major_name", "should_report_num","should_report_man_num","should_report_woman_num", "reported_num","reported_man_num","reported_woman_num", "unreported_num","unreported_man_num","unreported_woman_num", "reported_percentage"};
			String[] headers = new String[] {"院系", "學生類型", "錄取人數","錄取男生人數","錄取女生人數", "報道人數","男生報道人數","女生報道人數", "未報到人數","未報到男生人數","未報到女生人數", "報到率"};
			HttpServletResponse response = getResponse();
			ExportExcel export = new ExportExcel();
			ExportExcel.prepareResponseHeader(getRequest(), response, "院系報到統計");
//			export.exportExcel("院系報到統計", headers, pageList, getResponse().getOutputStream(), keys);
			export.exportCollegeExcel("院系報到統計", headers, pageList, getResponse().getOutputStream(), keys);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

然後進行poi進行表格的處理:

//專門爲院系專業報到統計定製 
	public void exportCollegeMajorExcel(String sheetTitle, String[] headers,
			Collection<T> dataset, OutputStream out, Object... keyStrings) {
		exportCollegeMajorExcel(sheetTitle, headers, dataset, out, "yyyy-MM-dd", keyStrings);
	}

接下來就是數據表頭的設計處理:

我們以一下爲例的表格:

此處的表頭需要進行相應的處理合並。

@SuppressWarnings("deprecation")
		public void exportCollegeMajorClassesExcel(String title, String[] headers,
				Collection<T> dataset, OutputStream out, String pattern,
				Object... keyStrings) {
			index = 0;
			// 聲明一個工作薄
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 生成一個表格
			HSSFSheet sheet;
			int init = 65535;   // 單個excel最大數據量
			// 定義註釋的大小和位置,詳見文檔
			// HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
			// 0, 0, 0, (short) 4, 2, (short) 6, 5));
			// 設置註釋內容
			// comment.setString(new HSSFRichTextString("可以在POI中添加註釋!"));
			// 設置註釋作者,當鼠標移動到單元格上是可以在狀態欄中看到該內容.
			// comment.setAuthor("leno");

			// 產生表格標題行
			int indexTotal = 0;
			if(!Utility.isEmpty(dataset)) {
				indexTotal = dataset.size() / init
						+ (dataset.size() % init > 0 ? 1 : 0);
				for (int k = 0; k < indexTotal; k++) {
					index = 0;
					List<T> dataList = new ArrayList<T>();
					if(dataset.size() > init) {
						if(k != indexTotal - 1) {
							dataList = ((List)dataset).subList(k*init, (k+1)*init);
						} else {
							dataList = ((List)dataset).subList(k*init, dataset.size());
						}
					} else {
						dataList = (List)dataset;
					}
					
					sheet = workbook.createSheet(title+k);
					// 設置表格默認列寬度爲15個字節
					sheet.setDefaultColumnWidth((short) 15);
					// 聲明一個畫圖的頂級管理器
					HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
					if (headers != null && headers.length > 0) {

						// 生成一個樣式
						try {
							HSSFCellStyle style = workbook.createCellStyle();
							// 設置這些樣式
							 
                           style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
							style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
							style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
							style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
							style.setBorderRight(HSSFCellStyle.BORDER_THIN);
							style.setBorderTop(HSSFCellStyle.BORDER_THIN);
							style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
							style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
							// 生成一個字體
							HSSFFont font = workbook.createFont();
							font.setColor((short) 20);
							font.setFontHeightInPoints((short) 12);
							style.setWrapText(true);
							// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
							// 把字體應用到當前的樣式
							style.setFont(font);
//						HSSFRow row = sheet.createRow(index); 
							//進行標題行的設計
                            //這裏的表頭使用HSSFRichTextString
							HSSFRow rowTitle=sheet.createRow(0);
							HSSFCell cellNum=rowTitle.createCell((short)0);
							HSSFRichTextString text1 = new HSSFRichTextString("院系");
							cellNum.setCellValue(text1);
							cellNum.setCellStyle(style);
							HSSFCell cellName=rowTitle.createCell((short)1);
							HSSFRichTextString text2 = new HSSFRichTextString("專業");
							cellName.setCellValue(text2);
							cellName.setCellStyle(style);
							HSSFCell cellClass=rowTitle.createCell((short)2);
							HSSFRichTextString text25 = new HSSFRichTextString("班級");
							cellClass.setCellValue(text25);
							cellClass.setCellStyle(style);
							HSSFCell cellMsg=rowTitle.createCell((short)3);
							HSSFRichTextString text3 = new HSSFRichTextString("錄取情況");
							cellMsg.setCellValue(text3);
							cellMsg.setCellStyle(style);
							HSSFCell cellDept=rowTitle.createCell((short)6);
							HSSFRichTextString text4 = new HSSFRichTextString("報道情況");
							cellDept.setCellValue(text4);
							cellDept.setCellStyle(style);
							HSSFCell cellnotArrivied=rowTitle.createCell((short)9);
							HSSFRichTextString text5= new HSSFRichTextString("未報到情況");
							cellnotArrivied.setCellValue(text5);
							cellnotArrivied.setCellStyle(style);
							HSSFCell cellpersent=rowTitle.createCell((short)12);
							HSSFRichTextString text6= new HSSFRichTextString("報到率");
							cellpersent.setCellValue(text6);
							cellpersent.setCellStyle(style);
							
							//第二行兩列數據
							 HSSFRow rowAT=sheet.createRow(1);
							 HSSFCell cellS=rowAT.createCell((short)0);
							 HSSFRichTextString text20= new HSSFRichTextString("");
							 cellS.setCellValue(text20);
							 cellS.setCellStyle(style);
							 HSSFCell cellSs=rowAT.createCell((short)1);
							 HSSFRichTextString text21= new HSSFRichTextString("");
							 cellSs.setCellValue(text21);
							 cellSs.setCellStyle(style);
							 HSSFCell cellclass=rowAT.createCell((short)2);
							 HSSFRichTextString text26= new HSSFRichTextString("");
							 cellclass.setCellValue(text26);
							 cellclass.setCellStyle(style);
							 
							 HSSFCell cellSss=rowAT.createCell((short)3);
							 HSSFRichTextString text7= new HSSFRichTextString("錄取人數");
							 cellSss.setCellValue(text7);
							 cellSss.setCellStyle(style);
							 HSSFCell cellCard=rowAT.createCell((short)4);
							 HSSFRichTextString text8= new HSSFRichTextString("男生人數");
							 cellCard.setCellValue(text8);
							 cellCard.setCellStyle(style);
							 HSSFCell cellD=rowAT.createCell((short)5);
							 HSSFRichTextString text9= new HSSFRichTextString("女生人數");
							 cellD.setCellValue(text9);
							 cellD.setCellStyle(style);
							 HSSFCell cella=rowAT.createCell((short)6);
							 HSSFRichTextString text10= new HSSFRichTextString("報到人數");
							 cella.setCellValue(text10);
							 cella.setCellStyle(style);
							 HSSFCell cellb=rowAT.createCell((short)7);
							 HSSFRichTextString text11= new HSSFRichTextString("男生人數");
							 cellb.setCellValue(text11);
							 cellb.setCellStyle(style);
							 HSSFCell cellc=rowAT.createCell((short)8);
							 HSSFRichTextString text12= new HSSFRichTextString("女生人數");
							 cellc.setCellValue(text12);
							 cellc.setCellStyle(style);
							 HSSFCell celld=rowAT.createCell(9);
							 HSSFRichTextString text13= new HSSFRichTextString("未報到人數");
							 celld.setCellValue(text13);
							 celld.setCellStyle(style);
							 HSSFCell celle=rowAT.createCell((short)10);
							 HSSFRichTextString text14= new HSSFRichTextString("男生人數");
							 celle.setCellValue(text14);
							 celle.setCellStyle(style);
							 HSSFCell cellf=rowAT.createCell((short)11);
							 HSSFRichTextString text15= new HSSFRichTextString("女生人數");
							 cellf.setCellValue(text15);
							 cellf.setCellStyle(style);
	//這裏的CellRangAddress(a,b,c,d)其中是從a行b行,從c列到d列的合併單元格			
CellRangeAddress  region1=new  CellRangeAddress ((short)0,(short)1,(short)0,(short)0);
CellRangeAddress  region2=new  CellRangeAddress ((short)0,(short)1,(short)1,(short)1);
CellRangeAddress  region7=new  CellRangeAddress ((short)0,(short)1,(short)2,(short)2);
CellRangeAddress  region3=new  CellRangeAddress ((short)0,(short)0,(short)3,(short)5);
CellRangeAddress  region4=new  CellRangeAddress ((short)0,(short)0,(short)6,(short)8);
CellRangeAddress  region5=new  CellRangeAddress ((short)0,(short)0,(short)9,(short)11);
CellRangeAddress  region6=new  CellRangeAddress ((short)0,(short)1,(short)12,(short)12);
							sheet.addMergedRegion(region1);
							sheet.addMergedRegion(region2);
							sheet.addMergedRegion(region3);
							sheet.addMergedRegion(region4);
							sheet.addMergedRegion(region5);
							sheet.addMergedRegion(region6);
							sheet.addMergedRegion(region7);
						//注意此爲標題的行數,一定要寫。
							index=2;
							
						} catch (Exception e) {
							e.printStackTrace();
						}    
					}	 
					// 遍歷集合數據,產生數據行,此處是循環顯示數據行的
					putDateset2Rows(dataList, pattern, workbook, sheet, patriarch,
							keyStrings);
				}
			} else {
				sheet = workbook.createSheet(title);
				// 設置表格默認列寬度爲15個字節
				sheet.setDefaultColumnWidth((short) 15);
				// 聲明一個畫圖的頂級管理器
				HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
				if (headers != null && headers.length > 0) {

					// 生成一個樣式
					HSSFCellStyle style = workbook.createCellStyle();
					// 設置這些樣式
					style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
					style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
					style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
					style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
					style.setBorderRight(HSSFCellStyle.BORDER_THIN);
					style.setBorderTop(HSSFCellStyle.BORDER_THIN);
					style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
					// 生成一個字體
					HSSFFont font = workbook.createFont();
					font.setColor((short) 20);
					font.setFontHeightInPoints((short) 12);
					style.setWrapText(true);
					// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
					// 把字體應用到當前的樣式
					style.setFont(font);

					HSSFRow row = sheet.createRow(index);
					for (short i = 0; i < headers.length; i++) {
						HSSFCell cell = row.createCell(i);
						cell.setCellStyle(style);
						HSSFRichTextString text = new HSSFRichTextString(headers[i]);
						cell.setCellValue(text);
					}
					//index++;
				}
				// 遍歷集合數據,產生數據行
				putDateset2Rows(dataset, pattern, workbook, sheet, patriarch,
						keyStrings);
			}
			

			try {
				workbook.write(out);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

 

導出效果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章