Java实现Excel导出多个Sheet
在实际开发中,有时候需要将数据导出到Excel文件中,而且可能会涉及到多个Sheet的操作。在本文中,我们将使用Java编程语言来解决一个实际问题:如何在Excel中创建多个Sheet,并将数据导出到对应的Sheet中。
问题描述
假设我们有一个学生信息表,里面包含了学生的姓名、性别、年龄等信息。我们需要将这些学生信息导出到一个Excel文件中,其中每个Sheet对应一个班级的学生信息。
解决方案
为了解决这个问题,我们可以使用Apache POI库来操作Excel文件。Apache POI是一个开源的Java库,可以用来读取和写入Microsoft Office格式的文档,包括Excel文件。
首先,我们需要导入Apache POI的依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
接下来,我们编写Java代码来实现导出多个Sheet的功能。首先,我们需要创建一个Excel工作簿对象,并在其中创建多个Sheet。然后,将学生信息按照班级分组,分别导出到对应的Sheet中。
下面是示例代码:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelExportExample {
public static void main(String[] args) {
List<Student> students = getStudents(); // 获取学生信息列表
try (Workbook workbook = new XSSFWorkbook()) {
Map<String, List<Student>> classMap = groupByClass(students); // 按班级分组
for (String className : classMap.keySet()) {
List<Student> classStudents = classMap.get(className);
Sheet sheet = workbook.createSheet(className); // 创建Sheet
createHeaderRow(sheet); // 创建表头
populateDataRows(sheet, classStudents); // 填充数据
}
FileOutputStream outputStream = new FileOutputStream("students.xlsx");
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<Student> getStudents() {
// 模拟获取学生信息的方法
List<Student> students = new ArrayList<>();
students.add(new Student("张三", "男", 18, "一班"));
students.add(new Student("李四", "女", 19, "一班"));
students.add(new Student("王五", "男", 17, "二班"));
students.add(new Student("赵六", "女", 18, "二班"));
return students;
}
private static Map<String, List<Student>> groupByClass(List<Student> students) {
// 按班级分组
Map<String, List<Student>> classMap = new HashMap<>();
for (Student student : students) {
String className = student.getClassName();
if (!classMap.containsKey(className)) {
classMap.put(className, new ArrayList<>());
}
classMap.get(className).add(student);
}
return classMap;
}
private static void createHeaderRow(Sheet sheet) {
// 创建表头
Row header = sheet.createRow(0);
CellStyle style = sheet.getWorkbook().createCellStyle();
Font font = sheet.getWorkbook().createFont();
font.setBold(true);
style.setFont(font);
String[] headers = {"姓名", "性别", "年龄"};
for (int i = 0; i < headers.length; i++) {
Cell cell = header.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(style);
}
}
private static void populateDataRows(Sheet sheet, List<Student> students) {
// 填充数据
for (int i = 0; i < students.size(); i++) {
Row row = sheet.createRow(i + 1);
Student student = students.get(i);
row.createCell(0).setCellValue(student.getName());
row.createCell(1).setCellValue(student.getGender());
row.createCell(2).setCellValue(student.getAge());
}
}
}
class Student {
private String name;
private String gender;
private