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 = "[email protected]"; // 发送邮件邮箱
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("[email protected]", "测试邮件标题", "邮件信息主体", 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;
}
}
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;
}
}
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;
}
}