java实现下载并选择保存路径

来源:互联网 发布:北京市大兴区 阿里云 编辑:程序博客网 时间:2024/06/05 22:39

一、通过浏览器提供下载,使文件直接以流的形式响应客户端浏览器。不多说了,直接上代码吧。


 public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws RowsExceededException, WriteException, IOException {
        OutputStream os = response.getOutputStream();// 取得输出流
        response.reset();// 清空输出流
        response.setHeader("Content-disposition", "attachment; filename=testRed.xls");// 设定输出文件头
        response.setContentType("application/msexcel");// 定义输出类型


        WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
        String tmptitle = "测试数据"; // 标题
        WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称


        // 设置excel标题
        WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false,
                                              UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        WritableCellFormat wcfFC = new WritableCellFormat(wfont);
        wcfFC.setBackground(Colour.AQUA);
        wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
        wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false,
                                           UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        wcfFC = new WritableCellFormat(wfont);
        // 开始生成主体内容
        wsheet.addCell(new Label(0, 2, "姓名"));
        wsheet.addCell(new Label(1, 2, "邮箱"));
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        Map<String, String> map = new HashMap<String, String>();
        map.put("Red1", "it_red@sina.com");
        map.put("Red2", "it_red@sohu.com");
        map.put("Red3", "it_red@163.com");
        int count = 0;
        for (String key : map.keySet()) {
            wsheet.addCell(new Label(0, count + 3, key));
            wsheet.addCell(new Label(1, count + 3, map.get(key)));
            count++;
        }



        // 主体内容生成结束
        wbook.write(); // 写入文件
        wbook.close();
        os.close(); // 关闭流
    }
}


二、在jsp页面不支持使用ajax,最好使用

window.location.href="后端路径";的方式


阅读全文
0 0
原创粉丝点击