struts jxl导出Excel

struts2配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
        <package name="vclassenroll" extends="json-default" namespace="/vclassenroll">
            <action name="op/*" class="com.xxx.action.VclassEnrollOperateAction" method="{1}">
                <result name="excel" type="stream">  
                <param name="contentType">  
                    application/vnd.ms-excel  
                </param>  
                <param name="inputName">excelStream</param>  
                <param name="contentDisposition">  
                   filename="${fileName}.xls"
                </param>  
                <param name="bufferSize">1024</param>  
            </result>
            </action>
        </package>
</struts>

struts2对应的action

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.annotation.Resource;

public class VclassEnrollOperateAction extends BaseAction {
    private static final long serialVersionUID = -941593970307742358L;
    @Resource
    private VclassEnrollService vclassEnrollService;
    private VclassEnroll vclassEnroll;
    private String classId;
    private InputStream excelStream;
    private String fileName;

    public String exportExcel() {
            List<VclassEnroll> list = 数据集合;
            // 将OutputStream转化为InputStream
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            JxlExcelUtils.getExcelStream(out, list, "报名记录-"+cname, columns);
            excelStream =  new ByteArrayInputStream(out.toByteArray());
            fileName = createFileName();
        }
        return "excel";
    }

你可能感兴趣的:(struts,Excel,导出)