使用poi导出excel 一张表 三个sheet 数据为三个不同的对象集合 写个案例
时间: 2024-02-16 22:02:44 浏览: 26
好的,以下是一个使用poi导出excel的案例,其中包含三个sheet,每个sheet的数据来自不同的对象集合。
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExportExample {
public static void main(String[] args) throws IOException {
// 创建一个Workbook
Workbook workbook = new XSSFWorkbook();
// 创建第一个Sheet,名为"Sheet1"
Sheet sheet1 = workbook.createSheet("Sheet1");
// 创建第二个Sheet,名为"Sheet2"
Sheet sheet2 = workbook.createSheet("Sheet2");
// 创建第三个Sheet,名为"Sheet3"
Sheet sheet3 = workbook.createSheet("Sheet3");
// 准备Sheet1的数据
List<Sheet1Data> sheet1DataList = new ArrayList<>();
sheet1DataList.add(new Sheet1Data("张三", 20, "男"));
sheet1DataList.add(new Sheet1Data("李四", 25, "女"));
sheet1DataList.add(new Sheet1Data("王五", 30, "男"));
// 写入Sheet1的数据
int rowIndex = 0;
for (Sheet1Data data : sheet1DataList) {
Row row = sheet1.createRow(rowIndex++);
Cell cell0 = row.createCell(0);
cell0.setCellValue(data.getName());
Cell cell1 = row.createCell(1);
cell1.setCellValue(data.getAge());
Cell cell2 = row.createCell(2);
cell2.setCellValue(data.getGender());
}
// 准备Sheet2的数据
List<Sheet2Data> sheet2DataList = new ArrayList<>();
sheet2DataList.add(new Sheet2Data("A", 80));
sheet2DataList.add(new Sheet2Data("B", 75));
sheet2DataList.add(new Sheet2Data("C", 90));
// 写入Sheet2的数据
rowIndex = 0;
for (Sheet2Data data : sheet2DataList) {
Row row = sheet2.createRow(rowIndex++);
Cell cell0 = row.createCell(0);
cell0.setCellValue(data.getName());
Cell cell1 = row.createCell(1);
cell1.setCellValue(data.getScore());
}
// 准备Sheet3的数据
List<Sheet3Data> sheet3DataList = new ArrayList<>();
sheet3DataList.add(new Sheet3Data(1, "A", "2021-01-01"));
sheet3DataList.add(new Sheet3Data(2, "B", "2021-01-02"));
sheet3DataList.add(new Sheet3Data(3, "C", "2021-01-03"));
// 写入Sheet3的数据
rowIndex = 0;
for (Sheet3Data data : sheet3DataList) {
Row row = sheet3.createRow(rowIndex++);
Cell cell0 = row.createCell(0);
cell0.setCellValue(data.getId());
Cell cell1 = row.createCell(1);
cell1.setCellValue(data.getName());
Cell cell2 = row.createCell(2);
cell2.setCellValue(data.getDate());
}
// 将Workbook写入文件
FileOutputStream outputStream = new FileOutputStream("example.xlsx");
workbook.write(outputStream);
outputStream.close();
System.out.println("Excel文件已成功导出。");
}
// Sheet1的数据对象
static class Sheet1Data {
private String name;
private int age;
private String gender;
public Sheet1Data(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
// Sheet2的数据对象
static class Sheet2Data {
private String name;
private int score;
public Sheet2Data(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
// Sheet3的数据对象
static class Sheet3Data {
private int id;
private String name;
private String date;
public Sheet3Data(int id, String name, String date) {
this.id = id;
this.name = name;
this.date = date;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDate() {
return date;
}
}
}
```
这个例子中,我们创建了一个Workbook,并创建了三个Sheet,分别为"Sheet1"、"Sheet2"、"Sheet3"。我们准备了三个不同的对象集合,分别为Sheet1Data、Sheet2Data、Sheet3Data,并将它们写入到Workbook的相应Sheet中。最后,将Workbook写入到文件中。
在这个例子中,我们使用了XSSFWorkbook类来创建Workbook,并使用了createSheet()方法来创建Sheet。我们通过创建Row对象和Cell对象来写入数据,然后通过FileOutputStream将Workbook写入到文件中。
相关推荐
![](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)
![](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)
![](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)
![](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)
![](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)