SSM中利用POI导出数据库到excel表格

本文介绍如何在Spring、SpringMVC和MyBatis(SSM)框架下,结合Apache POI库将数据库中的数据导出到Excel表格。主要涉及Controller层的处理和JSP视图的交互。

jsp:

<span><a href="#" onclick="export_data()">导出数据</a></span>
js:
<script type="text/javascript">
	function export_data(){
		window.location.href="${ctx}/swipeRecord/reprotRecord";
	}

</script>

controller:

/**
	 * 导出数据
	 * 
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/reprotRecord")
	public String reprotRecord(HttpServletResponse response) throws IOException {

		// 文件名称
		String fileName = URLEncoder.encode("刷卡记录.xls", "utf-8");
		// 通过response设置Content-Type、Content-Disposition
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename*=utf-8'zh_cn'" + fileName);
		
		//生成workBook
	//	HSSFWorkbook workbook = createWorkbook();
		
		OutputStream outputStream = null;
		HSSFWorkbook workBook = null;

		try {
			// 获取输出流
			outputStream = response.getOutputStream();
			// 生成workBook
			workBook = createWorkbook();
			workBook.write(outputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭
			if (outputStream!=null) {
				outputStream.close();
			}
		}

		return null;
	}
	public HSSFWorkbook createWorkbook() {
		// 创建表格
		HSSFWorkbook workBook = new HSSFWorkbook();
		// 创建工作簿
		HSSFSheet sheet = workBook.createSheet("我的工作簿01");
		// 样式
		HSSFCellStyle style = workBook.createCellStyle();
		// 创建Font
		HSSFFont font = workBook.createFont();
		// 设置字体
		font.setColor(HSSFFont.COLOR_NORMAL);
		style.setFont(font);

		// 创建行(表头)
		HSSFRow row = sheet.createRow(0);
		//创建列
		HSSFCell cell_01 = row.createCell(0);
		cell_01.setCellValue("id");
		cell_01.setCellStyle(style);
		HSSFCell cell_02 = row.createCell(1);
		cell_02.setCellValue("员工卡号");
		cell_02.setCellStyle(style);
		HSSFCell cell_03 = row.createCell(2);
		cell_03.setCellValue("持卡类型");
		cell_03.setCellStyle(style);
		HSSFCell cell_04 = row.createCell(3);
		cell_04.setCellValue("打卡时间");
		cell_04.setCellStyle(style);
		HSSFCell cell_05 = row.createCell(4);
		cell_05.setCellValue("手机号码");
		cell_05.setCellStyle(style);
		HSSFCell cell_06 = row.createCell(5);
		cell_06.setCellValue("图片");
		cell_06.setCellStyle(style);
		HSSFCell cell_07 = row.createCell(6);
		cell_07.setCellValue("时间");
		cell_07.setCellStyle(style);

		// 内容 真实环境查询数据库List,进行for遍历
		List<SwipeRecord> listSwipeRecord = swipeRecordService.listSwipeRecord();
		for (int i = 0; i < listSwipeRecord.size(); i++) {
			HSSFRow row1 = sheet.createRow(i+1);
			row1.setHeight((short) 300);
			SwipeRecord swipeRecord = listSwipeRecord.get(i);
			HSSFCell c1 = row1.createCell(0);
			c1.setCellValue(swipeRecord.getId());
			HSSFCell c2 = row1.createCell(1);
			c2.setCellValue(swipeRecord.getCardsNumber());
			HSSFCell c3 = row1.createCell(2);
			c3.setCellValue(swipeRecord.getCardType());
			HSSFCell c4 = row1.createCell(3);
			c4.setCellValue(swipeRecord.getSwipeTime());
			HSSFCell c5 = row1.createCell(4);
			c5.setCellValue(swipeRecord.getPhone());
			HSSFCell c6 = row1.createCell(5);
			c6.setCellValue(swipeRecord.getImage());
			HSSFCell c7 = row1.createCell(6);
			c7.setCellValue(swipeRecord.getRecordDate());
		}
		return workBook;

	}


评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值