????Web????????????????????????????????excel????????????????????????е???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????£?????????????????????itRed  ????it_red@sina.com  ?????????http://www.cnblogs.com/itred  GitHub?????http://github.com/itred
???????????????????????????????????????????Χ????????????????????????????????????JXL???excel???????????????????????????????????????????jsp???????ú?action???response??????excel????????ɡ?????????????????????????????????????????????????????????塢??????????????????????????jsp?????????????????????????????????????????????????????????????????????????????????????г???
????Demo 1:
?????????????????????????action?н???????session???????????У????????????????jsp??棬???????jsp???????????ContentType????????????httpЭ???response.ContentType ???????ContentType??????????????????Ч????????ContentType?text/html????????????????????
???????web?????????struts2?????jar???? ??????index.jsp?????????form????????????????????????????????????????????????????input?????????????action??index.jsp????????£?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>????</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">
</head>
<body>
<form action="getXls_excel" method="post">
<input type="submit" value="??" />
</form>
</body>
</html>
??????????action???ExcelAction????????£?
package com.red.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* jsp???????excel
* @author Red
*/
public class ExcelAction extends ActionSupport {
private static final long serialVersionUID = -3673769122296267756L;
protected HttpServletRequest request = ServletActionContext.getRequest();
protected HttpServletResponse response = ServletActionContext.getResponse();
public void getXls() throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("<table><tr><td>???????</td><td>??????</td></tr>");
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");
for (String key : map.keySet()) {
sb.append("<tr><td>").append(key).append("</td><td>").append(map.get(key)).append("</td></tr>");
}
request.getSession().setAttribute("excel"?? sb.toString());
response.sendRedirect(request.getContextPath() + "/export.jsp");
}
}