[Email sending] Display the list data in the email body in html format

Signature: But do good deeds, don't ask about the future.


foreword

The company's products put forward a new requirement, which requires displaying the data in Excel that does not meet the profit and inventory of group-buying products in the body of the email (previously, Excel was originally sent to the mailbox as an attachment). Requirements: The data displayed in the text should be beautiful.


1. Demand analysis

  • The data that can already be obtained: the list data that does not meet the profit and inventory of group buying products, the array of mailboxes of people who need to send emails, and the tool class for sending emails.
  • The part that needs to be optimized: display the list data in the email body.
  • Solution: Extract the list data into a table form, and then display it in the email body in the form of html (ps: emails support parsing html content)

2. Code writing

if(profitVOList != null && profitVOList.size() > 0){
    
    
                String subject = "团购商品利润和库存预警";
                //Step3:创建临时表格文件
                String fileName =  new String(subject.getBytes("utf-8"));
                File sheetFile = new File(fileName + ".xlsx");
                ExcelUtil.writeExcel(sheetFile, EmallGoodsStockAndProfitVO.class, profitVOList);

                if (notifyEmail != null) {
    
    
                    logger.info("邮件通知人[{}],[{}]", sendTo,sendTo2);
                    logger.info("邮件通知内容[{}]", profitVOList);

                    // Excel内容在邮件正文显示
                    StringBuilder htmlText = new StringBuilder();
                    String htmlStar = "<html><body><table border=\"1px\" cellpadding=\"10px\">";
                    String htmlEnd = "</table></body></html>";
                    // 表头
                    String th = "<tr>" +
                            "<th>营销活动ID</th>" +
                            "<th>营销活动</th>" +
                            "<th>场次ID</th>" +
                            "<th>场次名称</th>" +
                            "<th>场次开始时间</th>" +
                            "<th>场次结束时间</th>" +
                            "<th>商品ID</th>" +
                            "<th>商品名称</th>" +
                            "<th>利润率</th>" +
                            "<th>库存</th></tr>";
                    // 行数据
                    StringBuilder tr = new StringBuilder();
                    // for循坏table的行数据
                    for (int i = 0; i < profitVOList.size(); i++) {
    
    
                        // 只展示前20条数据
                        if (i == 20) {
    
    
                            tr.append("更多数据请打开Excel附件查看详情O(∩_∩)O~");
                            break;
                        }
                        // 依次获取表头对应的字段值
                        tr.append(
                                "<tr>" +
                                        "<td>" + profitVOList.get(i).getProjectId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getProjectName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionStartTime() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionEndTime() + "</td>" +
                                        "<td>" + profitVOList.get(i).getGoodsId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getGoodsName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getProfit() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSurplusNum() + "</td>" +
                                        "</tr>");
                    }
                    // 拼接HTML页面
                    htmlText.append(htmlStar);
                    htmlText.append(th);
                    htmlText.append(tr);
                    htmlText.append(htmlEnd);
                    notifyService.notifyMailWithAttachmentAndHtmlText(emailStr.split(","), fileName, sheetFile, String.valueOf(htmlText), false);
                }
            } else {
    
    
                logger.info("团购商品活动利润正常,无需预警, [{}]");
            }

3. Solutions

  1. Define html, body, table head and tail
 String htmlStar = "<html><body><table border=\"1px\" cellpadding=\"10px\">";
 String htmlEnd = "</table></body></html>";
  1. Define the header of the table
// 表头
                    String th = "<tr>" +
                            "<th>营销活动ID</th>" +
                            "<th>营销活动</th>" +
                            "<th>场次ID</th>" +
                            "<th>场次名称</th>" +
                            "<th>场次开始时间</th>" +
                            "<th>场次结束时间</th>" +
                            "<th>商品ID</th>" +
                            "<th>商品名称</th>" +
                            "<th>利润率</th>" +
                            "<th>库存</th></tr>";
  1. The for loop badly traverses the list to generate row data
// 行数据
                    StringBuilder tr = new StringBuilder();
                    // for循坏table的行数据
                    for (int i = 0; i < profitVOList.size(); i++) {
    
    
                        // 只展示前20条数据
                        if (i == 20) {
    
    
                            tr.append("更多数据请打开Excel附件查看详情O(∩_∩)O~");
                            break;
                        }
                        // 依次获取表头对应的字段值
                        tr.append(
                                "<tr>" +
                                        "<td>" + profitVOList.get(i).getProjectId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getProjectName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionStartTime() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSessionEndTime() + "</td>" +
                                        "<td>" + profitVOList.get(i).getGoodsId() + "</td>" +
                                        "<td>" + profitVOList.get(i).getGoodsName() + "</td>" +
                                        "<td>" + profitVOList.get(i).getProfit() + "</td>" +
                                        "<td>" + profitVOList.get(i).getSurplusNum() + "</td>" +
                                        "</tr>");
                    }
  1. Splicing HTML pages
// 拼接HTML页面
                    htmlText.append(htmlStar);
                    htmlText.append(th);
                    htmlText.append(tr);
                    htmlText.append(htmlEnd);
  1. Call the mail send method
notifyService.notifyMailWithAttachmentAndHtmlText(emailStr.split(","), fileName, sheetFile, String.valueOf(htmlText), false);

4. The final effect

Display the list data in the email body in html format

insert image description here


Summarize

The blog mainly records the problem of displaying list data in html format to the body of the email during work. Please correct me if there are any mistakes or deficiencies. If it is helpful to you, please click three times.

Guess you like

Origin blog.csdn.net/YangCunle/article/details/129669132