依赖
<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>
解决上述依赖警告问题
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.22</version>
</dependency>
案例
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;public class Demo {public static void main(String[] args) throws IOException {write();}public static void write() throws IOException {//在内存中创建一个Excel文件XSSFWorkbook excel = new XSSFWorkbook();//在Excel文件中创建一个Sheet页XSSFSheet sheet = excel.createSheet("demo");//在Sheet页中创建行对象XSSFRow row = sheet.createRow(1);//创建单元格并且写入内容row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("性别");//再创建一个行row = sheet.createRow(2);//创建单元格并且写入内容row.createCell(1).setCellValue("艾伦");row.createCell(2).setCellValue("男");//再创建一个行row = sheet.createRow(3);//创建单元格并且写入内容row.createCell(1).setCellValue("三笠");row.createCell(2).setCellValue("女");//将Excel文件从内存写入磁盘FileOutputStream output = new FileOutputStream("D:\\develop\\demo.xlsx");excel.write(output);//关闭资源output.close();excel.close();}
}