poi导出excel

本文详细介绍了如何在JavaWeb项目中利用Apache POI库导出Excel。首先,要在pom.xml中添加POI依赖。接着,定义实体类并使用注解。然后,查询所需数据并调用导出方法。此外,提供了一个工具类来协助导出操作。最后,前端页面通过window.location.href来触发导出,避免使用ajax导致的问题。

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

1、pom.xml文件中添加依赖

<!--EasyPOI-->
<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>2.3.0.2</version>
    <exclusions>
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>2.3.0.2</version>
</dependency>
<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>2.3.0.2</version>
</dependency>
<!--EasyPOI 校验,下面两个实现 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.3.Final</version>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.apache.bval</groupId>
    <artifactId>org.apache.bval.bundle</artifactId>
    <version>1.1.0</version>
</dependency>

2、实体类(用到了注解)

import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author   Yanli Yang
 */
@Data
public class StatementAccountDetails{
    /**
     *
     */
    private Long id;
    /**
     * 订单id
     */
    private Long orderId;
    /**
     * 订单编号
     */
    @Excel(name = "Order No.")
    private String orderNo;
    /**
     * 实收
     */
    @Excel(name = "Amount Received(RM)")
    private BigDecimal paidInAmount;
    /**
     * 卖家SKU
     */
    @Excel(name = "SellerID")
    private String sellerSku;
    /**
     * 平台SKU   这个字段不要了
     */
    private String platformSku;
    /**
     * 交易时间
     */
    @Excel(name = "Order Time")
    private Date tradingTime;
    /**
     * 第三方支付手续费
     */
    @Excel(name = "Third Party Payment Handling Fee")
    private BigDecimal poundage;
    /**
     * 运费
     */
    @Excel(name = "Shipping")
    private BigDecimal freight;
    /**
     * 对账单id
     */
    @Excel(name = "Reconciliation Order No.")
    private Long statementOfAccountId;
    /**
     * 手续费的税费
     */
    @Excel(name = "Tax for handling fee")
    private BigDecimal poundageTaxes;
    /**
     * 运费的税费
     */
    @Excel(name = "Tax for shipping")
    private BigDecimal freightTaxes;
    /**
     * 订单金额
     */
    @Excel(name = "Order Amount")
    private BigDecimal orderAmount;

    @ExcelCollection(name = "GoodsDetails", orderNum = "6")
    private List<StatementAccountGoods> statementAccountGoodsList;
}

3、查询数据,并调用导出的方法

@RequestMapping("exportExcel")
public void exportExcel(HttpServletResponse response,String statementAccountId){
    Integer accountId = null;
    if(StringUtils.isNotEmpty(statementAccountId)){
        accountId = Integer.parseInt(statementAccountId);
    }
    List<StatementAccountDetails> accountList = this.statementAccountService.getExportAccountDetailsList(accountId);
    if (accountList != null && accountList.size() > 0){
        for (StatementAccountDetails statementAccountDetails : accountList) {
            List<StatementAccountGoods> statementAccountGoodses = this.statementAccountService.getExportAccountGoodsList(statementAccountDetails.getOrderId());
            statementAccountDetails.setStatementAccountGoodsList(statementAccountGoodses);
        }
    }

    ExportParams exportParams = new ExportParams();
    exportParams.setTitle("Reconciliation Details");
    exportParams.setSheetName("Reconciliation Details");
    Workbook workbook = ExcelExportUtil.exportExcel(exportParams,StatementAccountDetails.class,accountList);
    try {
        ResponseUtils.writeExcel(response,workbook,"ReconciliationDetails.xls");
    } catch (IOException e) {
        log.error("对账单明细导出Excel错误",e);
    }
}

4、工具类

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * HttpServletResponse 相关的工具类
 *
 * @author songjunjie
 */
public class ResponseUtils {

    /**
     * 将excel数据写到HttpServletResponse中,浏览器将提示下载文件。默认的编码格式为UTF-8
     *
     * @param response
     * @param data 数据
     * @param filename 浏览器下载文件时,显示的文件名字
     * @throws IOException
     */
    public static void writeExcel(HttpServletResponse response, byte[] data , String filename) throws IOException {
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        workbook.write(byteArrayOutputStream);
//        byte[] bytes = byteArrayOutputStream.toByteArray();
        // 清空response
        response.reset();
        // 设置response的Header
        response.setContentType(HttpContentTypes.XLS + ";charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "iso8859-1"));
        response.addHeader("Content-Length", "" + data.length);
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();

    }
}

5、前端页面调用 要使用window.location.href 不能使用ajax调用

window.location.href = basePath+"/appointmentCtr/exportData?patientId="+appointment.patientId;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值