使用POI选择导出Excel文件(SSM)

文章介绍了如何在Java的SSM框架中利用ApachePOI库导出Excel文件。首先,定义了POI的各模块功能,然后详细阐述了从引入依赖、编写mapper层、service层、controller层的代码到前端JSP页面JS和HTML的实现步骤,最终展示了如何将数据库中的数据写入Excel并供用户下载。

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


前言

如何使用POI选择导出Excel文件(SSM)


一、什么是POI?

Apache POI 是基于 Office Open XML 标准(OOXML)和 Microsoft 的 OLE 2 复合文档格式(OLE2)处理各种文件格式的开源项目。 简而言之,您可以使用 Java 读写 MS Excel 文件,可以使用 Java 读写 MS Word 和 MS PowerPoint 文件。

模块

  • HSSF - 提供读写 Microsoft Excel XLS 格式 (Microsoft Excel 97 (-2003)) 档案的功能。
  • XSSF - 提供读写 Microsoft Excel OOXML XLSX 格式 (Microsoft Excel XML (2007+)) 档案的功能。
  • SXSSF - 提供低内存占用量读写 Microsoft Excel OOXML XLSX 格式档案的功能。
  • HWPF - 提供读写 Microsoft Word DOC97 格式 (Microsoft Word 97 (-2003)) 档案的功能。
  • XWPF - 提供读写 Microsoft Word DOC2003 格式 (WordprocessingML (2007+)) 档案的功能。
  • HSLF/XSLF - 提供读写 Microsoft PowerPoint 格式档案的功能。
  • HDGF/XDGF - 提供读 Microsoft Visio 格式档案的功能。
  • HPBF - 提供读 Microsoft Publisher 格式档案的功能。
  • HSMF - 提供读 Microsoft Outlook 格式档案的功能。

二、使用步骤

1.引入依赖

代码如下(示例):

<!--poi依赖-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
    </dependency>

2、mapper层代码 包括Mapper接口、Mapper SQL代码

Mapper接口中的方法

List<Activity> selectActivityByIds(String[] id);

Mapper层的SQL代码

<select id="selectActivityByIds" parameterType="string" resultMap="BaseResultMap">
    select a.id,u1.name as owner,a.name,
    a.start_date,a.end_date,a.cost,a.description,a.create_time,
    u2.name as create_by,a.edit_time,u3.name as edit_by
    from tbl_activity a
    join tbl_user u1 on a.owner = u1.id
    join tbl_user u2 on a.create_by = u2.id
    left join tbl_user u3 on a.edit_by=u3.id
    where a.id in
    <foreach collection="array" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
    order by a.create_time desc
  </select>

3、service层代码,包括service接口和实现类

service接口中的方法

List<Activity> queryActivityByIds(String[] ids);

接口中实现类的方法

@Override
    public List<Activity> queryActivityByIds(String[] ids) {
        return activityMapper.selectActivityByIds(ids);
    }

4、controller层代码

controller类中的方法

@RequestMapping("/workbench/activity/exportActivityByIds.do")
    public void exportActivityByIds(String[] id, HttpServletResponse response) throws IOException {
        List<Activity> activityList = activityService.queryActivityByIds(id);
        HSSFWorkbook 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("修改者");

        //遍历activity,每一条记录对应一行
        //为防止异常,先对activity进行空判断
        if (activityList != null && activityList.size() > 0) {
            Activity activity = null;
            for (int i = 0; i < activityList.size(); i++) {
                activity = activityList.get(i);
                row = sheet.createRow(i + 1);
                //将遍历得到的每一条数据放到Excel文件中的每一行
                cell = row.createCell(0);
                cell.setCellValue(activity.getId());
                cell = row.createCell(1);
                cell.setCellValue(activity.getOwner());
                cell = row.createCell(2);
                cell.setCellValue(activity.getName());
                cell = row.createCell(3);
                cell.setCellValue(activity.getStartDate());
                cell = row.createCell(4);
                cell.setCellValue(activity.getEndDate());
                cell = row.createCell(5);
                cell.setCellValue(activity.getCost());
                cell = row.createCell(6);
                cell.setCellValue(activity.getDescription());
                cell = row.createCell(7);
                cell.setCellValue(activity.getCreateTime());
                cell = row.createCell(8);
                cell.setCellValue(activity.getCreateBy());
                cell = row.createCell(9);
                cell.setCellValue(activity.getEditTime());
                cell = row.createCell(10);
                cell.setCellValue(activity.getEditBy());
            }
        }
        //客户端下载
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=activityList.xls");
        //2.获取输出流
        //PrintWriter outWriter = response.getWriter();
        OutputStream out = response.getOutputStream();
        wb.write(out);

        wb.close();
        out.flush();
    }

5、前台jsp中js代码和html代码

$("#exportActivityXzBtn").click(function () {
    //收集参数
    var checkedIds = $("#tBody input[type='checkbox']:checked");
    //alert("hahaha");
    if (checkedIds.size() == 0) {
        alert("请选择要导出的市场活动")
        return;
    }
    var ids = "";
    $.each(checkedIds, function () {
        ids += "&id=" + this.value + "&";
    });
    ids = ids.substr(0, ids.length - 1);

    window.location.href = "workbench/activity/exportActivityByIds.do?id="+ids+"";

});
<button id="exportActivityXzBtn" type="button"> 下载列表数据(选择导出)</button>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值