java导出excel时不能下载 20
提示:当前安全设置不允许下载该文件在internet中的安全设置都允许了,还是不行。求高手解答。代码:try{StringexcelName=newString("tes...
提示:当前安全设置不允许下载该文件
在internet中的安全设置都允许了,还是不行。
求高手解答。
代码:
try {
String excelName = new String("test.xls".getBytes(), "iso-8859-1");
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ excelName + "\"");
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("fristpage", 0);
Label idLabel = new Label(0, 0, "ID");
Label nameLabel = new Label(1, 0, "name");
Label passLabel = new Label(2, 0, "pass");
sheet.addCell(idLabel);
sheet.addCell(nameLabel);
sheet.addCell(passLabel);
for (int i = 0; i < 5; i++) {
Label labelEmployee_ID = new Label(0, i + 1, "111");
Label labelEmployee_Name = new Label(1, i + 1, "asd");
Label labelPassword = new Label(2, i + 1, "123");
sheet.addCell(labelEmployee_ID);
sheet.addCell(labelEmployee_Name);
sheet.addCell(labelPassword);
}
// Number number = new jxl.write.Number(1, 0, 789.123);
// sheet.addCell(number);
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
} 展开
在internet中的安全设置都允许了,还是不行。
求高手解答。
代码:
try {
String excelName = new String("test.xls".getBytes(), "iso-8859-1");
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ excelName + "\"");
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("fristpage", 0);
Label idLabel = new Label(0, 0, "ID");
Label nameLabel = new Label(1, 0, "name");
Label passLabel = new Label(2, 0, "pass");
sheet.addCell(idLabel);
sheet.addCell(nameLabel);
sheet.addCell(passLabel);
for (int i = 0; i < 5; i++) {
Label labelEmployee_ID = new Label(0, i + 1, "111");
Label labelEmployee_Name = new Label(1, i + 1, "asd");
Label labelPassword = new Label(2, i + 1, "123");
sheet.addCell(labelEmployee_ID);
sheet.addCell(labelEmployee_Name);
sheet.addCell(labelPassword);
}
// Number number = new jxl.write.Number(1, 0, 789.123);
// sheet.addCell(number);
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
} 展开
3个回答
展开全部
下列是我项目代码,复制就能运行:
你参考下:
Action:
public ActionForward downLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StringUtil util = new StringUtil();
DownloadDAO dao = new DownloadDAO();
// 获取文件路径
String filePath = servlet.getServletContext().getRealPath(
"/upload/demo");
String fileName = "";// 文件名称
filePath = filePath + "\\" + "资源数据.xls";
fileName = "资源数据.xls";
response.reset();
response.setContentType("application/x-msdownload");
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ util.encodeISO(fileName));
try {
dao.downLoad(filePath, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
return null;
}
dao:
/**
* 文件下载
* @param filePath
* @param output
* @return
*/
public boolean downLoad(String filePath, OutputStream output) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
int readByte = 0;
byte[] buffer = new byte[8192];
while ((readByte = inputStream.read(buffer, 0, 8192)) != -1) {
output.write(buffer, 0, readByte);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (output != null) {
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
你参考下:
Action:
public ActionForward downLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StringUtil util = new StringUtil();
DownloadDAO dao = new DownloadDAO();
// 获取文件路径
String filePath = servlet.getServletContext().getRealPath(
"/upload/demo");
String fileName = "";// 文件名称
filePath = filePath + "\\" + "资源数据.xls";
fileName = "资源数据.xls";
response.reset();
response.setContentType("application/x-msdownload");
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ util.encodeISO(fileName));
try {
dao.downLoad(filePath, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
return null;
}
dao:
/**
* 文件下载
* @param filePath
* @param output
* @return
*/
public boolean downLoad(String filePath, OutputStream output) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
int readByte = 0;
byte[] buffer = new byte[8192];
while ((readByte = inputStream.read(buffer, 0, 8192)) != -1) {
output.write(buffer, 0, readByte);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (output != null) {
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-10-21
展开全部
把response.setContentType("application/vnd.ms-excel");的类型改了试试?应该没道理会出现说当前安全设置不允许下载该文件的情况的。你可以换一个浏览器(firefox或者opera)测试一下,看看是你的代码出了问题还是浏览器本身有问题。
呃,问个无关的问题,String excelName = new String("test.xls".getBytes(), "iso-8859-1")在干嘛呢,这么无聊?
呃,问个无关的问题,String excelName = new String("test.xls".getBytes(), "iso-8859-1")在干嘛呢,这么无聊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java导出excel的传统方法太繁琐、不稳定,现在都流行用Office中间件了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询