第一步:
在MyEclipse新建项目个然后把所需的Struts2和POI的JAR包文件拷入lib文件夹中 。如下图
第二步:
配置好如下图的结构
注意struts.xml文件的配置
第三步:
建立如下图结构的类:
第四步:
先不忙写代码,在这里还要将web.xml文件配置好如下图
第五步:
开始那几个类的代码了:
从上往下 类的代码为:
第一个类:ExcelAction类
package com.hb.sxdl.action;
import java.util.ArrayList;
import java.util.List;
import com.hb.sxdl.model.Shop;
import com.hb.sxdl.uitl.BaseAction;
public class ExcelAction extends BaseAction {
private static final long serialVersionUID = -8753613460451428012L;
public String execute() throws Exception{
List<Shop> list=new ArrayList<Shop>();
Shop shop1=new Shop("S001","农夫山泉",2.00);
Shop shop2=new Shop("S002","哇哈哈",2.00);
Shop shop3=new Shop("S003","小当家",0.50);
list.add(shop1);
list.add(shop2);
list.add(shop3);
String [] titles={"商品编号","商品名称","商品价格"};
this.getRequest().put("titles", titles);
this.getRequest().put("dataList", list);
return "success";
}
}
package com.hb.sxdl.action;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
public class ExcelResult extends StrutsResultSupport {
private static final long serialVersionUID = -4133500233058664871L;
//定义HSSFWorkbook对象,代表excel工作表
private HSSFWorkbook workbook = null;
@SuppressWarnings("deprecation")
private void printExcel(ActionContext ctx) throws Exception
{
//获取request对象
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
//从请求中获取导出excel的标题行列表
String[] titles=(String[])request.getAttribute("titles");
//从请求中获取导出excel的数据列表
List<?> dataList=(List<?>)request.getAttribute("dataList");
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet("sheet1");
if(dataList!=null)
{
//创建标题行
HSSFRow titleRow = sheet.createRow(0);
for(int i=0;i<titles.length;i++)
{
HSSFCell cell=titleRow.createCell((short)i);//创建数据列
cell.setCellValue(titles[i]);//给单元格赋值
}
//填充表格
for(int i=0;i<dataList.size();i++)
{
HSSFRow dataRow = sheet.createRow(i+1);//创建数据行
Object obj=dataList.get(i);//得到dataList中的第i个对象
//利用java反射技术,执行一个对象中所有get方法
Method[] methods=obj.getClass().getMethods();
int j=0;
for(Method method:methods)
{
//如果方法以get开头,但不包括getClass方法时
if(method.getName().startsWith("get") && !method.getName().equals("getClass"))
{
HSSFCell cell=dataRow.createCell((short)j++);//创建数据列
Object value=method.invoke(obj);//执行get方法获取值
cell.setCellValue(value.toString());//给单元格赋值
}
}
}
}
}
//继承StrutsResultSupport类,必须实现doExecute方法
protected void doExecute(String arg0, ActionInvocation invocation)
throws Exception {
//获取ActionContext对象实例
ActionContext ctx = invocation.getInvocationContext();
//通过ActionContext对象实例,获取response对象
HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
//获取OutputStream实例,用于输出将excel输出到流中
OutputStream out = response.getOutputStream();
//设置影响头和内容
response.setHeader("Content-disposition","attachment;filename=sxdy.xls");
response.setContentType("application/msexcel;charset=UTF-8");
//调用printExcel方法
printExcel(ctx);
//把工作表,输出到输出流中
workbook.write(out);
//关闭输出流
out.flush();
out.close();
}
}
第三个类 Shop
package com.hb.sxdl.model;
import java.io.Serializable;
public class Shop implements Serializable {
private static final long serialVersionUID = 5043253787776974616L;
private String shopId;
private String shopName;
private double price;
public Shop(){}
public Shop(String shopId, String shopName, double price) {
this.shopId = shopId;
this.shopName = shopName;
this.price = price;
}
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
第四个类BaseAction
package com.hb.sxdl.uitl;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = -278519360580395712L;
/**
* 得到Request
* @return
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getRequest(){
Map<String, Object> map=(Map<String, Object>)ActionContext.getContext().get("request");
return map;
}
/**
* 得到HttpSession
* @return
*/
public Map<String, Object> getSession(){
Map<String, Object> map=(Map<String, Object>)ActionContext.getContext().getSession();
return map;
}
/**
* 得到Application
* @return
*/
public Map<String, Object> getApplication(){
Map<String, Object> map=(Map<String, Object>)ActionContext.getContext().getApplication();
return map;
}
/**
* 得到HttpServletResponse
* @return
*/
public HttpServletResponse getResponse(){
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
return response;
}
}
这样我们的类就写完了,也就是主要工作就OK了!
差点忘了个事了
我们的jsp页面要这样写这
最后就用Tomcat部署项目啦!相信大家都会,,,,
最最后就是浏览器看效果啦!
这样保存下来就可以用WPS以及Microsoft Office的EXCEL查看咯!
OK到此结束............