Excel格式下载方法

java代码

/**
  * 下载模板方法
  * @param actionMapping
  * @param actionForm
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward doDownload(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
   //文件路径
        String filePath = (String) request.getParameter("filePath");
        //文件名称
        String fileName = (String) request.getParameter("fileName");
        //获取文件后缀
        String fileSuffix = filePath.substring(filePath.lastIndexOf("."));
      //获取文件路径
        String realFilePath = this.getServlet().getServletContext().getRealPath(filePath);
       
        String filedisplay = "终端厂商信息模板";//给用户提供的下载文件名
       
       
        try {
            filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
        } catch (UnsupportedEncodingException e1) {
            filedisplay = "uploadTemplate";
        }
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=/"" + filedisplay + fileSuffix + "/"");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
        FileInputStream inStream = null;
       
        try {
            inStream = new FileInputStream(realFilePath);
            OutputStream out = response.getOutputStream();
            byte[] buff = new byte[2048];
            while (true) {
                int read = inStream.read(buff, 0, 2048);
                if (read <= 0)
                    break;
                out.write(buff, 0, read);
            }
            out.flush();
            out.close();
            inStream.close();
        } catch (IOException e) {
         SysLog.writeLogs("dmreg", GlobalParameters.ERROR,"VendorManagementAction--doDownload--1:" + e.getMessage());
            throw new ActionException(e);
        } finally {
            if(inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                 SysLog.writeLogs("dmreg", GlobalParameters.ERROR,"VendorManagementAction--doDownload--2:" + e.getMessage());
                    throw new ActionException(e);
                }
            }
        }
        return null;
    }

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

js代码

 function doDownload(){
   document.myform.action="vendorManagementAction.do?method=doDownload&filePath=/views/dmreg/dm/devicemanager/dmcompany/file/Template.xls&fileName=uploadTemplateUnicom";
   document.myform.submit();
   
  }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

jsp代码

<form name="myform" method="post" enctype="multipart/form-data">


   <table border="0" cellpadding="0" cellspacing="0" class="formTable">

    <!-- 表格标题行-->
    <tr class="tableTitleTR2">
     <td colspan="6">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
       <tr>
        <td class="tableTitleLeft2">
         &#160;
        </td>
        <td class="tableTitle2">
         新增终端厂商信息
        </td>
        <td class="tableTitleRight2">
         &#160;
        </td>
       </tr>
      </table>
     </td>
    </tr>
    <tr>
     <td class="formTableL">
      &#160;
     </td>
     <td class="formTableC">
      <!-- BEGIN 表单核心区域 -->
      <table align="center" border="0" cellpadding="0" cellspacing="3"
       class="formTableCore">
       <tr>
        <td class="formLabel" style="width: 20%">
         文件导入&nbsp;
         <font color="red">*</font>
        </td>
        <td class="formField">
         <input type="file" style="width: 450px" name="codeFile"
          id="codeFile" size="50" οnkeydοwn="okd(this)"
          style="ime-mode:disabled" />

        </td>
        <td class="formField">
         <input type="button" id="closeBtn" name="closeBtn"
          class="formButton" value="下载模板" οnclick="doDownload();"/>
         <input type="button" id="closeBtn" name="closeBtn"
          class="formButton" value="导入厂商信息" οnclick="doImport();" />
        </td>
       </tr>
      </table>
      <!-- END 表单核心区域 -->
     </td>
     <td class="formTableR">
      &#160;
     </td>
    </tr>
    <tr>
     <td class="formTableLB">
      &#160;
     </td>
     <td class="formTableB">
      &#160;
     </td>
     <td class="formTableRB">
      &#160;
     </td>
    </tr>

   </table>

   <!-- BEGIN 这里放入按钮 两种按钮定义方式 建议采用前者 button 更符合w3c规范 且功能更强大 支持html代码 -->
   <div id="bottom" align="center">


   </div>
   <!-- END 这里放入按钮 -->
   <div id="bottom" align="center" style="display: block">
    <iframe frameborder="0" scrolling="auto" marginwidth="0"
     marginheight="0" style="height: 50%; width: 100%;" name="grid"
     id="grid">
    </iframe>
   </div>
  </form>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值