struts 2とjxlを統合してExcelファイルをエクスポート

7369 ワード

詳細
struts 2とjxlを統合してExcelファイルをエクスポート
1、該当するJarパッケージを導入する
jxl.jar
2、ダウンロード要求
Excelファイルとしてエクスポート
  のページをExcelファイルとしてエクスポート

 3、struts.xmlの構成

	



	

 4、Action類の処理
package cn.hpu.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import cn.hpu.po.Complain;
import cn.hpu.service.ComplainService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ComplainAction extends ActionSupport {

	private ComplainService complainService;
	
	private List complains;
	
	//   
	private Integer pageSize;
	private Integer pageNumber;
	
	/**  Excel  
	 * @author qinrui
	 * @see cn.hpu.service.ComplainService complainService
	 * @param response
	 * @return java.lang.String
	 * @throws IOException 
	 */
	public void exportExcel() throws IOException {
		
		List complains = complainService.loadComplains();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.reset();
		//       
		response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader("Content-Disposition","attachment;filename=Complain.xls");
		
        OutputStream os = response.getOutputStream();
		printExcels(complains, os);
		
		os.flush();
		os.close();
	}
	
	/**       Excel
	 * @author qinrui
	 * @see cn.hpu.service.ComplainService complainService
	 * @throws IOException
	 */
	public void exportExcelPage() throws IOException {
		
		Integer sessionPageSize = (Integer) ActionContext.getContext().getSession().get("pageSize");
		
		if(sessionPageSize != null) {
			pageSize = sessionPageSize;
		} else {
			pageSize = 10;
		}
		
		complains = complainService.loadComplains((pageNumber - 1) * pageSize, pageSize);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.reset();
		//       
		response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader("Content-Disposition","attachment;filename=Complain.xls");
		
        OutputStream os = response.getOutputStream();
		printExcels(complains, os);
		
		os.flush();
		os.close();
	}
	
	/**        Excel
	 * @author qinrui
	 * @param complains
	 * @param os
	 */
	public void printExcels(List complains , OutputStream os) {
		
		try {
			//     Excel   
			WritableWorkbook book  =  Workbook.createWorkbook(os);
			 //     "     "    ,  0       
			WritableSheet sheet  =  book.createSheet("     " , 0);
			 //   Label                     (0,0)
			
			//       
			DateFormat df = new DateFormat("yyyy-MM-dd");
			WritableCellFormat wcf = new WritableCellFormat(df);
			
			String[] labels = new String[]{"    ",
					"    ","   ","   ","    ","    ",
					"     ","    ","      ","      "};
			
			//        
			for(int i = 0; i < labels.length; i++) {
				Label label = new Label(i , 0, labels[i]);
				sheet.addCell(label);
			}
			
			for(int x = 0; x < complains.size(); x++) {
				Label theme = new Label(0, x + 1, complains.get(x).getTheme());
				Label customer = new Label(1, x + 1, complains.get(x).getCustomerInfo().getName());
				Label complainant = new Label(2, x + 1, complains.get(x).getComplainant());
				Label dealer = new Label(3, x + 1, complains.get(x).getUser().getName());
				DateTime time = new DateTime(4, x + 1, complains.get(x).getTime() , wcf);
				Label state = new Label(5 , x + 1, complains.get(x).getState().getName());
				Label satisfication = new Label(6 , x + 1, complains.get(x).getSatisfication().getName());
				Label content = new Label(7, x + 1, complains.get(x).getContent());
				Label method = new Label(8, x + 1, complains.get(x).getMethod());
				Label remark = new Label(9, x + 1, complains.get(x).getRemark());
				
				sheet.addCell(theme);
				sheet.addCell(customer);
				sheet.addCell(complainant);
				sheet.addCell(dealer);
				sheet.addCell(time);
				sheet.addCell(state);
				sheet.addCell(satisfication);
				sheet.addCell(content);
				sheet.addCell(method);
				sheet.addCell(remark);
			}

			//          
			book.write();
			book.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ComplainService getComplainService() {
		return complainService;
	}

	public void setComplainService(ComplainService complainService) {
		this.complainService = complainService;
	}

	public List getComplains() {
		return complains;
	}

	public void setComplains(List complains) {
		this.complains = complains;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

}