用jxl导出excel文件时如何弹出文件保存对话框?
在JSP页面上点击一个按钮,导出excel文件,现在做的这个导出是固定了存储路径和文件名的:publicActionForwardexecute(ActionMappin...
在JSP页面上点击一个按钮,导出excel文件,现在做的这个导出是固定了存储路径和文件名的:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ListDateForm listDateForm = (ListDateForm) form;
String[] title = { "ID", "username", "password", "phone", "sex" };
String sql = "select * from test";
excel e = new excel();
e.createEXCEL(title, sql, "h:\\aa.xls");
return null;
}
怎样做能实现点击按钮时弹出保存文件对话框,自定义文件存储路径和文件名呢?
能不能详细说明一下啊~~~?初学JAVA,不是恨懂 展开
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ListDateForm listDateForm = (ListDateForm) form;
String[] title = { "ID", "username", "password", "phone", "sex" };
String sql = "select * from test";
excel e = new excel();
e.createEXCEL(title, sql, "h:\\aa.xls");
return null;
}
怎样做能实现点击按钮时弹出保存文件对话框,自定义文件存储路径和文件名呢?
能不能详细说明一下啊~~~?初学JAVA,不是恨懂 展开
2个回答
展开全部
首先生成一个输入流
InputStream stream = *****;
OutputStream os = null;
response.setContentType("APPLICATION/DOWNLOAD");
response.setHeader("Content-Disposition", "attachment; filename=" + ***);//***是文件名
response.setContentLength(stream.available());
os = response.getOutputStream();
int iBytesRead = 0;
byte[] buffer = new byte[8192];
while ((iBytesRead = stream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, iBytesRead);
}
os.close();
response.flushBuffer();
InputStream stream = *****;
OutputStream os = null;
response.setContentType("APPLICATION/DOWNLOAD");
response.setHeader("Content-Disposition", "attachment; filename=" + ***);//***是文件名
response.setContentLength(stream.available());
os = response.getOutputStream();
int iBytesRead = 0;
byte[] buffer = new byte[8192];
while ((iBytesRead = stream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, iBytesRead);
}
os.close();
response.flushBuffer();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询