java 通过Apache poi 生成excel文件
1、首先创建maven项目,添加apache poi依赖<dependency> <group朐袁噙岿Id>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version></dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version></dependency>

2、新建main测试类

3、源代码如下图public class PoiTest { public static void main(String[柯计瓤绘] args) throws IOException { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); Row headerRow = sheet.createRow(0); // 创建表头行 headerRow.createCell(0).setCellValue("姓名"); headerRow.createCell(1).setCellValue("性别"); headerRow.createCell(2).setCellValue("年龄"); Row row = null; for (int i = 1; i < 10; i++) { // 设置几行数据 row = sheet.createRow(i); // 创建表头行 row.createCell(0).setCellValue("张三"); row.createCell(1).setCellValue("男"); row.createCell(2).setCellValue(20); } OutputStream fileOut = null; File file = new File("D:/poitest/workbook.xls"); File fileParent = file.getParentFile(); if (!fileParent.exists()) { fileParent.mkdirs(); } fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); }}

4、执行main方法

5、然后生成了excel文件

6、打开excel文件,校验数据是否正确生成,发现数据正常。
