java中使用jxl導出Excel表格詳細通用步驟


該方法一般接收兩個參數,response和要導出的表格內容的list.

一般我們將數據庫的數據查詢出來在頁面進行展示,根據用戶需求,可能需要對頁面數據進行導出.

此時只要將展示之前查詢所得的數據放入session中備份一份,在調用導出方法時,從session中獲取即可,

如果為后台直接導出,直接查詢數據庫后將結果傳入即可,當然也可以在導出Excel方法中查詢.

查詢方法: 

  // 獲取查詢結果存入session中
        Object resultList = request.getAttribute("listItems");//傳入前台展示的查詢結果集
        request.getSession().setAttribute("resultList", resultList);

導出Excel方法:

   // 從session中獲取查詢結果集
        List<TTmoasCoNorecordInfo> resultList = (List<TTmoasCoNorecordInfo>) request.getSession().getAttribute("resultList");

        // 調用導出excel方法,傳入response,和resultList
        xsdwglService.downloadExcel(response, resultList);

downloadExcel如下:

    @Override
    public void downloadExcel(HttpServletResponse response, List<TTmoasCoNorecordInfo> list) {

        // 創建工作表
        WritableWorkbook book = null;
        response.reset();

        response.setCharacterEncoding("UTF-8");// 設置字符集

        // 創建工作流
        OutputStream os = null;
        try {

            // 設置彈出對話框
            response.setContentType("application/DOWLOAD");
            response.setCharacterEncoding("UTF-8");

            // 設置工作表的標題
            response.setHeader("Content-Disposition", "attachment; filename=Norecord_Social_Credit_Code.xls");// 設置生成的文件名字
            os = response.getOutputStream();

            // 初始化工作表
            book = Workbook.createWorkbook(os);

        } catch (IOException e1) {

            logger.error("導出excel出現IO異常", e1);
            throw new ServiceException("導出失敗", e1);
        }
        try {

            // 以下為excel表格內容
            // int nCount = list.size();
            WritableSheet sheet = book.createSheet("社信代無關聯及名稱不一致保存結果", 0);

            // 生成名工作表,參數0表示這是第一頁
            // int nI = 1;

            // 表字段名
            sheet.addCell(new jxl.write.Label(0, 0, "序號"));
            sheet.addCell(new jxl.write.Label(1, 0, "名稱(查詢)"));
            sheet.addCell(new jxl.write.Label(2, 0, "經營狀態"));
            sheet.addCell(new jxl.write.Label(3, 0, "名稱(所填企業名)"));
            sheet.addCell(new jxl.write.Label(4, 0, "統一社會信用代碼"));
            sheet.addCell(new jxl.write.Label(5, 0, "申請日期"));

            // 將數據追加
            for (int i = 1; i < list.size() + 1; i++) {

                sheet.addCell(new jxl.write.Label(0, i, String.valueOf(i)));// 序號從1開始
                sheet.addCell(new jxl.write.Label(1, i, list.get(i - 1).getEntname()));
                sheet.addCell(new jxl.write.Label(2, i, list.get(i - 1).getEntstatus()));
                sheet.addCell(new jxl.write.Label(3, i, list.get(i - 1).getUserName()));
                sheet.addCell(new jxl.write.Label(4, i, list.get(i - 1).getAppCertificateNum()));

                // 設置日期格式
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                Date appDate = list.get(i - 1).getAppDate();
                String appDateStr = sf.format(appDate);
                sheet.addCell(new jxl.write.Label(5, i, appDateStr));// 申請日期
            }
            book.write();
            book.close();
        } catch (Exception e) {
            logger.error("導出excel出現異常", e);
            throw new ServiceException("導出失敗", e);
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.error("關流出現異常", e);
                    e.printStackTrace();
                }
            }
        }
    }

這樣就可以導出Excel表格了.

需要設置多個Sheet/合並單元格等,根據相應API設置即可.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM