大家好,我是不熬夜崽崽!大家如果觉得看了本文有帮助的话,麻烦给不熬夜崽崽点个三连(点赞、收藏、关注)支持一下哈,大家的支持就是我写作的无限动力。
前言
在开发过程中,Excel 是一个常见的文件格式,不论是在导出报告,还是读取分析数据,都离不开它。今天,我将带大家一起看看如何实现一个简单高效的 Excel 操作工具类——ExcelUtils。这个工具类不仅能帮你导出 Excel,还能读取 Excel 文件、填充模板数据、设置单元格样式等。让我们通过实际的代码来了解一下这些功能,保证你用得上,且写起来简洁高效!
1. ExcelUtils 类概述
ExcelUtils 类是我们用来简化 Excel 操作的工具类。我们将用它来实现以下功能:
- 导出 Excel 文件
- 读取 Excel 文件
- 模板填充
- 设置单元格样式
1.1 依赖库
首先,咱们需要引入 Apache POI 库来操作 Excel 文件。在 Maven 项目中,加入以下依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
1.2 代码框架
接下来我们来实现 ExcelUtils 类。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.List;
public class ExcelUtils {
// 1. 导出 Excel 文件
public static void exportExcel(List<String[]> data, String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook(); // 创建 Excel 工作簿
Sheet sheet = workbook.createSheet(Sheet1); // 创建工作表
// 填充数据
int rowIndex = 0;
for (String[] rowData : data) {
Row row = sheet.createRow(rowIndex++);
int cellIndex = 0;
for (String cellData : rowData) {
row.createCell(cellIndex++).setCellValue(cellData);
}
}
// 写入文件
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
workbook.write(fileOut);
} finally {
workbook.close(); // 关闭工作簿
}
}
// 2. 读取 Excel 文件
public static void readExcel(String filePath) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell.toString() + \t);
}
System.out.println();
}
workbook.close();
fis.close();
}
// 3. 模板填充
public static void fillTemplate(String templatePath, String outputPath, List<String[]> data) throws IOException {
FileInputStream fis = new FileInputStream(templatePath);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
// 填充模板数据
int rowIndex = 1; // 假设从第二行开始填充
for (String[] rowData : data) {
Row row = sheet.createRow(rowIndex++);
int cellIndex = 0;
for (String cellData : rowData) {
row.createCell(cellIndex++).setCellValue(cellData);
}
}
// 写入输出文件
try (FileOutputStream fileOut = new FileOutputStream(outputPath)) {
workbook.write(fileOut);
} finally {
workbook.close();
fis.close();
}
}
// 4. 设置单元格样式
public static void setCellStyle(String filePath) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
// 创建单元格样式
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setColor(IndexedColors.RED.getIndex());
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER);
// 应用样式到单元格
for (Row row : sheet) {
for (Cell cell : row) {
cell.setCellStyle(style);
}
}
// 写入文件
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
workbook.write(fileOut);
} finally {
workbook.close();
fis.close();
}
}
public static void main(String[] args) throws IOException {
// 1. 导出 Excel 示例
List<String[]> data = List.of(
new String[]{ID, Name, Age},
new String[]{1, Alice, 24},
new String[]{2, Bob, 29}
);
exportExcel(data, exported.xlsx);
// 2. 读取 Excel 示例
readExcel(exported.xlsx);
// 3. 模板填充示例
List<String[]> templateData = List.of(
new String[]{Product, Price},
new String[]{Apple, 3.5},
new String[]{Banana, 2.0}
);
fillTemplate(template.xlsx, filled_template.xlsx, templateData);
// 4. 设置单元格样式示例
setCellStyle(filled_template.xlsx);
}
}
2. 解释与实现
2.1 导出 Excel
导出 Excel 功能的核心在于创建一个 Workbook
对象,并且通过 Sheet
和 Row
来填充数据。每一行数据通过 Row
创建,每个单元格则通过 Cell
填充内容。最后,使用 FileOutputStream
将数据写入文件。
2.2 读取 Excel
读取 Excel 文件的操作非常简单,我们可以使用 FileInputStream
打开文件,获取 Workbook
,然后通过遍历 Sheet
中的每一行来获取每个单元格的内容。cell.toString()
可以获取单元格的值。
2.3 模板填充
模板填充通常用于从模板文件中读取内容,并根据实际数据进行填充。在 fillTemplate
方法中,我们首先读取模板文件,然后根据数据逐行填充到工作表中。
2.4 设置单元格样式
通过 CellStyle
和 Font
,我们可以给单元格应用不同的样式。在这里,我给每个单元格设置了红色加粗的字体,并将其对齐方式设为居中。
3. 总结
通过这个 ExcelUtils
工具类,你能够轻松实现 Excel 文件的导出、读取、模板填充以及单元格样式设置。它不仅高效,还能有效减少你手动操作 Excel 的时间成本。开发者们,如果你还在为每次 Excel 操作手动编写复杂的代码而烦恼,不妨试试这个工具类吧!