`

数据导入Excel并下载功能

 
阅读更多

1、创建ExcelExportAction类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.poi.hssf.util.HSSFColor;
import com.opensymphony.xwork2.ActionSupport;
public class ExcelExportAction extends ActionSupport{
    private static final long serialVersionUID = 4565154051377016557L;
    // private static final long cen = 2565374152L;
    private static final String[] header = { "序号", "员工工号", "员工姓名", "所属部门", "职位", "入职日期", "备注" };
    private String excelFileName;
    private InputStream excelStream;

    /**
     * export excel
     * 
     * @return
     */
    public String exportExcel() {
	try {
	    // 创建excel
	    HSSFWorkbook workbook = new HSSFWorkbook();
	    HSSFSheet sheet = workbook.createSheet("测试文件1");
	    // 这里是createRow(int)可别用sheet.getRow(int)切记。。。。
	    HSSFRow row = sheet.createRow(0);
	    row.setHeightInPoints(25);
	    HSSFCellStyle cellstyle = workbook.createCellStyle();
	    cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
	    cellstyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
	    cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
	    HSSFFont font = workbook.createFont();
	    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	    cellstyle.setFont(font);
	    HSSFCell cell;
	    for (int i = 0; i < header.length; i++) {
		cell = row.createCell(i);
		cell.setCellStyle(cellstyle);
		sheet.setColumnWidth(i,25*256);
		cell.setCellValue(header[i]);
	    }
	    // 定义20行数据插入到excel表中
	    for (int j = 1; j <= 50; j++) {
		row = sheet.createRow(j);
		row.setHeightInPoints(20);
		HSSFCell cell0 = row.createCell(0);
		cell0.setCellValue(j);

		HSSFCell cell1 = row.createCell(1);
		cell1.setCellValue("员工工号" + j);

		HSSFCell cell2 = row.createCell(2);
		cell2.setCellValue("员工姓名" + j);

		HSSFCell cell3 = row.createCell(3);
		cell3.setCellValue("软研" + j + "部");

		HSSFCell cell4 = row.createCell(4);
		cell4.setCellValue("程序员" + j);

		HSSFCell cell5 = row.createCell(5);
		cell5.setCellValue("2013-05-28");

		HSSFCell cell6 = row.createCell(6);
		cell6.setCellValue("remark");
	    }
	    // 将文件存入流中
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    workbook.write(bos);
	    byte[] fileContent = bos.toByteArray();
	    ByteArrayInputStream bis = new ByteArrayInputStream(fileContent);

	    excelStream = bis;
	    excelFileName = "EXCEL文件导出测试.xls";
	    excelFileName = new String(excelFileName.getBytes(),"iso-8859-1");
	} catch (IOException e) {
	    e.printStackTrace();
	}

	return SUCCESS;
    }

    /** getter/setter method **/
    public String getExcelFileName() {
	return excelFileName;
    }

    public void setExcelFileName(String excelFileName) {
	this.excelFileName = excelFileName;
    }

    public InputStream getExcelStream() {
	return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
	this.excelStream = excelStream;
    }

}

 2、struts.xml配置

<!-- exprot excel file -->
		<action name="exportExcel" class="com.hnyyzw.trm.action.ExcelExportAction"
			method="exportExcel">
			<result name="success" type="stream">
				<!-- 下载文件类型 -->
				<param name="contentType">application/vnd.ms-excel</param>
				<!-- 返回流 excelStream 在Action中的变量名 -->
				<param name="inputName">excelStream</param>
				<!-- attachment 这个位置的参数挺特殊的,可以设置成下载时,是否出现个下载提示框,或者直接下载之类的。 fileName指定生成的文件名字(适合动态生成文件名,比如做报表时,一般都要说是几月的统计数据之类)为action中变量 -->
				<param name="contentDisposition">attachment;filename=${excelFileName}</param>
				<param name="bufferSize">1024</param>
			</result>
		</action>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics