Posted on 2012-03-05 16:37
viery 阅读(824)
评论(0) 编辑 收藏 所属分类:
JavaEE
1在apache
http://poi.apache.org/download.html上下载poi开源控件包。目前最稳定的版本:
The latest stable release is Apache POI 3.7 2将poi.jar引入项目
3更改业务逻辑,将业务逻辑返回的查询到的对象集合存入session
request.getSession().setAttribute("pclist", list);
4建立ToExcelAction


package com.vle.oa.action;

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.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.vle.oa.domain.Pc;


public class ToExcelAction extends DispatchAction
{
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)

throws Exception
{
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
//设置样式
HSSFCellStyle titleSytle=(HSSFCellStyle) wb.createCellStyle();
titleSytle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
titleSytle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Sheet sheet = wb.createSheet("电脑明细表");

// Create a row and put some cells in it. Rows are 0 based.
sheet.addMergedRegion(new CellRangeAddress(0,1,0,4));
HSSFRow rt=null;
HSSFCell ct=null;
rt=(HSSFRow) sheet.createRow(0);
ct=rt.createCell(0);
ct.setCellStyle(titleSytle);
ct.setCellValue(new HSSFRichTextString("电脑信息表"));
HSSFCellStyle tableSytle=(HSSFCellStyle) wb.createCellStyle();
tableSytle.setBorderBottom((short)1);
tableSytle.setBorderLeft((short)1);
tableSytle.setBorderRight((short)1);
tableSytle.setBorderTop((short)1);
tableSytle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Row row0 = sheet.createRow((short)1);

String[] title=new String[]
{"id","电脑编码","电脑类别","使用店铺","责任人"};
List list=(List) request.getSession().getAttribute("pclist");

for(int i=0;i<title.length;i++)
{
Cell cell = row0.createCell(i);
cell.setCellValue(title[i]);
}

for(int i=0;i<list.size();i++)
{
Pc pc=new Pc();
pc=(Pc) list.get(i);
Row r = sheet.createRow((short)i+2);

String[] pcInfo=new String[]
{String.valueOf(pc.getId()),pc.getPcId()
,pc.getPcType(),pc.getShopName(),pc.getUserName()};

for(int j=0;j<pcInfo.length;j++)
{
Cell c=r.createCell(j);
c.setCellStyle(tableSytle);
c.setCellValue(pcInfo[j]);
}

}
request.setAttribute("wb", wb);

return mapping.findForward("excel");
}

}

action返回了一个excel报表对象后跳转到struts中配置的页面
<action path="/toExcel"
type="com.vle.oa.action.ToExcelAction"
parameter="command"
scope="request">
<forward name="excel" path="/pc/excel.jsp"/>
</action>:
在这里我使用的是struts1,struts2同样原理一样最后编写跳转的jsp

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

response.reset();
out.clear();
out=pageContext.pushBody();

response.setContentType("application/msexcel");
response.setHeader("Content-disposition","inline; filename=myexcel.xls"); //attachment
HSSFWorkbook wb = (HSSFWorkbook)request.getAttribute("wb");
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
%>效果如下

:
