1、POI
官方网址:https://poi.apache.org/
1.1、导包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
1.2、创建Excel(完成99乘法表)
@Test
public void testCreateExcel() throws Exception {
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("99");
for (int i = 1; i <= 9; i++) {
Row row = sheet.createRow(i - 1);
for (int j = 1; j <= i; j++) {
Cell cell = row.createCell(j - 1);
cell.setCellValue(i + "×" + j + "=" + (i * j));
}
}
FileOutputStream out = new FileOutputStream("99.xlsx");
wb.write(out);
out.close();
wb.dispose();
}
1.3、读取Excel
@Test
public void testReadExcel() throws Exception {
Workbook wb = WorkbookFactory.create(new FileInputStream("99.xlsx"));
Sheet sheetAt = wb.getSheetAt(0);
int lastRowNum = sheetAt.getLastRowNum();
for (int i = 0; i <lastRowNum ; i++) {
Row row = sheetAt.getRow(i);
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue()+" ");
}
System.out.println();
}
}
2、EasyPOI
官方文档:http://easypoi.mydoc.io/
2.1、导包
- 注意:EasyPOI的包和poi的包可能会产生冲突,所以先将poi的包删除
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>