poi导出Excel样式代码

本文介绍了一个使用Java进行Excel文件导出的例子,详细展示了如何创建工作簿、设置样式、合并单元格及填充数据等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

		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();
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值