• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

移动端开发框架mui:文件下载和打开excel文件

武飞扬头像
zgphacker2010
帮助1

官方文档


1、文件下载api

学新通

 学新通

 学新通

 HTML5 API Reference学新通https://www.html5plus.org/doc/zh_cn/downloader.html

2、文件下载api

学新通

学新通

HTML5 API Reference学新通https://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL

HTML5 API Reference学新通https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.openFile

3、说明

手机app触发下载功能,下载文件到用户手机上。然后自动弹出打开文件的推荐打开软件(如果手机未安装软件,则提示下载打开该文件的软件后续才能打开文件)

4、代码

手机端:

  1.  
    <body style="background-color: #FFFFFF;">
  2.  
    <div class="mui-content" style="padding-bottom: 100px;" >
  3.  
     
  4.  
    <div style="width: 100%;height: 50px;line-height: 50px;font-weight: bold;font-size: 20px;margin-bottom: 0px;text-align: left;">
  5.  
    <form class="mui-input-group">
  6.  
    <div class="mui-input-row" style="height: 44px;" >
  7.  
    <label>开始时间</label>
  8.  
    <input type="text" data-options='{}' class="date-btn" readonly="readonly" id="startdate" placeholder="请选择开始时间" autocomplete="off">
  9.  
    </div>
  10.  
    <div class="mui-input-row" style="height: 44px;">
  11.  
    <label>结束时间</label>
  12.  
    <input type="text" data-options='{}' class="date-btn" readonly="readonly" id="enddate" placeholder="请选择结束时间" autocomplete="off" >
  13.  
    </div>
  14.  
     
  15.  
    <button type="button" class="mui-btn mui-btn-green" style="width: 50%;padding: 15px 0px;font-size: 18px;" id="exportBtn" data-id="">导出</button>
  16.  
    </div>
  17.  
     
  18.  
    </form>
  19.  
    </div>
  20.  
    </div>
  21.  
     
  22.  
     
学新通
  1.  
    //导出数据 exportBtn
  2.  
    var exportBtn = document.getElementById("exportBtn");
  3.  
    exportBtn.addEventListener("tap", function(){
  4.  
    var startdate = document.getElementById("startdate").value;//开始时间
  5.  
    var enddate = document.getElementById("enddate").value;//结束时间
  6.  
     
  7.  
    //alert("开始时间:" startdate ",结束时间:" enddate );
  8.  
    if (!startdate || startdate.length == 0 || !enddate || enddate.length == 0){
  9.  
    plus.nativeUI.alert("请选择开始时间和结束时间", function(){}, "系统提示", "确定");
  10.  
    return;
  11.  
    }
  12.  
    getAdminOrderListExport(startdate, enddate);
  13.  
    });
  14.  
     
  15.  
     
  16.  
    //导出工单信息 getAdminOrderListExport
  17.  
    function getAdminOrderListExport(startdate, enddate ){
  18.  
    var postData = {
  19.  
    startdate: startdate,
  20.  
    enddate: enddate
  21.  
    }
  22.  
    var postDataStr = "";
  23.  
    postDataStr = "startdate=" startdate;
  24.  
    postDataStr = "&enddate=" enddate;
  25.  
    var url = AppPath "/faultOrder/adminOrderListExport";
  26.  
    var wt = plus.nativeUI.showWaiting('数据正在导出,请勿关闭');
  27.  
    var dtask = plus.downloader.createDownload(url,{
  28.  
    method:"POST",
  29.  
    data: postDataStr
  30.  
    },function(d,status){
  31.  
    if(status == 200){
  32.  
    plus.nativeUI.closeWaiting();
  33.  
    var path = d.filename;
  34.  
    plus.io.resolveLocalFileSystemURL( path, function(entry){
  35.  
    plus.nativeUI.alert("导出成功。文件路径: " entry.fullPath, function(){
  36.  
    plus.runtime.openFile( entry.fullPath);
  37.  
    }, "系统提示", "确定");
  38.  
     
  39.  
    }, function(){
  40.  
    plus.nativeUI.alert("导出文件失败!", function(){}, "系统提示", "确定");
  41.  
    });
  42.  
    }else{
  43.  
    plus.nativeUI.alert('导出失败,错误码:' status, function(){}, "系统提示", "确定");
  44.  
    }
  45.  
    });
  46.  
    dtask.start();
  47.  
    }
学新通

后端:

  1.  
    /**
  2.  
    * 导出工单信息
  3.  
    * @param sign
  4.  
    * @return
  5.  
    */
  6.  
    // @ResponseBody
  7.  
    @RequestMapping(value = "/adminOrderListExport" , method = RequestMethod.POST)
  8.  
    public ResponseEntity<byte[]> getAdminOrderListExport(
  9.  
    @RequestParam("startdate") String startdate,
  10.  
    @RequestParam("enddate") String enddate,
  11.  
    HttpServletRequest request
  12.  
    ){
  13.  
    try {
  14.  
     
  15.  
    if (StringUtils.isNotEmpty(startdate)
  16.  
    && StringUtils.isNotEmpty(enddate)) {
  17.  
     
  18.  
    //请查询一个月内的数据
  19.  
    // String searchDate = decryptStartdate.substring(0 , 7);
  20.  
    // if (!decryptEnddate.startsWith(searchDate)){
  21.  
    // return JsonUtils.JsonError("请查询一个月内的数据!");
  22.  
    // }
  23.  
     
  24.  
     
  25.  
    //查询参数
  26.  
    Map<String , Object> paramsMap = new HashMap<>();
  27.  
    paramsMap.put("startdate" , decryptStartdate);
  28.  
    paramsMap.put("enddate" , decryptEnddate);
  29.  
     
  30.  
    List<Map<String , Object>> listMap = faultOrderService.getAdminOrderList(paramsMap);
  31.  
    // logger.info("获取工单数据导出: " mapper.writeValueAsString(listMap));
  32.  
     
  33.  
    File exportFile = adminOrderListExportHandle(listMap , request);
  34.  
    // ok表示http请求中状态码200
  35.  
    ResponseEntity.BodyBuilder builder = ResponseEntity.ok();
  36.  
    // 内容长度
  37.  
    builder.contentLength(exportFile.length());
  38.  
    // application/octet-stream 二进制数据流(最常见的文件下载)
  39.  
    // builder.contentType(MediaType.APPLICATION_OCTET_STREAM);
  40.  
    // xls 文件下载格式
  41.  
    builder.contentType(MediaType.parseMediaType("application/vnd.ms-excel"));
  42.  
     
  43.  
    // 使用URLEncoding.decode对文件名进行解码
  44.  
    String filename = URLEncoder.encode(exportFile.getName(), "UTF-8");
  45.  
    // 根据浏览器类型,决定处理方式
  46.  
    builder.header("Content-Disposition", "attachment; filename=" filename);
  47.  
    return builder.body(FileUtils.readFileToByteArray(exportFile));
  48.  
    }
  49.  
    } catch (Exception e){
  50.  
    e.printStackTrace();
  51.  
    logger.error("导出工单数据异常," e.getMessage());
  52.  
    // return JsonUtils.JsonException("获取数据出现异常");
  53.  
    }
  54.  
    return null;
  55.  
    }
  56.  
     
  57.  
     
  58.  
    //导出数据
  59.  
    public File adminOrderListExportHandle(List<Map<String, Object>> list, HttpServletRequest request){
  60.  
     
  61.  
    File toFile = null;
  62.  
    try {
  63.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
  64.  
    StringBuffer sb = new StringBuffer("");
  65.  
    sb.append("运维工单统计表").append("-").append(sdf.format(new Date())).append(".xls");
  66.  
    String filename = new String(sb.toString().getBytes("iso-8859-1"));
  67.  
    String shellName = sb.toString();
  68.  
    // String filePath = "/static/upload/" shellName;
  69.  
    // toFile = new File(request.getRealPath(filePath));
  70.  
    toFile = new File(getFaultFilePath() shellName);
  71.  
    // logger.info("filepath ; " getFaultFilePath());
  72.  
    // toFile = new File(shellName);
  73.  
    String titles[] = { "工单编号", "姓名", "手机号", "处室", "房间号", "报修类型" , "故障等级" , "故障描述", "工单时间", "处理人", "到场时间", "完成时间"};
  74.  
    HSSFWorkbook wb = new HSSFWorkbook();
  75.  
    HSSFSheet sheet = wb.createSheet(shellName);
  76.  
    sheet.autoSizeColumn(1, true);
  77.  
    HSSFCellStyle style = wb.createCellStyle();
  78.  
    HSSFRow row = sheet.createRow(0);
  79.  
    row.setHeightInPoints(20);//目的是想把行高设置成20px
  80.  
    // style.setAlignment();
  81.  
    HSSFCell cell;
  82.  
    for (int i = 0; i < titles.length; i ) {
  83.  
    cell = row.createCell((short) i);
  84.  
    cell.setCellValue(titles[i]);
  85.  
    cell.setCellStyle(style);
  86.  
    }
  87.  
    int totalCount = 0;
  88.  
    for (short i = 0; i < list.size(); i ) {
  89.  
    row = sheet.createRow(i 1);
  90.  
    Map<String,Object> currentDataMap = list.get(i);
  91.  
    String workOrderId = currentDataMap.get("workOrderId").toString();//工单编号
  92.  
    String username = currentDataMap.get("username").toString();//姓名
  93.  
    String mobile = currentDataMap.get("mobile").toString();//手机号
  94.  
    String departmentName = currentDataMap.get("departmentName").toString();//处室
  95.  
    String roomNumber = currentDataMap.get("roomNumber").toString();//房间号
  96.  
    String repairTypeText = currentDataMap.get("repairTypeText").toString();//报修类型
  97.  
    String faultLevelText = currentDataMap.get("faultLevelText").toString();//故障等级
  98.  
    String faultDesc = currentDataMap.get("faultDesc").toString();//故障描述
  99.  
    String createDate = currentDataMap.get("createDate").toString();//工单时间
  100.  
    String workUserName = currentDataMap.get("workUserName").toString();//处理人
  101.  
    String confirmProcessDate = currentDataMap.get("confirmProcessDate").toString();//到场时间
  102.  
    String confirmRepairedDate = currentDataMap.get("confirmRepairedDate").toString();//完成时间
  103.  
    row.setHeightInPoints(30);//目的是想把行高设置成20px
  104.  
    row.createCell(0).setCellValue(workOrderId);
  105.  
    row.createCell(1).setCellValue(username);
  106.  
    row.createCell(2).setCellValue(mobile);
  107.  
    row.createCell(3).setCellValue(departmentName);
  108.  
    row.createCell(4).setCellValue(roomNumber);
  109.  
    row.createCell(5).setCellValue(repairTypeText);
  110.  
    row.createCell(6).setCellValue(faultLevelText);
  111.  
    row.createCell(7).setCellValue(faultDesc);
  112.  
    row.createCell(8).setCellValue(createDate);
  113.  
    row.createCell(9).setCellValue(workUserName);
  114.  
    row.createCell(10).setCellValue(confirmProcessDate);
  115.  
    row.createCell(11).setCellValue(confirmRepairedDate);
  116.  
    }
  117.  
    // row = sheet.createRow(list.size() 1);
  118.  
    // row.setHeightInPoints(30);//目的是想把行高设置成20px
  119.  
    // row.createCell(0).setCellValue("采集总量");
  120.  
    // row.createCell(1).setCellValue("" totalCount);
  121.  
    // 回去输出流
  122.  
    // toFile = new File(sb.toString());
  123.  
     
  124.  
    OutputStream os = new FileOutputStream(toFile);
  125.  
    wb.write(os);
  126.  
    // FileUtils.copyFile(toFile , os);
  127.  
    os.close();
  128.  
    } catch (Exception e) {
  129.  
    e.printStackTrace();
  130.  
    logger.error("处理导出文件出错: " e.getMessage());
  131.  
    }
  132.  
    return toFile;
  133.  
    }
  134.  
     
  135.  
     
学新通

注意事项: 上面后端使用的是 springmvc 实现文件下载功能,跟struts2、原生servlet用法有点区别。

springmvc 另一种导出方式(xls)

Java使用poi导出数据到excel文件(单表或多表导出)学新通https://blog.csdn.net/zgphacker2010/article/details/110499696

JSP实现文件上传和文件下载学新通https://blog.csdn.net/zgphacker2010/article/details/124520408

struts2 下载文件

Java页面到后台中文乱码处理、下载文件浏览器文件名中文乱码问题学新通https://blog.csdn.net/zgphacker2010/article/details/89921088

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghccaf
系列文章
更多 icon
同类精品
更多 icon
继续加载