引入Hutool
Hutool-all是一个Hutool的集成打包产品,由于考虑到“懒人”用户及分不清各个模块作用的用户,“无脑”引入hutool-all模块是快速开始和深入应用的最佳方式。
起初Hutool只提供了两种引入方式:
引入hutool-all以便使用所有工具类功能
引入hutool-xxx单独模块使用
介绍Hutool-poi
Java针对MS Office的操作的库屈指可数,比较有名的就是Apache的POI库。这个库异常强大,但是使用起来也并不容易。Hutool针对POI封装一些常用工具,使Java操作Excel等文件变得异常简单。
Hutool-poi是针对Apache POI的封装,因此需要用户自行引入POI库,Hutool默认不引入。到目前为止,Hutool-poi支持:
Excel文件(xls, xlsx)的读取(ExcelReader)
Excel文件(xls,xlsx)的写出(ExcelWriter)
Maven引入
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
借助Hutool的Poi工具写入Excel数据
Java对象ORM数据库表的实体
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author Administrator
* @apiNote
* @date 2024/10/11 16:58
*/
@Data
public class PaymentVIewVO implements Serializable {
private Long mid;
private Integer pageSize;
private Integer currentPage;
private String goodsName;
private String paymentNo;
private String orderNo;
private String refundOldPaymentNo;
private String transactionType;
private String goodsType;
private Long price;
private Long amount;
private Long refundAmount;
private String queryParam;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTimeStart;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date refundSuccessTime;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date successTime;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTimeEnd;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date payTimeStart;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date payTimeEnd;
private String channelReturnMessage;
private String defaultValue = "3";
private boolean isFlag;
}
Mybatis对象映射的XML
<resultMap id="voView" type="com.cashier.domain.model.PaymentVIewVO">
<id column="mid" jdbcType="BIGINT" property="mid"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="success_time" property="successTime" jdbcType="TIMESTAMP"/>
<result column="channel_refund_success_time" property="refundSuccessTime" jdbcType="TIMESTAMP"/>
<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
<result column="order_no" property="orderNo" jdbcType="VARCHAR"/>
<result column="channel_flow_no" property="refundOldPaymentNo" jdbcType="VARCHAR"/>
<result column="channel_return_message" property="channelReturnMessage" jdbcType="VARCHAR"/>
<result column="goods_type" property="goodsType" jdbcType="VARCHAR"/>
<result column="payment_no" property="paymentNo" jdbcType="VARCHAR"/>
<result column="price" property="price" jdbcType="NUMERIC"/>
<result column="amount" property="amount" jdbcType="NUMERIC"/>
<result column="refund_amount" property="refundAmount" jdbcType="NUMERIC"/>
<result column="transaction_type" property="transactionType" jdbcType="NUMERIC"/>
</resultMap>
<select id="listReconciliationByPage" resultMap="voView">
select
main.mid ,main.create_time ,main.success_time ,main.channel_refund_success_time ,main.goods_name ,main.order_no ,main.channel_return_message,
main.channel_flow_no ,main_price.goods_type ,main.payment_no ,main_price.price,main.transaction_type,main.amount,main.refund_amount
from cashier_payment main
left join tx_order_price_cd main_price on main.mid = main_price.order_id
<include refid="listReconciliation"></include>
order by main.mid desc
</select>
<sql id="listReconciliation">
<where>
<if test="bean.defaultValue == 3 ">
and main.transaction_type in (1,2) and main.state_id = 1 and main.status = 1
</if>
<if test="bean.defaultValue == 2 ">
and main.transaction_type = 2 and main.state_id = 1 and main.status = 1
</if>
<if test="bean.defaultValue == 1 ">
and main.transaction_type = 1 and main.state_id = 1 and main.status = 1
</if>
<if test="bean.queryParam != null and bean.queryParam != ''">
and main.pay_channel_id = #{bean.queryParam}
</if>
<if test="bean.createTimeStart != null">
and main.create_time >= #{bean.createTimeStart, jdbcType=TIMESTAMP}
and main.create_time <= #{bean.createTimeEnd, jdbcType=TIMESTAMP}
</if>
<if test="bean.payTimeStart != null">
and ((main.success_time >= #{bean.payTimeStart, jdbcType=TIMESTAMP}
and main.success_time <= #{bean.payTimeEnd, jdbcType=TIMESTAMP} )
or (main.channel_refund_success_time >= #{bean.payTimeStart, jdbcType=TIMESTAMP}
and main.channel_refund_success_time <= #{bean.payTimeEnd, jdbcType=TIMESTAMP}))
</if>
<if test="bean.orderNo != null and bean.orderNo != ''">
and main.order_no = #{bean.orderNo}
</if>
<if test="bean.price != null and bean.price != ''">
and main_price.price = #{bean.price}
</if>
<if test="bean.paymentNo != null and bean.paymentNo != ''">
and main.payment_no = #{bean.paymentNo}
</if>
<if test="bean.channelReturnMessage != null and bean.channelReturnMessage != ''">
and main.channel_return_message = #{bean.channelReturnMessage}
</if>
<if test="bean.amount != null and bean.amount != ''">
and (main.amount = #{bean.amount} or main.refund_amount = #{bean.amount})
</if>
<if test="bean.goodsType != null and bean.goodsType != ''">
<bind name="goodsTypeLike" value="'%'+ bean.goodsType + '%'"/>
and main_price.goods_type like #{goodsTypeLike}
</if>
<if test="bean.goodsName != null and bean.goodsName != ''">
<bind name="goodsNameLike" value="'%'+ bean.goodsName + '%'"/>
and main.goods_name like #{goodsNameLike,jdbcType=VARCHAR}
</if>
</where>
</sql>
查询出List-PaymentVIewVO
List<PaymentVIewVO> result = paymentRepository.listReconciliationByLimit(paymentVIewVO, currentPage,currentSize);
将List-PaymentVIewVO转化为Excel导出的实体
private List<PaymentToExcelFile> getPaymentToExcelFiles(List<PaymentVIewVO> result) {
List<PaymentToExcelFile> collect = result.stream().map(x -> {
PaymentToExcelFile paymentToExcelFile = new PaymentToExcelFile();
paymentToExcelFile.setOrderNo(x.getOrderNo());
paymentToExcelFile.setCreateTime(x.getCreateTime());
if (x.getTransactionType().equals("1")) {
paymentToExcelFile.setTransactionType("支付");
paymentToExcelFile.setAmount(x.getAmount() == null ? null : x.getAmount().toString());
paymentToExcelFile.setSuccessTime(x.getSuccessTime());
} else {
paymentToExcelFile.setTransactionType("退款");
paymentToExcelFile.setAmount(x.getRefundAmount() == null ? null : x.getRefundAmount().toString());
paymentToExcelFile.setSuccessTime(x.getRefundSuccessTime());
paymentToExcelFile.setChannelReturnMessage(x.getChannelReturnMessage());
}
paymentToExcelFile.setPrice(x.getPrice() == null ? null : x.getPrice().toString());
paymentToExcelFile.setGoodsName(x.getGoodsName());
paymentToExcelFile.setGoodsType(x.getGoodsType());
paymentToExcelFile.setPaymentNo(x.getPaymentNo());
return paymentToExcelFile;
}).collect(Collectors.toList());
return collect;
}
将List-PaymentToExcelFile实体写入Excel
ExcelWriter writer = null;
try {
String os = System.getProperty("os.name");
String filePath = "/tmp/tempPayment.xlsx";
if (os.toLowerCase().startsWith("win")) {
filePath = "d:/tmp/tempPayment.xlsx";
}
writer = ExcelUtil.getWriter(filePath);
//自定义标题别名
writer.addHeaderAlias("createTime", "交易创建时间");
writer.addHeaderAlias("successTime", "交易成功时间");
writer.addHeaderAlias("orderNo", "业务订单号");
writer.addHeaderAlias("channelReturnMessage", "原业务订单号");
writer.addHeaderAlias("goodsName", "业务来源");
writer.addHeaderAlias("transactionType", "交易类型");
writer.addHeaderAlias("amount", "交易金额");
writer.addHeaderAlias("goodsType", "分账方收款主体");
writer.addHeaderAlias("price", "分账金额");
writer.addHeaderAlias("paymentNo", "渠道的商户订单号");
// 默认的,未添加alias的属性也会写出,如果想只写出加了别名的字段,可以调用此方法排除之
writer.setOnlyAlias(true);
List<PaymentToExcelFile> paymentToExcelFileList=null;
for (int i = 0; i <= index; i++) {
writer.write(paymentToExcelFileList, true);
writer.flush();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
if (writer != null) {
writer.close();
}
}
Java读取Excel导出
/**
* 导出对账单
*
* @param response request
*/
@RequestMapping(value = "/exportFile", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public void exportFile(HttpServletResponse response) throws IOException {
String os = System.getProperty("os.name");
String filePath = "/tmp/tempPayment.xlsx";
if (os.toLowerCase().startsWith("win")) {
filePath = "d:/tmp/tempPayment.xlsx";
}
OutputStream outputStream = null;
File file = null;
try {
file = new File(filePath);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(yyyyMMdd + "对账明细数据.xlsx", "UTF-8"));
outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(file);
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null && file.exists()) {
file.delete();
}
}
}
查看Excel导出结果


2762

被折叠的 条评论
为什么被折叠?



