web開發——寫一個簡單的表格匯出操作

來源:互聯網
上載者:User

標籤:gre   --   script   stream   icon   height   tac   進度條   ring   

一、前台頁面:

主要是一個按鈕和一個表格,表格有顯示資料,按鈕負責將表格中的資料選擇性地匯出。除此外,可以附加一個小視窗和進度條,用於顯示下載進度。

1. 按鈕:<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-redo" data-options="plain:true" id="btn-exp" onclick="fun_export()">匯出詳細資料</a>

2.表格:<div id="dataGrid" style="margin-bottom:5px;margin-top:1px">

                <table id="dg" data-options="toolbar:‘#tb‘"></table>
           </div>

3.進度條和小視窗
<div id="win" class="easyui-window" title="下載中" style="width: 500px; height: 80px" data-options="iconCls:‘icon-save‘,modal:true">
  <div id="p" class="easyui-progressbar" data-options="value:10" style="width:400px;margin-top:10px;margin-left:50px;"></div>
</div>

4.下載框:<iframe id="bgfileDownFrame" src="" style="display:none; visibility:hidden;"></iframe>

二、jS請求

 

<script>

  function fun_export(){
    $(‘#win‘).window(‘open‘);
    setProgress();

         //AJAX請求
    $.ajax({
      url:‘${pageContext.request.contextPath}/media/getExl.action‘, //TODO
      data:{},
      type: ‘post‘,
      dataType:"json",
      success: function(data){
      // alert("匯出!!!!!");
      if(data.isSuccess == "true"){
        $("#bgfileDownFrame").attr("src","${pageContext.request.contextPath}/media/downloadExl.action?docToken="+data.token);
        value = 100;
        setProgress();
        $(‘#win‘).window(‘close‘);
      }

      else{
        $(‘#win‘).window(‘close‘);
        alert("產生統計表出錯!");
        }
      },
  });
}

  //設定進度條的值
  function setProgress(){
  var value = $(‘#p‘).progressbar(‘getValue‘);
  if (value < 100){
  value += Math.floor(Math.random() * 20);
  $(‘#p‘).progressbar(‘setValue‘, value);
  setTimeout(arguments.callee, 200);
  }
}
</script>

三、action要求方法:


@Action(value="getExl")
public String getExl(){

Map<String,String> result = new HashMap<String,String>();
result.put("isSuccess", "false");


/* 資料來源*/
Map<String, Object> dataMap = new HashMap<String, Object>();//匯出結果
List<Map<String, Object>> resultTable1 = mediaManageBPO.getMediaInfo();
dataMap.put("rows", resultTable1);
//路徑
String webPath = this.request.getSession().getServletContext().getRealPath("");
MDoc mdoc = new MDoc();
try {

  String tmpFileDir = webPath + File.separator +"docTemplate";
  //檢查臨時檔案夾是否存在

      Util.checkDirExist(tmpFileDir);
  //20161227105212956742620
  String fileToken = Util.generateTmpFileName(tmpFileDir);

    //fileToken 是產生的臨時檔案名稱
  String docTemplatePath = webPath + File.separator +"docTemplate" + File.separator + fileToken;

     //media.ftl是模板檔案,先根據xls檔案--另存新檔xml檔案--eclipse下再重新命名為ftl檔案
  String templateName = "media.ftl";

   
  mdoc.createXls(dataMap, docTemplatePath,templateName);
  result.put("isSuccess", "true");
  result.put("token", fileToken);
  this.request.getSession().setAttribute("DownloadFile","資料統計表"+UtilsLXJ.getDate());
} catch (UnsupportedEncodingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

outputJson(result);

return NONE;
}
  //下載
  @Action(value="downloadExl")
  public String downloadExl(){
  //資料統計表20161227105333.xls
    String downloadName = (String)this.request.getSession().getAttribute("DownloadFile");
      if(Util.trim(downloadName).isEmpty()){
      downloadName = "資料統計.xls";
      }else{
      downloadName = downloadName + ".xls";
      }
try {
  OutputStream os = this.response.getOutputStream();

  if (Util.isIE(this.request)) {
  downloadName = URLEncoder.encode(downloadName, "utf-8");
} else {
  downloadName = new String(downloadName.getBytes("utf-8"),
  "iso-8859-1");
}
  this.response.setContentType("application/x-download");
  this.response.addHeader("Content-Disposition",
  "attachment;filename=\"" + downloadName + "\"");
  this.response.flushBuffer();
  
  String webPath = this.request.getSession().getServletContext().getRealPath("");
  //tmpFileDir臨時檔案的位置
  String tmpFileDir = webPath + File.separator +"docTemplate";
  //臨時檔案後面加上檔案token
  String bgFile = tmpFileDir + File.separator + docToken;//docToken是從getExl action那裡傳來的
     FileInputStream fis = new FileInputStream(bgFile);
  //輸出
  Util.copyStrem(fis, os);
  fis.close();
  os.close();
  //刪除臨時檔案
   Util.delFile(bgFile);

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

結果就是如下:

 

web開發——寫一個簡單的表格匯出操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.