当前位置:   article > 正文

Java后台创建Excel文档通过邮件发送字节流,本地不生成实体文件_字节流不产生本地文件file 对象

字节流不产生本地文件file 对象

java后台创建Excel实体文档跳转

Java后台创建Excel文档通过字节流发送,不生成实体文件:
poi.jar包下载

Test类

package com.gaohan.universal.emailtest;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * @author gaohan
 * @version 1.0
 * @date 2020/6/3 22:38
 */
public class Test {
    private static final String EMAIL_SENDER = "834363368@qq.com";  // 发送邮件邮箱
    private static final String EMAIL_KEY = "XXXXXXXX";     //授权码
    private static final String EMAIL_HOST = "smtp.qq.com";         // QQ 邮件服务器

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\gaohan\\Pictures\\Camera Roll\\nic.png");
        byte[] fileByte = new byte[0];
        fileByte = Files.readAllBytes(file.toPath());
        Test.sendEmailByQQ("3508940548@qq.com", "测试邮件标题", "邮件信息主体", CreateExcelFlow.createExcel());
    }

    public static boolean sendEmailByQQ(String toEmail, String titleEmail, String mainBody, byte[] fileBytes) {
        boolean flag = false;
        // 收件人邮箱,不仅仅QQ邮箱
        String to = toEmail;
        
        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置发送邮件的邮件服务器
        properties.setProperty("mail.smtp.host", EMAIL_HOST);

        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(EMAIL_SENDER, EMAIL_KEY); // 发件人电子邮箱,你生成授权码的QQ邮箱
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(EMAIL_SENDER));
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // Set Subject: 头部头字段
            message.setSubject(titleEmail);    // 邮件信息标题
            // 设置消息体
            Multipart multipart = new MimeMultipart();
            BodyPart bodyPart = new MimeBodyPart();
            // 邮件信息内容
            bodyPart.setText(mainBody);
//            bodyPart.setContent(mainBody, "text/html;charset=utf-8");
            multipart.addBodyPart(bodyPart);

            bodyPart = new MimeBodyPart();
            DataSource source = new ByteArrayDataSource(fileBytes, "application/excel");
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText("TEST.xls"));
            multipart.addBodyPart(bodyPart);


            message.setContent(multipart);
            // 发送消息
            Transport.send(message);
            flag = true;
        } catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return flag;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

CreateExcelFlow类

package com.gaohan.universal.emailtest;


import org.apache.poi.hssf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author gaohan
 * @version 1.0
 * @date 2020/6/2 18:23
 */
public class CreateExcelFlow {
    public static byte[] createExcel() {
        //获取表头
        String[] titile = createTitle();
        String[][] content = new String[5][];
        //处理数据
        for (int i = 0; i < 5; i++) {
            content[i] = new String[titile.length];
            content[i][0] = "REGION";
            content[i][1] = "SUPPLIER";
            content[i][2] = "COMMODITY_NAME";
            content[i][3] = "COMMODITY_CODE";
            content[i][4] = "OPERATION_STATUS";
            content[i][5] = "TURNOVER_DAYS";
            content[i][6] = "PURCHASE_QUANTITY";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook("提醒单", titile, content, null);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bt = os.toByteArray();
        return bt;
    }

    /**
     * 获取标题
     *
     * @return
     */
    private static String[] createTitle() {
        String[] title = new String[]{"地区", "供应商", "商品名称", " 商品编码", "运营状态", "周转天数", "建议采购量"};
        return title;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

ExcelUtil类

package com.gaohan.universal.emailtest;

import org.apache.poi.hssf.usermodel.*;

/**
 * @author gaohan
 * @version 1.0
 * @date 2020/6/2 18:40
 */
public class ExcelUtil {
    /**
     * 导出Excel
     *
     * @param sheetName sheet名称
     * @param title     标题
     * @param values    内容
     * @param wb        HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
//        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
//            cell.setCellStyle(style);
        }

        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

效果图如下:
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/591899
推荐阅读
相关标签
  

闽ICP备14008679号