Posted on 2012-03-05 16:37
viery 閱讀(827)
評論(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();
%>效果如下

:
