使用poi导出excel并调整样式

上一篇实现了基本的导出功能,这一篇再给excel加点样式。在上一篇的基础上,修改代码如下:

	public String export() throws Exception {
		// 文件名
		String title = "单点登录信息";
		// 设置标题
		String headers[] = {"系统名称","IP地址","传输协议","所属单位","是否连通","录入时间"};
		// 获取数据库数据
	    List singleSignons = exportAll();
	    ExcelUtil.fillExcelData(singleSignons, headers, title, ServletActionContext.getResponse());
		return null;
	}

	/**
	 * 利用反射机制遍历对象的属性值,并放入单元格中,为excel添加样式
	 */
	public static void fillExcelData(List list, String[] headers, String title, 
			HttpServletResponse response) throws Exception {
		// 创建工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = wb.createSheet(title);
		// 设置表格默认宽度为15个字节
		sheet.setDefaultColumnWidth((short) 15);
		// 生成一个样式
		HSSFCellStyle style = wb.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.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);
		// 生成一个字体
		HSSFFont font = wb.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);
		// 生成并设置另一个样式
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 生成另一个字体
		HSSFFont font2 = wb.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		style2.setFont(font2);
				
		// 合并单元格0到headers.length - 1行
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
		
		// 标题
		int rowIndex = 0;
		HSSFRow row0 = sheet.createRow(rowIndex++);
		HSSFCell cell1 = row0.createCell(0);
		cell1.setCellStyle(style);  
        cell1.setCellValue(title); 
		for (short i = 1; i < headers.length; i++) {
			HSSFCell cell = row0.createCell(i);
			cell.setCellStyle(style);
		}
        
		// 产生表格标题行
		HSSFRow row = sheet.createRow(rowIndex++);
		for (short i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			cell.setCellValue(headers[i]);
		}
		// 产生表格内容行
		for(Object obj : list){
			// 利用反射得到对象的属性
			Class cls = obj.getClass();
			java.lang.reflect.Field[] flds = cls.getDeclaredFields(); // 得到私有属性
			row = sheet.createRow(rowIndex++);
			for(int j=0; j<headers.length; j++) {
					flds[j+1].setAccessible(true);
					HSSFCell cell = row.createCell(j);
					cell.setCellStyle(style2);
					cell.setCellValue((flds[j+1].get(obj)).toString());
			}
		}
		String fileName = title+".xls"; 
		response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1"));
		response.setContentType("application/ynd.ms-excel;charset=UTF-8");
		OutputStream out=response.getOutputStream();
		wb.write(out);
		out.flush();
		out.close();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值