1.创建项目apache_poi
2.添加依赖
<!-- ApachePOI依赖--> <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
3. 编写测试类,实现导出excel、导入excel
package com.pomelo.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.junit.Test; import java.io.FileInputStream; import java.io.FileOutputStream; public class ApachePoiDemo { //往excel写入数据 @Test public void write() throws Exception { // 创建工作簿 Workbook workbook = new HSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet(); // 创建第一行 Row row = sheet.createRow(0); // 创建第一行的第一列 Cell cell = row.createCell(0); // 往单元格设置内容 cell.setCellValue("第一行第一列"); // 输出excel workbook.write(new FileOutputStream("c:\\tmp\\poi\\demo1.xls")); workbook.close(); } // 读取excel内容 @Test public void read() throws Exception { // 根据excel文件流获取工作簿 Workbook workbook = new HSSFWorkbook(new FileInputStream("c:\\tmp\\poi\\demo1.xls")); // 获取工作表 Sheet sheet = workbook.getSheetAt(0); // 获取第一行 Row row = sheet.getRow(0); // 获取第一行第一列 Cell cell = row.getCell(0); // 获取单元格 System.out.println("单元格内容:" + cell.getStringCellValue()); System.out.println("总行数:"+sheet.getPhysicalNumberOfRows()); System.out.println("总列数:"+row.getPhysicalNumberOfCells()); workbook.close(); } }