response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-isposition","inline; attachment;filename=" + filePath);
File file = new File(filePath);
if (file.exists()) {
DataOutputStream temps = new DataOutputStream(response.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(filePath));
byte[] b = new byte[2048];
while((in.read(b))!=-1){
temps.write(b);
temps.flush();
}
in.close();
temps.close();
}
} catch(Exception e) {
e.printStackTrace();
}
以上代码在 office 2003 中 excel 能直接在jsp页面打开
但是 office 2007中 excel 就开不开 大家有知道的吗?
帮帮忙 谢谢啦!!!!
response.setHeader("Content-isposition","inline; attachment;filename=" + filePath);
File file = new File(filePath);
if (file.exists()) {
DataOutputStream temps = new DataOutputStream(response.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(filePath));
byte[] b = new byte[2048];
while((in.read(b))!=-1){
temps.write(b);
temps.flush();
}
in.close();
temps.close();
}
} catch(Exception e) {
e.printStackTrace();
}
以上代码在 office 2003 中 excel 能直接在jsp页面打开
但是 office 2007中 excel 就开不开 大家有知道的吗?
帮帮忙 谢谢啦!!!!
contentType 这个我也换了不少了,但是就是没找到对xlsx 好用的。
提示框倒是有,但是一点击 打开 就死在画面上了。。
貌似jxl只支持2003以下的。。
https://www.coderanch.com/t/468817/JSP/java/opening-office-document-JSP
那catch
try 呢
上面的代码是写在jsp中的
我的想法是调用服务器上的一个xlsx 文件 在IE中直接打开2003的(xls) 能打开 2007(xlsx) 的开不开
test.jsp<%
try{
if("pdf".equals(format)){
response.setContentType("application/pdf");
}else if ("txt".equals(format)) {
response.setContentType("text/plain;charset=shift-jis");
}else if ("pptx".equals(format)) {
response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
}else if ("xlsx".equals(format)) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}else if ("docx".equals(format)) {
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}else if ("dotx".equals(format)) {
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.template");
}else if ("doc".equals(format)) {
response.setContentType("application/msword");
}else if ("xls".equals(format)) {
response.setContentType("application/vnd.ms-excel application/x-excel");
}else if ("pps".equals(format)) {
response.setContentType("application/vnd.ms-powerpoint");
}else if ("ppsx".equals(format)) {
response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.slideshow");
}else {
response.setContentType("application/octet-stream");
}
response.addHeader("Content-Disposition","filename="+sfilename);
FileInputStream fis = new FileInputStream(filePath);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
while (fis.read(buffer) != -1) {
bos.write(buffer);
}
buffer = null;
fis.close();
bos.close();
out.clear();
out = pageContext.pushBody();
}catch (Exception e){
e.printStackTrace();
}
%>
以上是我的全部代码 大家帮忙看看哪个地方有问题呀!!!!