Java查询的数据生成excel并发送邮件
时间: 2024-10-15 17:22:22 浏览: 32
在Java中,我们可以使用Apache POI库来操作Excel文件,包括创建、读取和写入数据。而Spring Boot等框架可以方便地集成邮箱服务,如JavaMail API来发送电子邮件。以下是一个简单的步骤概述:
1. **导入依赖**:
首先,在Maven或Gradle项目中添加Apache POI(`poi`和`poi-ooxml`)以及JavaMail API的依赖。
```xml
<!-- Maven -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.6.2</version>
</dependency>
// Gradle
implementation 'org.apache.poi:poi:5.0.0'
implementation 'org.apache.poi:poi-ooxml:5.0.0'
implementation 'com.sun.mail:javax.mail:1.6.2'
```
2. **生成Excel**:
使用`HSSFWorkbook`或`XSSFWorkbook`创建一个新的工作簿,然后通过`Sheet`对象写入数据。例如,创建行和单元格:
```java
import org.apache.poi.ss.usermodel.*;
...
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello Excel!");
```
3. **发送邮件**:
创建`SimpleMailMessage`对象,并设置收件人、主题、内容以及附件(Excel文件)。然后使用`JavaMailSender`发送邮件:
```java
import org.springframework.mail.javamail.JavaMailSender;
...
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("recipient@example.com");
message.setSubject("Excel attachment");
message.setText("Please find the attached file.");
MimeMessage mimeMessage = mailSender.createMimeMessage();
Part excelPart = new ByteArrayResource(workbook.getBytes()) {
@Override
public String getContentType() {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
@Override
public String getName() {
return "example.xlsx";
}
};
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.attach(excelPart);
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
mimeMessage.setContent(multipart);
mailSender.send(mimeMessage);
```
4. **整合到Spring Boot**:
如果你在一个Spring Boot应用中,需要在`@RestController`或其他处理程序中组合这两个功能,将上述代码封装成Service或Helper类,并在适当的地方调用。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)