第一步导入jar包
<!--POI报表-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
第二部测试文件的读或写
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class POITest {
@Test
public void testRead() throws Exception {
FileInputStream fileInputStream =
new FileInputStream(new File("Excel文件全路径"));
XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = excel.getSheetAt(0);
for (Row row : sheet) {
System.out.println(row);
for (Cell cell : row) {
System.out.println(cell.getStringCellValue());
}
}
excel.close();
}
@Test
public void testWriteExcel() throws IOException {
XSSFWorkbook excel = new XSSFWorkbook();
XSSFSheet sheet = excel.createSheet("xxx");
XSSFRow title = sheet.createRow(0);
title.createCell(0).setCellValue("姓名");
title.createCell(1).setCellValue("年龄");
title.createCell(2).setCellValue("地址");
XSSFRow dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("张三");
dataRow.createCell(1).setCellValue("男");
dataRow.createCell(2).setCellValue("北京");
FileOutputStream outputStream =
new FileOutputStream(new File("Excel文件全路径"));
excel.write(outputStream);
outputStream.flush();
excel.close();
}
}