商品导出
This commit is contained in:
@@ -2,12 +2,11 @@ package com.czg.product.dto;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品导出
|
||||
@@ -17,112 +16,61 @@ import java.util.List;
|
||||
@Data
|
||||
public class ProductExportDTO {
|
||||
|
||||
@Data
|
||||
public static class ProductSkuExportDTO {
|
||||
|
||||
@ExcelProperty("条形码")
|
||||
private String barCode;
|
||||
|
||||
@ExcelProperty("原价")
|
||||
private BigDecimal originPrice;
|
||||
|
||||
@ExcelProperty("成本价")
|
||||
private BigDecimal costPrice;
|
||||
|
||||
@ExcelProperty("会员价")
|
||||
private BigDecimal memberPrice;
|
||||
|
||||
@ExcelProperty("售价")
|
||||
private BigDecimal salePrice;
|
||||
|
||||
@ExcelProperty("起售数量")
|
||||
private Integer suitNum;
|
||||
|
||||
@ExcelProperty("规格详情")
|
||||
private String specInfo;
|
||||
|
||||
@ExcelProperty("是否上架")
|
||||
private Integer isGrounding;
|
||||
|
||||
|
||||
@ExcelProperty("规格名称")
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ProductGroupExportDTO {
|
||||
@ExcelProperty("套餐内商品总数")
|
||||
private Integer count;
|
||||
|
||||
@ExcelProperty("套餐选几")
|
||||
private Integer number;
|
||||
|
||||
@ExcelProperty("套餐名称")
|
||||
private String title;
|
||||
|
||||
@ExcelProperty("套餐内商品列表")
|
||||
private List<FoodExportDTO> goods = new ArrayList<>();
|
||||
|
||||
@Data
|
||||
public static class FoodExportDTO {
|
||||
|
||||
@ExcelProperty("商品名称")
|
||||
private String proName;
|
||||
|
||||
@ExcelProperty("规格名称")
|
||||
private String skuName;
|
||||
}
|
||||
}
|
||||
|
||||
@ExcelProperty("商品名称")
|
||||
@ColumnWidth(20)
|
||||
private String name;
|
||||
|
||||
@ExcelProperty("商品分类名称")
|
||||
@ExcelProperty("商品分类")
|
||||
@ColumnWidth(15)
|
||||
private String categoryName;
|
||||
|
||||
@ExcelProperty("条码")
|
||||
@ColumnWidth(20)
|
||||
private String barCode;
|
||||
|
||||
@ExcelProperty("商品规格名称")
|
||||
@ExcelProperty("商品规格")
|
||||
@ColumnWidth(20)
|
||||
private String specFullName;
|
||||
|
||||
@ExcelProperty("售价")
|
||||
@ColumnWidth(10)
|
||||
private BigDecimal price;
|
||||
@ExcelProperty("会员价")
|
||||
@ColumnWidth(10)
|
||||
private BigDecimal memberPrice;
|
||||
@ExcelProperty("成本价")
|
||||
@ColumnWidth(10)
|
||||
private BigDecimal costPrice;
|
||||
|
||||
@ExcelProperty("商品单位名称")
|
||||
@ExcelProperty("商品单位")
|
||||
@ColumnWidth(10)
|
||||
private String unitName;
|
||||
|
||||
/**
|
||||
* 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
|
||||
*/
|
||||
@ExcelProperty("商品类型")
|
||||
@ColumnWidth(15)
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 套餐类型 0 固定套餐 1可选套餐
|
||||
*/
|
||||
@ExcelIgnore
|
||||
private Integer groupType;
|
||||
@ExcelProperty("套餐类型")
|
||||
private String groupTypeRemark;
|
||||
|
||||
/**
|
||||
* 可用开始时间
|
||||
*/
|
||||
@ExcelProperty("可用开始时间")
|
||||
@ColumnWidth(16)
|
||||
private LocalTime startTime;
|
||||
/**
|
||||
* 可用结束时间
|
||||
*/
|
||||
@ExcelProperty("可用结束时间")
|
||||
@ColumnWidth(16)
|
||||
private LocalTime endTime;
|
||||
|
||||
/**
|
||||
* 商品级库存数量
|
||||
*/
|
||||
@ExcelProperty("库存数量")
|
||||
@ColumnWidth(10)
|
||||
private Integer stockNumber;
|
||||
|
||||
/**
|
||||
@@ -131,16 +79,10 @@ public class ProductExportDTO {
|
||||
@ExcelIgnore
|
||||
private Integer isSale;
|
||||
@ExcelProperty("是否上架")
|
||||
@ColumnWidth(10)
|
||||
private String isSaleRemark;
|
||||
|
||||
|
||||
@ExcelIgnore
|
||||
private List<ProductSkuExportDTO> skuList;
|
||||
|
||||
@ExcelIgnore
|
||||
private List<ProductGroupExportDTO> proGroupVo;
|
||||
|
||||
|
||||
public String getType() {
|
||||
return switch (type) {
|
||||
case "single" -> "单规格商品";
|
||||
@@ -152,17 +94,6 @@ public class ProductExportDTO {
|
||||
};
|
||||
}
|
||||
|
||||
public String getGroupTypeRemark() {
|
||||
if (!"package".equals(type)) {
|
||||
return "";
|
||||
}
|
||||
return switch (groupType) {
|
||||
case 0 -> "固定套餐";
|
||||
case 1 -> "可选套餐";
|
||||
case null, default -> "未知类型";
|
||||
};
|
||||
}
|
||||
|
||||
public String getIsSaleRemark() {
|
||||
return switch (isSale) {
|
||||
case 0 -> "下架";
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.czg.product.dto;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 套餐商品导出
|
||||
* @author yjjie
|
||||
* @date 2026/1/30 10:26
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ProductPackageExportDTO {
|
||||
|
||||
@ExcelProperty("套餐名称")
|
||||
@ColumnWidth(20)
|
||||
private String name;
|
||||
|
||||
@ExcelProperty("套餐分类")
|
||||
@ColumnWidth(15)
|
||||
private String categoryName;
|
||||
|
||||
@ExcelProperty("售价")
|
||||
@ColumnWidth(10)
|
||||
private BigDecimal price;
|
||||
@ExcelProperty("会员价")
|
||||
@ColumnWidth(10)
|
||||
private BigDecimal memberPrice;
|
||||
|
||||
/**
|
||||
* 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
|
||||
*/
|
||||
@ExcelIgnore()
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 套餐类型 0 固定套餐 1可选套餐
|
||||
*/
|
||||
@ExcelIgnore
|
||||
private Integer groupType;
|
||||
@ExcelProperty("套餐类型")
|
||||
@ColumnWidth(15)
|
||||
private String groupTypeRemark;
|
||||
|
||||
@ExcelProperty("组名称")
|
||||
@ColumnWidth(15)
|
||||
private String groupTitleName;
|
||||
|
||||
@ExcelProperty("商品名称")
|
||||
@ColumnWidth(21)
|
||||
private String groupProductName;
|
||||
|
||||
@ExcelProperty("商品单位")
|
||||
@ColumnWidth(10)
|
||||
private String unitName;
|
||||
|
||||
@ExcelProperty("套餐内选择数量")
|
||||
@ColumnWidth(10)
|
||||
private String groupProductNumber;
|
||||
|
||||
/**
|
||||
* 商品级库存数量
|
||||
*/
|
||||
@ExcelProperty("库存数量")
|
||||
@ColumnWidth(10)
|
||||
private Integer stockNumber;
|
||||
|
||||
/**
|
||||
* 是否上架
|
||||
*/
|
||||
@ExcelIgnore
|
||||
private Integer isSale;
|
||||
@ExcelProperty("是否上架")
|
||||
@ColumnWidth(10)
|
||||
private String isSaleRemark;
|
||||
|
||||
public String getGroupTypeRemark() {
|
||||
if (!"package".equals(type)) {
|
||||
return "";
|
||||
}
|
||||
return switch (groupType) {
|
||||
case 0 -> "固定套餐";
|
||||
case 1 -> "可选套餐";
|
||||
case null, default -> "未知类型";
|
||||
};
|
||||
}
|
||||
|
||||
public String getIsSaleRemark() {
|
||||
return switch (isSale) {
|
||||
case 0 -> "下架";
|
||||
case 1 -> "上架";
|
||||
case null, default -> "未知状态";
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.czg.excel;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
|
||||
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
||||
@@ -248,54 +251,66 @@ public class ExcelExportUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 带合并单元格的商品导出到Response
|
||||
* 带合并单元格的导出到Response
|
||||
* 多sheet导出到response
|
||||
*
|
||||
* @param data 数据列表
|
||||
* @param sheetDataList 数据列表
|
||||
* @param fileName 文件名
|
||||
* @param response HttpServletResponse
|
||||
* @param <T> 数据类型
|
||||
*/
|
||||
public static <T> void exportProductWithMergeToResponse(List<T> data, Class<T> clazz,
|
||||
String fileName,
|
||||
HttpServletResponse response, List<SheetWriteHandler> handlers) {
|
||||
if (data == null) {
|
||||
data = Collections.emptyList();
|
||||
public static void exportMultipleSheetsToResponse(List<SheetData> sheetDataList,
|
||||
String fileName,
|
||||
HttpServletResponse response) {
|
||||
if (CollectionUtil.isEmpty(sheetDataList)) {
|
||||
throw new CzgException("数据列表不能为空");
|
||||
}
|
||||
|
||||
setResponseHeader(response, fileName, DEFAULT_CONFIG);
|
||||
|
||||
try (OutputStream outputStream = response.getOutputStream()) {
|
||||
// 创建样式策略 - 设置表头和内容都居中
|
||||
// 创建样式策略
|
||||
WriteCellStyle headStyle = new WriteCellStyle();
|
||||
headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||
headStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
|
||||
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
WriteCellStyle contentStyle = new WriteCellStyle();
|
||||
contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||
contentStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
|
||||
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);
|
||||
|
||||
// 创建写入器 - 必须指定clazz
|
||||
ExcelWriterBuilder builder = EasyExcel.write(outputStream, clazz)
|
||||
// 创建ExcelWriter
|
||||
ExcelWriterBuilder builder = EasyExcel.write(outputStream)
|
||||
.autoCloseStream(true)
|
||||
.registerConverter(new LocalTimeConverter())
|
||||
.registerWriteHandler(styleStrategy);
|
||||
|
||||
if (handlers != null && !handlers.isEmpty()) {
|
||||
for (SheetWriteHandler h : handlers) {
|
||||
builder.registerWriteHandler(h);
|
||||
}
|
||||
}
|
||||
|
||||
ExcelWriter excelWriter = builder.build();
|
||||
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet(DEFAULT_CONFIG.getDefaultSheetName()).build();
|
||||
// 逐个写入sheet
|
||||
for (int i = 0; i < sheetDataList.size(); i++) {
|
||||
SheetData sheetData = sheetDataList.get(i);
|
||||
String sheetName = StrUtil.isNotBlank(sheetData.getSheetName())
|
||||
? sheetData.getSheetName()
|
||||
: DEFAULT_CONFIG.getDefaultSheetName() + (i + 1);
|
||||
|
||||
ExcelWriterSheetBuilder sheetBuilder = EasyExcel.writerSheet(sheetName);
|
||||
|
||||
// 注册该sheet的合并处理器
|
||||
if (sheetData.getHandlers() != null && !sheetData.getHandlers().isEmpty()) {
|
||||
for (SheetWriteHandler handler : sheetData.getHandlers()) {
|
||||
sheetBuilder.registerWriteHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
WriteSheet writeSheet = sheetBuilder.head(sheetData.getClazz()).build();
|
||||
excelWriter.write(sheetData.getData(), writeSheet);
|
||||
}
|
||||
|
||||
excelWriter.write(data, writeSheet);
|
||||
excelWriter.finish();
|
||||
|
||||
log.info("带合并单元格的商品Excel导出成功,文件名:{},数据量:{}", fileName, data.size());
|
||||
log.info("多sheet商品Excel导出成功,文件名:{},共{}个sheet", fileName, sheetDataList.size());
|
||||
} catch (IOException e) {
|
||||
log.error("带合并单元格的商品Excel导出失败", e);
|
||||
log.error("多sheet商品Excel导出失败", e);
|
||||
throw new CzgException("Excel导出失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.czg.excel;
|
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多sheet导出数据封装类
|
||||
* @author yjjie
|
||||
* @date 2026/1/30 10:53
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class SheetData {
|
||||
private List<?> data;
|
||||
private Class<?> clazz;
|
||||
private String sheetName;
|
||||
private List<SheetWriteHandler> handlers;
|
||||
}
|
||||
Reference in New Issue
Block a user