poi之导出Excel(导出一个模版)

 

html

                    <div class="l-bar-separator"></div>
                    <div class="group">
                        <a class="link " id="downloadMode" href="javascript:;">下载导入模版</a>
                    </div>

js

    $(function() {//导出模版
        $("#downloadMode")
                .click(
                        function() {
                            var url = __ctx
                                    + '/yjfhltjbwczb/yjfhltjbwczb/yjfhltjbwczb/downLoadExcelModel.ht';
                            var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
                            //alert(userAgent);
                            if (userAgent.indexOf("compatible") > -1
                                    && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
                                window.location.href(url);
                            } else {
                                window.open(url, "导出模版");
                            }

                        });

    });

 

java

/**
     * 导出excel模板文件 
     * @param request  
     * @param response
     * @return (有时,客户需要一个标准的模板来填东西,然后在导入 这时可以弄好excel模板,供导出)
     * @throws Exception
     */
    @RequestMapping("downLoadExcelModel")
    @Action(description="导出子表Excel模板文件")
    public void exportExcelMode(HttpServletRequest request,
           HttpServletResponse response) throws Exception{
           String dirPath = FileUtil.getRootPath() + File.separator+"commons" +File.separator+"template"+File.separator+"exportMode"+File.separator;
           String fileName="xxxx报表模板.xls";
            FileInputStream inStream = new FileInputStream(new File(dirPath+fileName));
            byte[] buf = new byte[4096]; 
            int readLength; 
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Type","application/vnd.ms-excel");
            response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("GB2312"), "8859_1" ));
            response.addHeader("Pargam", "no-cache"); 
            response.addHeader("Cache-Control", "no-cache");
            OutputStream out = response.getOutputStream();   
            while (((readLength = inStream.read(buf)) != -1)) { 
                out.write(buf, 0, readLength); 
            } 
            inStream.close(); 
            out.flush();   
            out.close();
       }

 

posted @ 2018-09-29 15:43  爱跳舞的程序员  阅读(287)  评论(0编辑  收藏  举报