当前位置:首页 > 开发 > 编程语言 > Struts2 > 正文

struts2与jxl整合导出Excel文件

发表于: 2013-08-25   作者:chinrui   来源:转载   浏览次数:
摘要: struts2与jxl整合导出Excel文件 1、导入相应的Jar包 jxl.jar 2、下载请求 <a href="Complain_exportExcel.action" >导出为Excel文件</a> <a href="Complain_exportExcelPage.action?pageNumber=${ page.p

struts2与jxl整合导出Excel文件

1、导入相应的Jar包

jxl.jar

2、下载请求

<a href="Complain_exportExcel.action" >导出为Excel文件</a>
<a href="Complain_exportExcelPage.action?pageNumber=${ page.pageNumber }" >导出当前页为Excel文件</a>

 3、struts.xml的配置

<action name="Complain_exportExcel" class="cn.hpu.action.ComplainAction" method="exportExcel">
	<result></result>
</action>

<action name="Complain_exportExcelPage" class="cn.hpu.action.ComplainAction" method="exportExcelPage">
	<result></result>
</action>

 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<Complain> 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<Complain> 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<Complain> 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<Complain> getComplains() {
		return complains;
	}

	public void setComplains(List<Complain> 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;
	}

}

 

struts2与jxl整合导出Excel文件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
package util; import java.io.IOException; import java.net.URL; import java.util.HashMap; impo
平时用js导出table有些不足,导出的Excel内容也不够好看。 所以写了个通用的将Table导出为Excel的方
*********************************************先看看转载的这部分******************************
jxl.jar 包 下载地址: http://www.andykhan.com/jexcelapi/ 真实下载地址: http://www.andykhan.com
前段时间在搞数学建模,好多数据都是直接存在excel文件中,利用java直接读取行不通。 把数据直接粘
这周因为需要向数据库中导入数据,有多个文件,就想将读Excel文件的功能抽象出来,形成一个单独的组
1.添加引用 2.将ExcelRender.cs和SqlHelper.cs两个类拷贝到App_Code文件夹下 3.写后台代码   eg:
一、功能简述 点击“导出”按钮,弹出文件选择对话框,选择存放路径,填写文件名,点击“导出”按钮
 从事JAVA开发一年多了,大约有半年都在搞Excel导入导出吧.刚刚交接项目时,还是个对Java有些陌生
常见报表开发工具POI和JXL(JExcel)。本文采用JXL实现导出Excle功能。Excle-2003中对每个工作表的
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号