Excel导出与上传实现,包含单元格样式,合并单元格,对象属性的属性值获取等

本文介绍了如何在Java中实现Excel导出和上传,包括根据页面选择导出属性列、处理复杂对象属性、自定义单元格样式和合并。通过自定义下载注解、导出Dto对象、导出工具类ExcelWriter和上传工具类ExcelReader,详细讲解了导出与上传的逻辑和关键方法,如getWeekDataValue和getSetterMethod。
摘要由CSDN通过智能技术生成

年前上线的最后一个需求是对业务系统进行界面优化,其中我所负责的是按照优化界面进行定制化的Excel导出和上传。和以往导出上传不同点在于:
1、导出项会根据页面的不同选择导出相应的属性列;
2、导出DTO对象中不再是仅包含导出属性,还包含属性对象,属性对象中还有自定义属性,而导出属性除了DTO中的属性外,还得导出属性对象中的自定义属性值;
3、导出表头背景色不同,数据格式不同,且存在合并单元格等。

本文仅对所做的下载与上传部分作逻辑说明,对以后进行相关开发起到一个参考借鉴作用。注意两个方法:导出多元属性解析的getWeekDataValue和上传多元属性解析的getSetterMethod。

一、自定义下载注解:ColumnDesc

该下载注解,主要是为了在下载时,根据所写的下载规则,获取其注解内的title等信息,从而构建出所需要的Excel表头。

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnDesc {
    /**
     *  title,可以用来构建表头的文字内容
    */
    String title() default "";
    /**
    * index,可以用来指定表头的顺序
    */
    int index() default 0;
}

二、导出Dto对象,也可作上传Dto对象

@Getter
@Setter
@ToString
public class ExportDto {  // 导出对象
    @ColumnDesc(title = "商品编码", index = 0)
    private String id;
    
    @ColumnDesc(title = "商品名称", index = 0)
    private String name;
    
    @ColumnDesc
    private WeekData weekData;// 周数据
}

@Getter
@Setter
@ToString
public class WeekData { //周数据对象
    @ColumnDesc(title = "销售数量")
    private double sales;
    @ColumnDesc(title = "销售金额")
    private double saleroom;
}

如上所示,我们构建了一个导出Dto,其属性赋予了自定义注解,Dto内还设置了一个对象属性,该对象属性内的属性是我们所要构建表头时所需要的内容。

三、导出工具类ExcelWriter

1、ExcelWriter属性及一些导出设置

由于导出涉及到合并单元格等问题,因此设计了两个header。以下为导出类的一些属性。

public class ExcelWriter<T> {
    /**
     *  表头1,含有合并项的
     */
    private List<HeaderProperties> firstHeader;
    /**
     *  表头2,和数据项一一对应的表头
     */
    private List<HeaderProperties> secondHeader;
    /**
     * 组装SecondHeader表头
     */
    private List<TitleAndProperty> titleAndPropertyList;
    /**
     * 传入的数据集合
     */
    private List<T> contentList;
    /**
     * Excel版本
     */
    private Excel version;
    /**
     * 最大写入限制,默认-1 不限制
     */
    private int maxExportNum = -1;
    /**
     * 第一行是否换行,不换行则高度默认
     */
    private boolean isAutoNewLineOfFirstHeader =  false;
    /**
     *  表示数据的类型
     */
    private Class dataClazz;
}

其中,HeaderProperties是为了设置表头单元格的样式,是否合并单元格,如要合并,则合并长度是多少。

@Getter
@Setter
public class HeaderProperties {
    // 标题
    String title;
    // 是否合并单元格,默认不合并
    boolean isMerged = false;
    // 合并单元格的长度,为0则不合并
    int mergeLength;
    // 单元格样式问题
    CellStyleProperties cellStyleProperties;
    //  表头顺序
    int order;

    public HeaderProperties(String title, boolean isMerged, int mergeLength,
        CellStyleProperties cellStyleProperties, int order) {
        this.title = title;
        this.isMerged = isMerged;
        this.mergeLength = mergeLength;
        this.cellStyleProperties = cellStyleProperties;
        this.order = order;
    }

    public HeaderProperties(String title, CellStyleProperties cellStyleProperties, int order) {
        this.title = title;
        this.isMerged = false;
        this.mergeLength = 0;
        this.cellStyleProperties = cellStyleProperties;
        this.order = order;
    }

    public static List<HeaderProperties> listHeaderProperties(List<String> titles, CellStyleProperties cellStyleProperties, int order) {
        List<HeaderProperties> headerProperties = Lists.newArrayList();
        titles.forEach(title -> {
            headerProperties.add(new HeaderProperties(title, cellStyleProperties, order));
        });

        return headerProperties;
    }
}

单元格的格式设置如下:

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;

@Data
@NoArgsConstructor
public class CellStyleProperties {
    String fontName;//字体
    short fontBoldweight;//字体线条粗细
    short fontHeightInPoints;//字体大小
    short fontColor;//字体颜色
    short fillForegroundColor;//背景色
    // 填充方式
    short fillPattern;
    // 是否自动换行
    boolean autoWrapText;
    // 水平对齐格式
    short alignCenter;
    //设置上下对齐方式
    short crossCenter;
    // 左边框格式
    short borderLeft;
    //边框颜色
    short borderLeftColor;
    // 上边框格式
    short borderTop;
    //边框颜色
    short borderTopColor;
    // 右边框格式
    short borderRight;
    //边框颜色
    short borderRightColor;
    // 下边框格式
    short borderBottom;
    //边框颜色
    short borderBottomColor;


    public CellStyleProperties(String fontName, short fontBoldweight, short fontHeightInPoints,
        short fontColor, short fillForegroundColor, short fillPattern, boolean autoWrapText,
        short alignCenter, short crossCenter, short borderLeft, short borderLeftColor,
        short borderTop, short borderTopColor, short borderRight, short borderRightColor, short borderBottom,
        short borderBottomColor) {
        this.fontName = fontName;
        this.fontBoldweight = fontBoldweight;
        this.fontHeightInPoints = fontHeightInPoints;
        this.fontColor = fontColor;
        this.fillForegroundColor = fillForegroundColor;
        this.fillPattern = fillPattern;
        this.autoWrapText = autoWrapText;
        this.alignCenter = alignCenter;
        this.crossCenter = crossCenter;
        this.borderLeft = borderLeft;
        this.borderLeftColor = borderLeftColor;
        this.borderTop = borderTop;
        this.borderTopColor = borderTopColor;
        this.borderRight = borderRight;
        this.borderRightColor = borderRightColor;
        this.borderBottom = borderBottom;
        this.borderBottomColor = borderBottomColor;
    }

    /**
     *  默认边框格式
     * @param fontName
     * @param fontBoldweight
     * @param fontHeightInPoints
     * @param fontColor
     * @param fillForegroundColor
     * @param fillPattern
     * @param alignCenter
     */
    public CellStyleProperties(String fontName, short fontBoldweight, short fontHeightInPoints,
        short fontColor, short fillForegroundColor, short fillPattern, boolean autoWrapText, short alignCenter) {
        this(fontName, fontBoldweight, fontHeightInPoints, fontColor, fillForegroundColor,
            fillPattern, autoWrapText, alignCenter, HSSFCellStyle.VERTICAL_CENTER,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
    }

    /**
     *  默认边框格式为25%的灰色
     * @param fontName
     * @param fontBoldweight
     * @param fontHeightInPoints
     * @param fontColor
     * @param fillForegroundColor
     * @param fillPattern
     * @param alignCenter
     */
    public CellStyleProperties(String fontName, short fontBoldweight, short fontHeightInPoints,
        short fontColor, short fillForegroundColor, short fillPattern, boolean autoWrapText, short alignCenter, short crossCenter) {
        this(fontName, fontBoldweight, fontHeightInPoints, fontColor, fillForegroundColor,
            fillPattern, autoWrapText, alignCenter, crossCenter,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
    }

    /**
     *  默认边框格式为25%的灰色,默认对齐方式为左对齐
     * @param fontName
     * @param fontBoldweight
     * @param fontHeightInPoints
     * @param fontColor
     * @param fillForegroundColor
     * @param fillPattern
     */
    public CellStyleProperties(String fontName, short fontBoldweight, short fontHeightInPoints,
        short fontColor, short fillForegroundColor, short fillPattern) {
        this(fontName, fontBoldweight, fontHeightInPoints, fontColor, fillForegroundColor,
            fillPattern, false, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
    }

    /**
     *  除了填充颜色外,其余均默认
     *  微软雅黑,加粗,字体大小9,字体颜色黑色,背景全填充,居中
     *  边框颜色为灰色
     * @param fillForegroundColor
     */
    public CellStyleProperties(short fillForegroundColor) {
        this("微软雅黑", HSSFFont.BOLDWEIGHT_BOLD, (short)9, HSSFColor.BLACK.index, fillForegroundColor,
            HSSFCellStyle.SOLID_FOREGROUND, false,
            HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index,
            HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
    }

    /
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值