java发送邮件-获取包含表格的html内容

1 依赖


    commons-beanutils
    commons-beanutils
    1.9.4

2 工具类

/**
 * 获取包含表格的html内容
 * @param titleMap 表头
 * @param propsMap 字段属性
 * @param attachList 表格数据
 * @return
 */
public static String getHtmlContent(Map titleMap, Map propsMap, Map attachList) {
    if (titleMap.isEmpty() || propsMap.isEmpty() || attachList.isEmpty()) {
        return null;
    }

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("\r\n");

    stringBuilder.append("");
    for (Map.Entry entry : attachList.entrySet()) {
        String tableName = entry.getKey();

        stringBuilder.append("

" + tableName + "

"); stringBuilder.append(""); String[] titles = titleMap.get(tableName); if (titles == null) { log.warn("表头为空。sheetName:{}", tableName); stringBuilder.append("表头为空"); stringBuilder.append("\r\n"); continue; } String[] props = propsMap.get(tableName); if (props == null) { log.warn("属性名称为空。tableName:{}", tableName); stringBuilder.append("属性名称为空"); stringBuilder.append("\r\n"); continue; } List list = entry.getValue(); if (CollectionUtils.isEmpty(list)) { log.warn("暂无数据。tableName:{}", tableName); stringBuilder.append("暂无数据"); stringBuilder.append("\r\n"); continue; } // 设置表格表头 stringBuilder.append(""); for (String title : titles) { stringBuilder.append(""); } stringBuilder.append(""); // 设置表格内容 for (Object obj : list) { stringBuilder.append(""); for (String prop : props) { try { Object fieldValue = PropertyUtils.getProperty(obj, prop); if (fieldValue == null) { fieldValue = ""; } stringBuilder.append(""); } catch (Exception e) { log.error("填充列表失败", e); } } stringBuilder.append(""); } stringBuilder.append("
" + title + "
" + fieldValue + "
"); stringBuilder.append("\r\n"); } stringBuilder.append(""); return stringBuilder.toString(); }

3 测试

public static void main(String[] args) {
    List list4VerifyOverview = new ArrayList<>();
    List list4Warehouse = new ArrayList<>();

    String[] titles4VerifyOverview = new String[]{"", "总审核次数", "审核通过次数", "审核不通过次数", "审核不通过率"};
    String[] titles4WarehouseVerify = new String[]{"商家", "总审核次数", "审核通过次数", "审核不通过次数", "审核不通过率"};
    String[] props4VerifyNum = new String[]{"desc", "totalVerifyNum", "verifyPassNum", "verifyRefuseNum", "verifyRefuseRatio"};

    // 表头
    Map titleMap = new HashMap<>();
    titleMap.put("审核数量概览", titles4VerifyOverview);
    titleMap.put("各仓审核数量统计", titles4WarehouseVerify);

    // 字段属性
    Map propsMap = new HashMap<>();
    propsMap.put("审核数量概览", props4VerifyNum);
    propsMap.put("各仓审核数量统计", props4VerifyNum);

    // 表格数据
    Map dataMap = new LinkedHashMap<>();
    dataMap.put("审核数量概览", list4VerifyOverview);
    dataMap.put("各仓审核数量统计", list4Warehouse);

    String htmlContent = getHtmlContent(titleMap, propsMap, dataMap);
}

VerifyResultDto

@Data
public class VerifyResultDto implements Serializable {
    private static final long serialVersionUID = -2391266072403916849L;

    /**
     * 描述
     */
    private String desc;
    /**
     * 总审核次数
     */
    private Long totalVerifyNum;
    /**
     * 审核通过次数
     */
    private Long verifyPassNum;
    /**
     * 审核不通过次数
     */
    private Long verifyRefuseNum;
    /**
     * 审核不通过率
     */
    private String verifyRefuseRatio;

}

4 备注

包含表格的html页面的基本结构


    

定义标题

定义表头
定义单元格

5 参考

(1)Java使用html样式发送带表格的邮件_岁月静静好的博客-CSDN博客

你可能感兴趣的:(开发包,java,发送邮件,表格,html)