我用jsp导出excel文件的方式导出jsp文件为
<%@ page language="java" import="java.util.*,com.revoview.comm.vo.*,com.revoview.comm.bean.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ page contentType="application/msexcel" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP '1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
<!--
.STYLE4 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; color: #FFFFFF; font-weight: bold; }
.STYLE5 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 12px;
color: #006699;
}
.STYLE6 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}
.STYLE7 {font-size: 14}
-->
</style>
<script type="text/javascript" src="js/order.js"></script>
</head> <body>

<table width="100%" border="1">
<tr height="10%" align="center">
<td colspan="7"  >
<strong>${requestScope.order.gdbh }</strong>
</td>
</tr>
<tr align="center" height="30%">

<td width="23%"  >
<strong>工单数量</strong>
</td>
<td width="11%"  >
<strong>${requestScope.order.gdsl }</strong>
</td>
<td  valign="middle" >
<strong>客户</strong>
</td>
<td  valign="middle" >
<strong>${requestScope.order.gdkh }</strong>
</td>
<td  valign="middle" >
<strong>BOM编号:</strong>
</td>
<td width="7%"  valign="middle" >
<strong>${requestScope.bom.bombh }</strong>
</td>
<td width="23%"  valign="middle" >
<strong>&nbsp;</strong>
</td>
</tr>
<tr align="center">
<td width="7%" >
<strong>物料编号</strong>
</td>
<td width="23%" >
<strong>规格描述</strong>
</td>
<td width="23%" >
<strong>物料型号</strong>
</td>
<td width="11%" >
<strong>制造商</strong>
</td>
<td  valign="middle" >
<strong>数量</strong>
</td>
<td  valign="middle">
<strong>单价</strong>
</td>
<td  valign="middle">
<strong>(约)总金额</strong>
</td>
</tr>
<%
double zje=0.0;
int i=0;
int a=0;
 %>
<c:forEach items="${requestScope.list}" var="list">
<%int j=0; %>
<c:forEach items="${list}" var="m">
<tr align="center"  valign="middle">
<%if(j==0){%>
<td rowspan="${fn:length(list)}">
${m.wlbh }&nbsp;
</td>
<td rowspan="${fn:length(list)}">
${m.ggms }&nbsp;
</td>
<%} %>
<td>
${m.wlxh }&nbsp;
</td>
<td>
${m.zzs }&nbsp;
</td>
<%if(j==0){ %>
<td  rowspan="${fn:length(list)}">
${m.wlsl*requestScope.order.gdsl }&nbsp;

</td>
<td  rowspan="${fn:length(list)}">
${m.dj }&nbsp;
</td>

<td  rowspan="${fn:length(list)}">
﹩<fmt:formatNumber value="${(m.wlsl*requestScope.order.gdsl)*m.dj }" pattern="0.000000"/>&nbsp;
<%
BomVO bvo=(BomVO)pageContext.findAttribute("m");
Order o=(Order)request.getAttribute("order");
zje=zje+((bvo.getWlsl()*o.getGdsl())*bvo.getDj());
 %>
</td>
<%} 
++i;
++j;
%>
</tr>
</c:forEach>
<%
if(j>1)
{
a=a+(j-1);
}
 %>
</c:forEach>
</table>
  <%
  Order o=(Order)request.getAttribute("order");
   response.setHeader("Content-disposition","inline; filename="+o.getGdbh()+".xls");
   //以上这行设定传送到前端浏览器时的档名为test1.xls
   //就是靠这一行,让前端浏览器以为接收到一个excel档  
   //response.sendRedirect("loginuser.jsp");
%>
</body>
</html>
导出excel文件是没有问题,都可以保存,但是我吧他部署到服务器上以后,就不能了!这是为什么呀!
jsp主要的代码

解决方案 »

  1.   

    response.setHeader("Content-disposition","inline; filename="+o.getGdbh()+".xls");
     <%@ page contentType="application/msexcel" %>
    主要就是这两句!!就可以到处excel文件了!
      

  2.   

    自问自答啊。<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 这一句都解决了!
      

  3.   

    去看看我这里http://blog.csdn.net/huiwenjie168/article/details/7287849
      

  4.   

    第三方jar包很多的啊。不过你直接在jsp用小脚本写最好加上
    out.clear(); 
    out = pageContext.pushBody();
    不然你会有意外惊喜的
      

  5.   

    这样写: response.setContentType( "application/vnd.ms-excel");   
     response.setHeader("Content-Disposition", "attachment;filename=tqkhmx.xls");