Strurts2下导出Excel文件(含下载弹出框)

项目是基于S2SH框架下做的一个导出Excel数据表的功能,包含有下载弹出框供选择下载存放路径。

前期因为跟的一个项目用户要求导出数据库中的数据到Excel表格中,参考了网上的资料研究一番之后,整理如下文:

下面是代码的部分:

struts2.xml代码部分:

<action name="*$*" class="com.lmc.action.{1}Action" method="{2}"><span style="font-family: Arial, Helvetica, sans-serif;"><!-- 通用的请求路径配置,{1}是请求的Action类,{2}是请求的方法名 --></span>
	    <result name="success">${URL}</result>
	    <!-- excel导出 -->
	    <result name="excel" type="stream">  
                <param name="contentType">application/vnd.ms-excel</param>  
                <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>  
                <param name="bufferSize">1024</param>
                <param name="inputName">excelFile</param>  
            </result>  
</action>
Action类:

package com.lmc.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

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.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;


public class OutputExcelAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private InputStream excelFile;
	private String downloadFileName;
	
	//构造导出excel的名称
	public String getDownloadFileName() {
		String downloadFileName = System.currentTimeMillis() + ".xls";
		try {
		    downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
		    e.printStackTrace();
		}
		return downloadFileName;
	}
	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}
	public InputStream getExcelFile() {
		return excelFile;
	}
	public void setExcelFile(InputStream excelFile) {
		this.excelFile = excelFile;
	}
	
//	关键代码:导出
	public String outputExcel() throws Exception {
//		HttpServletResponse response = ServletActionContext.getResponse();/* 这里我注释的4行代码是从数据裤中获取数据的方法,返回的是List集合 */
//		List<FrontProjectList> dataList = projectApplyBaseService
//		.query_ProjectApply3(pqc, 0, projectApplyBaseService
//		.count_queryTotalProject_consumption(pqc) + 1);

//		HSSFWorkbook workbook = exportExcel(dataList);/* 把List集合传进去,我改成没有传数据,最后返回的是一个只有 */
		HSSFWorkbook workbook = exportExcel();

		ByteArrayOutputStream output = new ByteArrayOutputStream();
		workbook.write(output);
		byte[] ba = output.toByteArray();
		excelFile = new ByteArrayInputStream(ba);
		output.flush();
		output.close();
		return "excel";
	}

	//创建excel并填充数据方法
//	public HSSFWorkbook exportExcel(List dataList) throws Exception {
	public HSSFWorkbook exportExcel() throws Exception {
		HSSFWorkbook workbook = null;
		try {
		// 这里的数据即是你要从后台取得的数据
		// 创建工作簿实例
		workbook = new HSSFWorkbook();
		// 创建工作表实例
		HSSFSheet sheet = workbook.createSheet("TscExcel");
		// 设置列宽
		this.setSheetColumnWidth(sheet);
		// 获取样式
		HSSFCellStyle style = this.createTitleStyle(workbook);

//		if (dataList != null && dataList.size() > 0) {
		if (true) {	
		// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
		HSSFRow row = sheet.createRow((short) 0);// 建立新行,创建表头
		this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING, "第一列");
		this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING, "<span style="font-family: Arial, Helvetica, sans-serif;">第二列</span>");
		this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING, "第三列");
		this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING, "第四列");
		this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING, "第五列");
		this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING, "第六列");
		this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING, "第七列");
		this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING, "第八列");

		// 给excel填充数据
//		for (int i = 0; i < dataList.size(); i++) {
//
//		// 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
//
//		FrontProjectList model = (FrontProjectList) dataList.get(i);
//
//		HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
//
//		this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING, i + 1);
//
//		if (model.getXmmc() != null)
//		    this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_STRING, model.getXmmc());
//		if (model.getXmlb() != null)
//		    this.createCell(row1, 2, style, HSSFCell.CELL_TYPE_STRING, model.getXmlb());
//		if (model.getXmdw() != null)
//		    this.createCell(row1, 3, style, HSSFCell.CELL_TYPE_STRING, model.getXmdw());
//		if (model.getZrbm() != null)
//		    this.createCell(row1, 4, style, HSSFCell.CELL_TYPE_STRING, model.getZrbm());
//		if (model.getJsqsn() != null)
//		    this.createCell(row1, 5, style, HSSFCell.CELL_TYPE_STRING, model.getJsqsn());
//		if (model.getZtz() != null)
//		    this.createCell(row1, 6, style, HSSFCell.CELL_TYPE_STRING, model.getZtz());
//		if (model.getSbsj() != null)
//		    this.createCell(row1, 7, style, HSSFCell.CELL_TYPE_STRING, model.getSbsj());
		
//		}

		} else {
		    this.createCell(sheet.createRow(0), 0, style, HSSFCell.CELL_TYPE_STRING, "查无资料");
		}
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return workbook;
	}

	    //设置列宽与列数
		private void setSheetColumnWidth(HSSFSheet sheet) {
		// 根据你数据里面的记录有多少列,就设置多少列
		    sheet.setColumnWidth(0, 3000);
		    sheet.setColumnWidth(1, 8000);
		    sheet.setColumnWidth(2, 3000);
		    sheet.setColumnWidth(3, 8000);
		    sheet.setColumnWidth(4, 8000);
		    sheet.setColumnWidth(5, 5000);
		    sheet.setColumnWidth(6, 5000);
		    sheet.setColumnWidth(7, 5000);
		}

		// 设置excel的title样式
		private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
		    HSSFFont boldFont = wb.createFont();
		    boldFont.setFontHeight((short) 200);
		    HSSFCellStyle style = wb.createCellStyle();
		    style.setFont(boldFont);
		    style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
		    return style;
		}

		// 创建Excel单元格
		private void createCell(HSSFRow row, int column, HSSFCellStyle style, int cellType, Object value) {
		    HSSFCell cell = row.createCell(column);
		    if (style != null) {
		        cell.setCellStyle(style);
		    }
		    switch (cellType) {
		        case HSSFCell.CELL_TYPE_BLANK: {
		        }
		        break;
		        case HSSFCell.CELL_TYPE_STRING: {
		            cell.setCellValue(value.toString());
		        }
		        break;
		        case HSSFCell.CELL_TYPE_NUMERIC: {
		            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		            cell.setCellValue(Double.parseDouble(value.toString()));
		        }
		        break;
		        default:
		        break;
		   }   

	    }
	
		
}
效果如下:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值