java实现下载并选择保存路径
2017-11-21 16:09
549 查看
一、通过浏览器提供下载,使文件直接以流的形式响应客户端浏览器。不多说了,直接上代码吧。
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="后端路径";的方式
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="后端路径";的方式
相关文章推荐
- java读取本地图片和下载可选择路径保存图片
- java下载excel文件源码(可选择保存文件路径)
- java opencv 调用摄像头实现拍照及本地保存 支持上传FTP 路径配置
- java 如何实现文件下载时,弹出文件保存窗口
- Java中实现FTP文件上传下载 涉及中文路径以及中文文件
- Java-十种内部排序实现(选择,冒泡,插入,希尔,堆,归并,快速,基数,计数,桶)及代码下载
- 下载文件,并弹出保存提示框,选择位置,绝对路径 or相对路径
- java导出文件弹出下载框让用户选择路径
- JS实现选择文件保存路径
- JAVA 实现下载功能 保存为EXCEL格式
- 用Java实现下载功能,并保存到excel表格中
- java选择保存文件的路径
- jsp页面导出用jxl方式导出数据到excel表格并下载保存(第二种直接用java类实现)
- java 实现文件下载时,弹出文件保存窗口(IE9)
- 关于创建Excel表格问题(保存至指定文件/弹出直接打开还是下载,并可以选择保存路径的对话框)
- java实现文件下载功能,自动弹出保存窗口
- java文件下载通过response流实现文件存储在客户端指定路径
- Java实现选择电脑路径的方法
- java实现文件下载功能,自动弹出保存窗口
- Java文件下载选择路径注意事项