Struts2+POI 实现生成Excel文件和下载

本文介绍使用Struts框架实现导出Excel的功能。通过配置struts.xml文件和编写Java代码,实现从数据库获取学生数据并将其填充到Excel表格中,最终通过浏览器下载。文章包含完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts.xml部分

<package name="demo" namespace="/test" extends="struts-default">
        <action name="exportExcel" class="com.test.action.StudentAction " method="exportExcel">
            <result name="exportExcel" type="stream">
                <param name="contentType">application/vnd.ms-excel</param>  
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">1024</param>
                <param name="inputName">excelFile</param> 

            </result>
        </action>
    </package>

Action部分

public class StudentAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private Student student;
    private InputStream excelFile;
    private String fileName;
    //set,get方法
    public String exportExcel() throws Exception{

        List<Student> stuList = stuService.exportMarkActive(student);
        HSSFWorkbook workbook = exportExcel(stuList);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        workbook.write(output);
        byte[] ba = output.toByteArray();
        excelFile = new ByteArrayInputStream(ba);
        output.flush();
        output.close();
        return "exportExcel";
    }

exportExcel方法

public HSSFWorkbook exportExcel(List<Student> studentList) throws Exception {
        fileName = "学生活动报表.xls";
        //设置字符,防止乱码
        fileName= new String(fileName.getBytes("UTF-8"),"ISO8859-1");
        HSSFWorkbook wb;
        //根据studentList生成excel文件
        wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("学生活动列表");
        HSSFRow row=sheet.createRow(0);
        HSSFCell cell=row.createCell(0);
        cell.setCellValue("ID");
        cell=row.createCell(1);
        cell.setCellValue("学生活动名称");
        cell=row.createCell(2);
        cell.setCellValue("学生活动类型");
        cell=row.createCell(3);
        cell.setCellValue("学生活动状态");
        cell=row.createCell(4);
        cell.setCellValue("学生活动开始日期");
        cell=row.createCell(5);
        cell.setCellValue("学生活动结束日期");
        cell=row.createCell(6);
        cell.setCellValue("学生活动所有者");
        cell=row.createCell(7);
        cell.setCellValue("预算成本");
        cell=row.createCell(8);
        cell.setCellValue("实际成本");
        cell=row.createCell(9);
        cell.setCellValue("创建人");
        cell=row.createCell(10);
        cell.setCellValue("学生活动创建时间");
        cell=row.createCell(11);
        cell.setCellValue("修改人");
        cell=row.createCell(12);
        cell.setCellValue("学生活动修改时间");
        cell=row.createCell(13);
        cell.setCellValue("详细描述");
        cell=row.createCell(14);
        if(studentList!=null&&studentList.size()>0){
            MarketingActivities ma=null;
            for(int i=0;i<studentList.size();i++){
                ma=studentList.get(i);
                row=sheet.createRow(i+1);
                cell=row.createCell(0);
                cell.setCellValue(ma.getId());
                cell=row.createCell(1);
                cell.setCellValue(ma.getName());
                cell=row.createCell(2);
                cell.setCellValue(ma.getType());
                cell=row.createCell(3);
                cell.setCellValue(ma.getState());
                cell=row.createCell(4);
                cell.setCellValue(ma.getStartDate());
                cell=row.createCell(5);
                cell.setCellValue(ma.getEndDate());
                cell=row.createCell(6);
                cell.setCellValue(ma.getOwner());
                cell=row.createCell(7);
                cell.setCellValue(ma.getBudgetcost());
                cell=row.createCell(8);
                cell.setCellValue(ma.getActualcost());
                cell=row.createCell(9);
                cell.setCellValue(ma.getCreateBy());
                cell=row.createCell(10);
                cell.setCellValue(ma.getCreateTime());
                cell=row.createCell(11);
                cell.setCellValue(ma.getEditBy());
                cell=row.createCell(12);
                cell.setCellValue(ma.getEditTime());
                cell=row.createCell(13);
                cell.setCellValue(ma.getDescription());
            }
        }
        return wb;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值