POI實現EXCEL文件的下載

參考代碼

@GET
	@Path("/downloadTemplate")
	@Produces("*/*")
	public String downloadTemplate(){
		System.out.println("請進!");
		try {
			Message message = PhaseInterceptorChain.getCurrentMessage();
			HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
			String templatePath = request.getServletContext().getRealPath("/") + "\\make\\xlsprint\\";
			//通過流打開模板grade.xls
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(templatePath + "userTemplate.xls")));
			//打開一個工作表
			HSSFSheet sheet = wb.getSheetAt(0);
			HSSFRow nRow = null;
			HSSFCell nCell = null;
			//設置大標題
			nRow = sheet.getRow(0);
			nCell = nRow.getCell(1);
			nCell.setCellStyle(nCell.getCellStyle());
			// 生成流對象
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
			// 將excel寫入流
	        wb.write(byteArrayOutputStream);
	        // 利用工具類DownloadBase,封裝彈出下載框
	        String outFileName = "用戶信息表.xls";
	        DownloadBase down = new DownloadBase();
	        //獲取response對象
	        HttpServletResponse response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
	        down.download(byteArrayOutputStream, response, outFileName);
		} catch (Exception e) {
			return StringParam.returnPageFalse();
		}
		return StringParam.returnPageTrue();
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章