1. 程式人生 > >Javaweb專案,簡單應用Apache POI匯出Excel的簡單例子

Javaweb專案,簡單應用Apache POI匯出Excel的簡單例子

直接上程式碼:

jsp:

說明:這裡使用ajax請求是會有問題的,會導致瀏覽器視窗不會彈出下載提示和選擇位址列的彈窗

 //匯出
 $('#btn-export').click(function () {        
 location.href = "${pageContext.request.contextPath}/exportExcel?key=key&name=name";
 })

Controller:

@RequestMapping(value = "exportExcel", produces = "application/json;charset=UTF-8")
@ResponseBody
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HSSFWorkbook wb;
//宣告文字格式
		wb= exportExcelService.export();
//呼叫Service層的方法
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String string=sdf.format(date);
//以上內容只是為了當檔案下載時,能夠有一串按系統當前時間生成的數字作為命名
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition",                       "attachment;filename="+string+".xls");
//.xsl是檔案下載的格式,該一整句就是一個宣告,傳送給web端的
		OutputStream ouputStream = response.getOutputStream();
		wb.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}

Service:

public HSSFWorkbook export() {
        HSSFWorkbook wb = new HSSFWorkbook();			//關鍵語句
        Sheet sheet = null;		//工作表物件
        Row nRow = null;		//行物件

        List<xxxxView> list = xxxxViewXmlDao.findxxxxList(new xxxxView());
        //因為我用的是資料庫查詢資料,然後返回一個list物件

        int rowNo = 0;		//總行號
        int pageRowNo = 0;	//頁行號

        for (int i = 0; i < list.size(); i++) {
            if(rowNo%60000==0){
                System.out.println("Current Sheet:" + rowNo/60000);
                sheet = wb.createSheet("sheet"+(rowNo/60000));//建立新的sheet物件
                sheet = wb.getSheetAt(rowNo/60000);		//動態指定當前的工作表
                pageRowNo = 0;		//每當新建了工作表就將當前工作表的行號重置為0
            }

            rowNo++;
            nRow = sheet.createRow(pageRowNo++);	//新建行物件


            if (rowNo%60000==1 || i == 0){   //該作用是再每張sheet的表格第一行寫下中文欄
                nRow.createCell(0).setCellValue("ID");
                nRow.createCell(1).setCellValue("編號");
                nRow.createCell(2).setCellValue("名稱");
                nRow.createCell(3).setCellValue("英文名稱");
                nRow.createCell(4).setCellValue("助記碼");
                nRow.createCell(5).setCellValue("狀態");
                nRow.createCell(6).setCellValue("變更時間");
                nRow = sheet.createRow(pageRowNo++);
            }

            xxxxView View = list.get(i); //將資料庫中反出的資料轉換為物件

            nRow.createCell(0).setCellValue(View.getid());
            nRow.createCell(1).setCellValue(View.getbh());
            nRow.createCell(2).setCellValue(View.getmc());
            nRow.createCell(3).setCellValue(View.getywmc());
            nRow.createCell(4).setCellValue(View.getZjm());
            nRow.createCell(5).setCellValue(View.getState());
            nRow.createCell(6).setCellValue(View.getDate());

            //以上是將物件插入到每行中的每一個文字框中

        }
        return wb;
    }

這裡需要注意,上面之所以滿60000資料就新建一張sheet,是因為xls檔案有個單頁sheet有個行數上限,最多65536行,如果不換頁,匯出資料是會報異常的