在日常开发中,经常会遇到需要将数据导入和导出Excel文件的需求。Java提供了Apache POI这个强大的库来处理Excel文件。本文将介绍如何使用Apache POI导入和导出Excel文件,并提供一个完整的工具类供大家使用。
环境准备
在使用Apache POI之前,需要确保你的项目中引入了相关依赖。假设你使用Maven来管理项目,可以在pom.xml
中添加以下依赖:
<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>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.0.4</version>
</dependency>
导出Excel
下面是一个简单的导出Excel的例子,我们将创建一个包含用户信息的Excel文件。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelUtil {
public static void exportExcel(String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用户信息");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("姓名");
headerRow.createCell(1).setCellValue("年龄");
headerRow.createCell(2).setCellValue("性别");
// 填充数据
Object[][] userData = {
{"张三", 25, "男"},
{"李四", 30, "女"},
{"王五", 28, "男"},
};
int rowNum = 1;
for (Object[] user : userData) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < user.length; i++) {
row.createCell(i).setCellValue(user[i].toString());
}
}
// 写入文件
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
workbook.write(fileOut);
}
workbook.close();
}
}
导入Excel
下面是一个导入Excel的例子,我们将读取之前创建的Excel文件并输出内容到控制台。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelUtil {
public static void importExcel(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
System.out.print("未知类型\t");
}
}
System.out.println();
}
workbook.close();
fileInputStream.close();
}
}
完整工具类
结合上述功能,我们可以将导入和导出Excel的功能封装在一个工具类中:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelUtil {
public static void exportExcel(String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用户信息");
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("姓名");
headerRow.createCell(1).setCellValue("年龄");
headerRow.createCell(2).setCellValue("性别");
Object[][] userData = {
{"张三", 25, "男"},
{"李四", 30, "女"},
{"王五", 28, "男"},
};
int rowNum = 1;
for (Object[] user : userData) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < user.length; i++) {
row.createCell(i).setCellValue(user[i].toString());
}
}
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
workbook.write(fileOut);
}
workbook.close();
}
public static void importExcel(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
System.out.print("未知类型\t");
}
}
System.out.println();
}
workbook.close();
fileInputStream.close();
}
public static void main(String[] args) {
try {
String filePath = "用户信息.xlsx";
exportExcel(filePath);
importExcel(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
小结
这就是一个使用Java通过Apache POI库来导入导出Excel文件的简单示例。通过以上代码,我们可以看到导入和导出Excel文件都是相对简单的操作。希望本教程能帮助你更好地处理Excel文件的相关需求。上述工具类仅为基础示例,实际应用中可以根据需要进行扩展与优化。