poi导出Excel样式代码

		XSSFWorkbook workBook = new XSSFWorkbook();
		XSSFSheet sheet = workBook.createSheet();

		//样式
		Integer rowIndex = 0;
		CellRangeAddress ranage = new CellRangeAddress(0, 0, 0, 2); // 合并单元格
		sheet.addMergedRegion(ranage);
		sheet.setDefaultColumnWidth(20);// 设置单元格宽度
		sheet.setDefaultRowHeightInPoints(22);// 设置单元格高度
		XSSFRow sumRow = sheet.createRow(rowIndex);

		CellStyle style = workBook.createCellStyle(); // 样式对象1
		XSSFFont font = workBook.createFont();
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER); //水平
		style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂
		font.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index //字体颜色
		font.setFontHeightInPoints((short) 18);
		font.setFontName("宋体");
		style.setFont(font);

		CellStyle style2 = workBook.createCellStyle(); // 样式对象2
		XSSFFont font2 = workBook.createFont();
		style2.setAlignment(XSSFCellStyle.ALIGN_LEFT); //水平
		style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂
		font2.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index //字体颜色
		font2.setFontHeightInPoints((short) 11);
		font2.setFontName("宋体");
		style2.setFont(font2);

		//标题
		Cell cell = sumRow.createCell((short) 0);
		cell.setCellStyle(style);
		cell.setCellValue("标题");	
		rowIndex++;

		XSSFRow titleRow = sheet.createRow(rowIndex);
		cell = titleRow.createCell((short) 0);
		cell.setCellStyle(style2);
		cell.setCellValue("序号");

		cell = titleRow.createCell((short) 1);
		cell.setCellStyle(style2);
		cell.setCellValue("个人登记流水号");

		cell = titleRow.createCell((short) 2);
		cell.setCellStyle(style2);
		cell.setCellValue("姓名");
		rowIndex++;

		int index=1;
		for (UserDTO dto: list) {
		
			XSSFRow row = sheet.createRow(rowIndex);
			
			cell = row.createCell((short) 0);
			cell.setCellStyle(style2);
			cell.setCellValue(index);

			cell = row.createCell((short) 1);
			cell.setCellStyle(style2);
			cell.setCellValue(examUser.getExamUserId());

			cell = row.createCell((short) 2);
			cell.setCellStyle(style2);
			cell.setCellValue(examUser.getUserName());

			index++;
			rowIndex++;
		}

		ranage = new CellRangeAddress(rowIndex, rowIndex, 0, 2);
		sheet.addMergedRegion(ranage);
		sumRow = sheet.createRow(rowIndex);
		cell = sumRow.createCell((short) 0);
		cell.setCellStyle(style2);
		cell.setCellValue("导出机构:"+dto.get(0).getCreateOrganizationName());
		rowIndex++;

		ranage = new CellRangeAddress(rowIndex, rowIndex, 0, 2);
		sheet.addMergedRegion(ranage);
		sumRow = sheet.createRow(rowIndex);
		cell = sumRow.createCell((short) 0);
		cell.setCellStyle(style2);
		cell.setCellValue("导出时间:"+new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		rowIndex++;

		ranage = new CellRangeAddress(rowIndex, rowIndex, 0, 2);
		sheet.addMergedRegion(ranage);
		sumRow = sheet.createRow(rowIndex);
		cell = sumRow.createCell((short) 0);
		cell.setCellStyle(style2);
		cell.setCellValue("导出人:"+EasyUtils.getLoginUser(request).getUserName());

		//响应参数
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"export_" + System.currentTimeMillis() + ".xlsx\"");
		response.setContentType("application/force-download");

		//输出流文件
		ServletOutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
			outJson(errorInfo(ResponseCodeEnum.EXCEPTION_REQUEST.getCode()));
		}
		try {
			workBook.write(outputStream);
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}

你可能感兴趣的:(JAVA)