Java處理數據導出功能

一:思路
前提:首先要獲取要導出的數據列表(一般都是通過請求查詢條件從數據庫中獲取數據);然後調用封裝好的生成Excel文件方法,在業務層中渲染文件中的數據列表。
二:步驟
1.導入使用的 jxl.jar ;
在頁面定義“導出”按鈕,發送導出功能請求;
在控制層中,獲取要導出的數據集,傳入文件名,執行生成Excel文件方法;

@RequestMapping("/exportChkinm")
@ResponseBody
public boolean exportChkinm(HttpServletResponse response, HttpServletRequest request, HttpSession session, Chkinm chkinm, String checkOrNot, Page page, Date madedEnd) throws Exception{

    //獲取數據      
    List<Chkinm> chkinmList = chkinmService.findAllChkinm(checkOrNot, chkinm, page, madedEnd, session.getAttribute("locale").toString());
    //文件名
    String fileName = "已審覈入庫單彙總";
    //執行方法
    setExpArg(response, request, fileName);
    //返回文件
    return chkinmService.exportRUChkinm(response.getOutputStream(), chkinmList);
}
//生成Excel文件方法
public void setExpArg(HttpServletResponse response,HttpServletRequest request,String fileName) throws UnsupportedEncodingException {
        response.setContentType("application/msexcel; charset=UTF-8");
        if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
            //IE
            fileName = URLEncoder.encode(fileName, "UTF-8");
        }else{
            fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
        }
        response.setHeader("Content-disposition", "attachment; filename="+ fileName + ".xls");
}

2.渲染文件數據
在業務層中

public boolean exportRUChkinm(ServletOutputStream outputStream, List<Chkinm> chkinmList) {
    //獲取工作薄
    WritableWorkbook workBook = null;
    //設置工作簿樣式
    WritableFont titleFont = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        WritableCellFormat titleStyle = new WritableCellFormat(titleFont);
        try {
            titleStyle.setAlignment(Alignment.CENTRE);
            workBook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workBook.createSheet("已審覈入庫單彙總導出表", 0);
            sheet.addCell(new Label(0, 0, "已審覈入庫單彙總導出表", titleStyle));
            sheet.mergeCells(0, 0, 11, 0);
            sheet.addCell(new Label(0, 1, "入庫單號"));
            sheet.addCell(new Label(1, 1, "憑證號"));
            sheet.addCell(new Label(2, 1, "單據類型"));
            sheet.addCell(new Label(3, 1, "制單日期"));
            sheet.addCell(new Label(4, 1, "制單時間"));
            sheet.addCell(new Label(5, 1, "入庫倉位"));
            sheet.addCell(new Label(6, 1, "供應商"));
            sheet.addCell(new Label(7, 1, "總金額"));
            sheet.addCell(new Label(8, 1, "稅前總金額"));
            sheet.addCell(new Label(9, 1, "制單人"));
            sheet.addCell(new Label(10, 1, "審覈人"));
            //遍歷list填充表格內容
            int index = 1;
            for(int j=0; j<chkinmList.size(); j++){
                if(j == chkinmList.size()){
                    break;
                }
                index += 1;
                 sheet.addCell(new Label(0, index, chkinmList.get(j).getChkinno().toString()));
                 sheet.addCell(new Label(1, index, chkinmList.get(j).getVouno()));
                 sheet.addCell(new Label(2, index, chkinmList.get(j).getTyp()));
                 sheet.addCell(new Label(3, index, new SimpleDateFormat("yyyy-MM-dd").format(chkinmList.get(j).getMaded())));
                 sheet.addCell(new Label(4, index, chkinmList.get(j).getMadet()));
                 sheet.addCell(new Label(5, index, String.valueOf(chkinmList.get(j).getPositn().getDes())));
                 sheet.addCell(new Label(6, index, String.valueOf(chkinmList.get(j).getDeliver().getDes())));
                 sheet.addCell(new Label(7, index, Float.toString(chkinmList.get(j).getTotalamt())));
                 sheet.addCell(new Label(8, index, Double.toString(chkinmList.get(j).getNoTaxTotalamt())));
                 sheet.addCell(new Label(9, index, chkinmList.get(j).getMadeby()));
                 sheet.addCell(new Label(10, index,chkinmList.get(j).getChecby()));
            }
            workBook.write();
            outputStream.flush();
        } catch (WriteException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(workBook!=null){
                   workBook.close();
                }
                outputStream.close();
            } catch (WriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

這裏寫圖片描述

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