1. 程式人生 > >java查詢結果後並下載輸出到excel(OutputStream)

java查詢結果後並下載輸出到excel(OutputStream)

1、首先查詢出所有的資料,使用spring data jpa查詢

public Page<Employee> pageQuery(PageRequest pageRequest) {
		return employeeDao.findAll(pageRequest);
	}
2、查詢出來後匯出,設定檔名、檔案型別、輸出流及檔案格式
@Action(value="employeeAction_download")
	public String download(){
		try {
			Page<Employee> list =  facadeService.getEmployeeService().pageQuery(getPageRequest());
			//使用poi下載檔案
			HSSFWorkbook workbook = new HSSFWorkbook();
			//建立sheet
			HSSFSheet sheet1 = workbook.createSheet("分割槽資訊一");
			//建立row資訊
			HSSFRow row = sheet1.createRow(0);
			//建立單元格頭標
			row.createCell(0).setCellValue("編號");
			row.createCell(1).setCellValue("姓名");
			row.createCell(2).setCellValue("性別");
			row.createCell(3).setCellValue("入職日期");
			row.createCell(4).setCellValue("出生年月");
			row.createCell(5).setCellValue("戶籍所在地");
			row.createCell(6).setCellValue("畢業院校");
			row.createCell(7).setCellValue("職務");
			row.createCell(8).setCellValue("社保賬號");
			row.createCell(9).setCellValue("公積金賬號");
			row.createCell(10).setCellValue("電話號碼");
			row.createCell(11).setCellValue("是否離職");
			//獲取資料
			if (list != null && list.getSize() != 0) {
				for (Employee e : list) {
					int lastRowNum = sheet1.getLastRowNum();
					HSSFRow lastRow = sheet1.createRow(lastRowNum + 1);
					lastRow.createCell(0).setCellValue(e.getId());
					lastRow.createCell(1).setCellValue(e.getName());
					lastRow.createCell(2).setCellValue(e.getSex());
					lastRow.createCell(3).setCellValue(e.getEntryday());
					lastRow.createCell(4).setCellValue(e.getBirthday());
					lastRow.createCell(5).setCellValue(e.getRegister());
					lastRow.createCell(6).setCellValue(e.getPosition());
					lastRow.createCell(7).setCellValue(e.getSchool());
					lastRow.createCell(8).setCellValue(e.getSocialsecurity());
					lastRow.createCell(9).setCellValue(e.getPublicfund());
					lastRow.createCell(10).setCellValue(e.getTelephone());
					lastRow.createCell(11).setCellValue(e.getDeltag());
				}
			}
			//response檔案流
			HttpServletResponse response = ServletActionContext.getResponse();
			//設定檔名
			String filename = "中喜員工資訊.xls";
			//設定檔案輸出頭
			response.setHeader("Content-Disposition", "attachment;filename="+MyFileUtils.encodeDownloadFilename(filename, getRequest().getHeader("user-agent")));
			//設定檔案型別servletAction.getMine
			ServletContext servletContext = ServletActionContext.getServletContext();
			response.setContentType(servletContext.getMimeType(filename));
			//下載輸出流
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		//下載不需要頁面跳轉
		return NONE;
	}
3、在這裡出現的一個問題是我設定的每頁顯示10行資料,匯出來的資料也只有10行
//獲取分頁物件  接受頁面的頁碼和每頁顯示記錄
	protected int pageNum;//頁碼
	protected int pageSize = 10;//每頁顯示數量
	public void getPageNum(int pageNum){
		this.pageNum = pageNum;
	}
	public void getPageSize(int pageSize){
		this.pageSize = pageSize;
	}

4、為解決這個問題我採用的是輸出檔案前重新定義一個查詢資料的頁面設定,程式碼冗餘了,但是問題可以解決,歡迎留言提出新的解決方案~~~

5、對於各個不同瀏覽器的文字編碼不同,建立工具類

public class MyFileUtils {
	/**
	 * 下載檔案時,針對不同瀏覽器,進行附件名的編碼
	 * @param filename下載檔名
	 * @param agent客戶端瀏覽器
	 * @return 編碼後的下載附件名
	 * @throws IOException
	 */
	public static String encodeDownloadFilename(String filename, String agent) throws IOException {
		if (agent.contains("Firefox")) { // 火狐瀏覽器
			filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";
			filename = filename.replaceAll("\r\n", "");
		} else { // IE及其他瀏覽器
			filename = URLEncoder.encode(filename, "utf-8");
			filename = filename.replace("+", " ");
		}
		return filename;
	}
}