使用poi导出excel 一张表 三个sheet 数据为三个不同的对象集合 写个案例
时间: 2024-02-16 13:03:37 浏览: 28
好的,下面是使用poi导出excel的案例代码,该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 ExcelExport {
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
List<Car> carList = new ArrayList<>();
List<Book> bookList = new ArrayList<>();
// 生成测试数据
for (int i = 1; i <= 10; i++) {
User user = new User(i, "User" + i, 20 + i);
userList.add(user);
Car car = new Car(i, "Brand" + i, "Model" + i);
carList.add(car);
Book book = new Book(i, "Book" + i, "Author" + i);
bookList.add(book);
}
try (Workbook workbook = new XSSFWorkbook()) {
// 创建三个sheet
Sheet userSheet = workbook.createSheet("User");
Sheet carSheet = workbook.createSheet("Car");
Sheet bookSheet = workbook.createSheet("Book");
// 导出用户数据
int rowNum = 0;
Row userHeader = userSheet.createRow(rowNum++);
userHeader.createCell(0).setCellValue("ID");
userHeader.createCell(1).setCellValue("Name");
userHeader.createCell(2).setCellValue("Age");
for (User user : userList) {
Row row = userSheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getName());
row.createCell(2).setCellValue(user.getAge());
}
// 导出汽车数据
rowNum = 0;
Row carHeader = carSheet.createRow(rowNum++);
carHeader.createCell(0).setCellValue("ID");
carHeader.createCell(1).setCellValue("Brand");
carHeader.createCell(2).setCellValue("Model");
for (Car car : carList) {
Row row = carSheet.createRow(rowNum++);
row.createCell(0).setCellValue(car.getId());
row.createCell(1).setCellValue(car.getBrand());
row.createCell(2).setCellValue(car.getModel());
}
// 导出书籍数据
rowNum = 0;
Row bookHeader = bookSheet.createRow(rowNum++);
bookHeader.createCell(0).setCellValue("ID");
bookHeader.createCell(1).setCellValue("Name");
bookHeader.createCell(2).setCellValue("Author");
for (Book book : bookList) {
Row row = bookSheet.createRow(rowNum++);
row.createCell(0).setCellValue(book.getId());
row.createCell(1).setCellValue(book.getName());
row.createCell(2).setCellValue(book.getAuthor());
}
// 写入到文件
try (FileOutputStream outputStream = new FileOutputStream("test.xlsx")) {
workbook.write(outputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class User {
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
class Car {
private int id;
private String brand;
private String model;
public Car(int id, String brand, String model) {
this.id = id;
this.brand = brand;
this.model = model;
}
public int getId() {
return id;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
}
class Book {
private int id;
private String name;
private String author;
public Book(int id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
}
```
以上代码会生成一个名为 `test.xlsx` 的excel文件,其中包含三个sheet,分别对应三个不同的对象集合。每个sheet中的数据都是从相应的对象集合中生成的,具体的数据格式可以根据需求进行修改。