在 Maven 中使用 Apache POI,需要在项目的 pom.xml 文件中添加以下依赖:
org.apache.poi poi 5.2.1 org.apache.poi poi-ooxml 5.2.1
然后可以在项目中使用 Apache POI 提供的 API 来操作 Excel 文件,例如:
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelWriter { public static void main(String[] args) { try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello, World!"); try (FileOutputStream fileOut = new FileOutputStream("workbook.xlsx")) { workbook.write(fileOut); } } catch (IOException e) { e.printStackTrace(); } } }
这段代码演示了如何创建一个 Excel 文件,并在第一个单元格中写入内容。您可以根据需要使用 Apache POI 的各种类和方法来操作 Excel 文件。