Java實現導出Excel功能

剛寫瞭個導入Excel表格,現在來寫個導出,其實形式都差不多,廢話不多說,貼代碼

<div>
      <button type="button" class="btn-btn" @click="exportData()">導出</button>
 </div>

這裡根據個人需求,我這裡寫時間設置,根據條件去請求導出,這裡因人而異

exportData(){
                //判斷用戶有無填寫操作類型
                if (this.type != "") {
                    //默認全部操作類型,不傳參
                    this.param["code"] = this.code;
                }
                //判斷用戶有無填寫開始日期
                if (this.startDate != '') {
                    this.param['statrDate'] = new Date(this.startDate);
                }
                //判斷用戶有無填寫結束日期
                if (this.endDate != '') {
                    this.param['endDate'] = new Date(this.endDate);
                }
                this.param[this.optionSelected] = this.param.searchText;
                var url = "${ctx}/operatelog/exportOperateLog";
                window.open(url);
            }

java中Controller層,來接受請求,數據庫查詢到的數據進行封裝,然後使用ExcelUtils進行輸出。

@RequestMapping("/exportOperateLog")
    @ResponseBody
    public void exportOperateLog(HttpServletRequest request, HttpServletResponse response, OperateLogParam param) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            response.reset();
            response.setContentType("text/html;charset=GBK");// 設置response內容的類型
            response.setHeader("Content-disposition", "attachment;filename=SysteamLog" + dateFormat.format(new Date()) + ".xls;");
            String strTableName = "系統日志"; //這個是標題
            List<String> headers = new ArrayList<>();
            headers.add("序號"); //這裡的表頭,根據數據的字段命名也行,隨你喜歡
            headers.add("操作日期時間");
            headers.add("操作類型");
            headers.add("數據1");
            headers.add("數據2");
            headers.add("操作員id");
            headers.add("操作員");
            List<OperateLogParam> operateLogList = operateLogService.findOperateLogList(param);
            List<String[]> operateListStr = new ArrayList<>();
            for (int i = 0; i < operateLogList.size(); i++) {
                OperateLogParam opeLogPar = operateLogList.get(i);
                String[] strarr = new String[]{(StringUtils.isEmpty(opeLogPar.getOperateLogId().toString())?"-":opeLogPar.getOperateLogId().toString()),
                        (StringUtils.isEmpty(opeLogPar.getCreateTime().toString())?"-":opeLogPar.getCreateTime().toString()),
                        (StringUtils.isEmpty(opeLogPar.getTitle())?"-":opeLogPar.getTitle()),
                        (StringUtils.isEmpty(opeLogPar.getData1())?"-":opeLogPar.getData1()),
                        (StringUtils.isEmpty(opeLogPar.getData2())?"-":opeLogPar.getData2()),
                        (StringUtils.isEmpty(opeLogPar.getAdminId().toString())?"-":opeLogPar.getAdminId().toString()),
                        (StringUtils.isEmpty(opeLogPar.getOperatorName())?"-":opeLogPar.getOperatorName()),
                };
                operateListStr.add(strarr);
            }

            ExcelUtil.createExcel(strTableName, headers, operateListStr, response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }

這裡裡面的ExcelUtils 在我的另一篇筆記中有

Excel導入功能

https://www.jb51.net/article/230516.htm

導出效果

到此這篇關於Java實現導出Excel功能的文章就介紹到這瞭,更多相關java導出excel內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: