1. 程式人生 > >SSM中利用POI匯出資料庫到excel表格

SSM中利用POI匯出資料庫到excel表格

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;

	}