javaweb使用poi下載excel設置樣式、合併單元格、設置列寬

@Override
	public void exportMajorInfo(@RequestBody StudentInfoDto studentInfoDto) {
		// 至少存在4列
		int miniCell = 4;
		// 查詢有多少個年級存在在校人數
		List<String> grades = studentStatusMapper.getOnSchoolGrade();
		// 構建excel
		HSSFWorkbook workbook = new HSSFWorkbook();
		//2.創建工作表
		HSSFSheet sheet = workbook.createSheet("在校專業人數");
		String fileName = "*****學院在校生專業人數--預覽.xls";
		// 設置默認列寬
		setExcelColumnWidth(sheet);
		// 年級集合是否存在數據,不存在數據之間返回
		if (CollectionUtils.isNotEmpty(grades)) {
			// 創建excel頭標題內容
			createExcelHeader(workbook, miniCell, sheet, grades, setExcelStyle(workbook, 12));
			// 創建內容
			createExcelContent(miniCell, sheet, grades, setExcelStyle(workbook, 10));
		}
		response.setContentType("application/octet-stream;charset=utf-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName));
			workbook.write(response.getOutputStream());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 設置excel列寬
	 * @param sheet
	 */
	private void setExcelColumnWidth(HSSFSheet sheet) {
		sheet.setDefaultColumnWidth(12);
		// y = 256*width+184
		sheet.setColumnWidth(0, 256 * 25 + 184);
		sheet.setColumnWidth(2, 256 * 30 + 184);
	}

	/**
	 * 設置樣式
	 * @param workbook
	 * @param fontSize
	 * @return
	 */
	private HSSFCellStyle setExcelStyle(HSSFWorkbook workbook, int fontSize) {
		HSSFCellStyle style = createCellStyle(workbook,(short)fontSize,false,true);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		return style;
	}

	/**
	 * 創建在校學生內容
	 * @param miniCell
	 * @param sheet
	 * @param grades
	 * @param headStyle
	 */
	private void createExcelContent(int miniCell, HSSFSheet sheet, List<String> grades, HSSFCellStyle headStyle) {
		// 獲取二級學院集合
		List<StudentStatusVo> departments = studentStatusMapper.getDepartmentList();
		if (CollectionUtils.isNotEmpty(departments)) {
			int index = 3;
			// 查詢獲取專業
			for (int i = 0; i < departments.size(); i++) {
				List<StudentStatusVo> majors = studentStatusMapper.getMajorList(departments.get(i).getStudentDepartment());
				if (CollectionUtils.isEmpty(majors)) {
					continue;
				}
				// 創建一個學院的行列數據
				HSSFRow row = sheet.createRow(index);
				CellRangeAddress cellRangeAddress = new CellRangeAddress(index, index + majors.size() - 1,0,0);//起始行,結束行,起始列,結束列
				sheet.addMergedRegion(cellRangeAddress);
				HSSFCell department = row.createCell(0);
				department.setCellStyle(headStyle);
				department.setCellValue(departments.get(i).getStudentDepartmentName());
				for (int j = 0; j < majors.size(); j++) {
					// 創建一個學院下專業的行列數據
					HSSFRow majorRow = j == 0 ? row : sheet.createRow(index + j);
					// 空白格填充
					if (j != 0) {
						HSSFCell nullCell = majorRow.createCell(0);
						nullCell.setCellStyle(headStyle);
					}
					// 序號
					HSSFCell noCell = j == 0 ? row.createCell(1) : majorRow.createCell(1);
					noCell.setCellStyle(headStyle);
					noCell.setCellValue(index + j - 2);
					// 專業名稱
					HSSFCell majorCell = j == 0 ? row.createCell(2) : majorRow.createCell(2);
					majorCell.setCellStyle(headStyle);
					majorCell.setCellValue(majors.get(j).getMajorName());
					// 班級人數
					HSSFCell classCell = j == 0 ? row.createCell(3) : majorRow.createCell(3);
					classCell.setCellStyle(headStyle);
					classCell.setCellValue(studentStatusMapper.getClassNum(majors.get(j).getMajorId(), grades));
					int atSchool = 0;
					for (int k = 0; k < grades.size(); k++) {
						int gradeNum = studentStatusMapper.getAtSchoolNum(majors.get(j).getMajorId(), grades.get(k));
						atSchool = atSchool + gradeNum;
						// 年級在校人數
						HSSFCell atSchoolCell = j == 0 ? row.createCell(miniCell + k) : majorRow.createCell(miniCell + k);
						atSchoolCell.setCellStyle(headStyle);
						atSchoolCell.setCellValue(gradeNum);
						if (k + 1 == grades.size()) {
							HSSFCell atSchoolTotalCell = j == 0 ? row.createCell(miniCell + k + 1) : majorRow.createCell(miniCell + k + 1);
							atSchoolTotalCell.setCellStyle(headStyle);
							atSchoolTotalCell.setCellValue(atSchool);
						}
					}
					int totalNum = 0;
					for (int k = 0; k < grades.size(); k++) {
						int gradeNum = studentStatusMapper.getTotalSchoolNum(majors.get(j).getMajorId(), grades.get(k));
						totalNum = totalNum + gradeNum;
						// 年級總共人數
						HSSFCell atSchoolCell = j == 0 ? row.createCell(miniCell + k + grades.size() + 1) : majorRow.createCell(miniCell + k + grades.size() + 1);
						atSchoolCell.setCellStyle(headStyle);
						atSchoolCell.setCellValue(gradeNum);
						if (k + 1 == grades.size()) {
							HSSFCell atSchoolTotalCell = j == 0 ? row.createCell(miniCell + k + grades.size() + 2) : majorRow.createCell(miniCell + k + grades.size() + 2);
							atSchoolTotalCell.setCellStyle(headStyle);
							atSchoolTotalCell.setCellValue(totalNum);
						}
					}
				}
				index = index + majors.size();
			}
		}
	}

	/**
	 * 創建excel頭
	 * @param workbook
	 * @param miniCell
	 * @param sheet
	 * @param grades
	 * @param headStyle
	 */
	private void createExcelHeader(HSSFWorkbook workbook, int miniCell, HSSFSheet sheet, List<String> grades, HSSFCellStyle headStyle) {
		// 創建第一行及內容
		HSSFRow row = sheet.createRow(0);
		// 創建第一行第一列 (主題)
		CellRangeAddress titleRange = new CellRangeAddress(0, 0,0,miniCell + grades.size() * 2 + 1);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(titleRange);
		HSSFCell excelTitle = row.createCell(0);
		// 加載單元格樣式
		excelTitle.setCellStyle(setExcelStyle(workbook, 15));
		excelTitle.setCellValue("*****學院在校生專業人數--預覽(截止至" + new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + ")");

		// 創建第二行
		HSSFRow title = sheet.createRow(1);
		// 創建第二行第一列 (二級學院)
		CellRangeAddress departmentRange = new CellRangeAddress(1, 2,0,0);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(departmentRange);
		HSSFCell department = title.createCell(0);
		department.setCellStyle(headStyle);
		department.setCellValue("二級學院");

		// 創建第二行第二列 (序號)
		CellRangeAddress noRange = new CellRangeAddress(1, 2,1,1);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(noRange);
		HSSFCell no = title.createCell(1);
		no.setCellStyle(headStyle);
		no.setCellValue("序號");

		// 創建第二行第三列 (專業名稱)
		CellRangeAddress majorRange = new CellRangeAddress(1, 2,2,2);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(majorRange);
		HSSFCell major = title.createCell(2);
		major.setCellStyle(headStyle);
		major.setCellValue("專業名稱");

		// 創建第二行第四列 (班級數)
		CellRangeAddress classRange = new CellRangeAddress(1, 2,3,3);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(classRange);
		HSSFCell classCell = title.createCell(3);
		classCell.setCellStyle(headStyle);
		classCell.setCellValue("班級數");

		// 創建第二行(在校人數)
		CellRangeAddress atSchool = new CellRangeAddress(1, 1,miniCell,miniCell + grades.size());//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(atSchool);
		HSSFCell atSchoolCell = title.createCell(miniCell);
		atSchoolCell.setCellStyle(headStyle);
		atSchoolCell.setCellValue("在校人數");

		// 創建第二行(總人數)
		CellRangeAddress totalNum = new CellRangeAddress(1, 1,miniCell + grades.size() + 1,miniCell + grades.size() * 2 + 1);//起始行,結束行,起始列,結束列
		sheet.addMergedRegion(totalNum);
		HSSFCell totalNumCell = title.createCell(miniCell + grades.size() + 1);
		totalNumCell.setCellStyle(headStyle);
		totalNumCell.setCellValue("總人數");
		// 填充空白格格式
		for (int i = miniCell + 1; i <= grades.size() + miniCell; i++) {
			HSSFCell nullCell = title.createCell(i);
			nullCell.setCellStyle(headStyle);
		}
		for (int i = grades.size() + miniCell + 2; i <= grades.size() * 2 + miniCell + 1; i++) {
			HSSFCell nullCell = title.createCell(i);
			nullCell.setCellStyle(headStyle);
		}

		// 創建第三行及內容
		HSSFRow thirdRow = sheet.createRow(2);
		// 填充空白格格式
		for (int i = 0; i < miniCell; i++) {
			HSSFCell nullCell = thirdRow.createCell(i);
			nullCell.setCellStyle(headStyle);
		}
		for (int i = 0; i < grades.size(); i++) {
			HSSFCell gradeCell = thirdRow.createCell(miniCell + i);
			gradeCell.setCellStyle(headStyle);
			gradeCell.setCellValue(grades.get(i) + "級");
			if (i + 1 == grades.size()) {
				HSSFCell majorTotalCell = thirdRow.createCell(miniCell + i + 1);
				majorTotalCell.setCellStyle(headStyle);
				majorTotalCell.setCellValue("專業總人數");
			}
		}
		for (int i = 0; i < grades.size(); i++) {
			HSSFCell gradeCell = thirdRow.createCell(miniCell + i + grades.size() + 1);
			gradeCell.setCellStyle(headStyle);
			gradeCell.setCellValue(grades.get(i) + "級");
			if (i + 1 == grades.size()) {
				HSSFCell majorTotalCell = thirdRow.createCell(miniCell + i + grades.size() + 2);
				majorTotalCell.setCellStyle(headStyle);
				majorTotalCell.setCellValue("專業總人數");
			}
		}
	}

	/**
	 *
	 * @param workbook
	 * @param fontsize
	 * @return 單元格樣式
	 */
	private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize,boolean flag,boolean flag1) {
		// TODO Auto-generated method stub
		HSSFCellStyle style = workbook.createCellStyle();
		//是否水平居中
		if(flag1){
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
		}

		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
		//創建字體
		HSSFFont font = workbook.createFont();
		//是否加粗字體
		if(flag){
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		}
		font.setFontHeightInPoints(fontsize);
		//加載字體
		style.setFont(font);
		return style;
	}

合併單元格使用

CellRangeAddress departmentRange = new CellRangeAddress(1,2,0,0);//起始行,結束行,起始列,結束列
sheet.addMergedRegion(departmentRange);

設置列寬

sheet.setDefaultColumnWidth(12); // 默認列寬
// y = 256*width+184
sheet.setColumnWidth(0, 256 * 25 + 184); // 每列寬度
sheet.setColumnWidth(2, 256 * 30 + 184);

設置水平居中、垂直居中、字體大小、是否加粗

HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//創建字體
HSSFFont font = workbook.createFont();
//加粗字體
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints(fontsize);
//加載字體
style.setFont(font);

實現圖爲:

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