在本文中,我们将讨论如何使用Apache POI读写Excel文件
1. Apache POI库的基本定义
本节简要介绍有关Excel读写期间使用的基本类。
HSSF
在类名之前添加前缀,以指示与Microsoft Excel 2003文件相关的操作。XSSF
在类名之前添加前缀,以指示与Microsoft Excel 2007文件或更高版本相关的操作。XSSFWorkbook
和HSSFWorkbook
是充当Excel工作簿的类HSSFSheet
和XSSFSheet
是充当Excel工作表的类Row
定义一个Excel行Cell
定义参照行寻址的Excel单元格。
2.下载Apache POI
使用Maven依赖关系可以轻松获得Apache POI库。pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
3. Apache POI库–编写简单的Excel
以下代码显示了如何使用Apache POI库编写一个简单的Excel文件。该代码使用二维数据数组来保存数据。数据被写入XSSFWorkbook
对象。XSSFSheet
是正在处理的工作表。代码如下所示:ApachePOIExcelWrite.java
package com.mkyong;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ApachePOIExcelWrite {
private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
Object[][] datatypes = {
{"Datatype", "Type", "Size(in bytes)"},
{"int", "Primitive", 2},
{"float", "Primitive", 4},
{"double", "Primitive", 8},
{"char", "Primitive", 1},
{"String", "Non-Primitive", "No fixed size"}
};
int rowNum = 0;
System.out.println("Creating excel");
for (Object[] datatype : datatypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field : datatype) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}
执行上述代码后,您将获得以下excel作为输出。
4. Apache POI库–读取Excel文件
以下代码说明了如何使用Apache POI库读取Excel文件。函数getCellTypeEnum
在版本3.15中已弃用,并且将从版本4.0 getCellType
命名为getCellType
。ApachePOIExcelRead.java
package com.mkyong;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
public class ApachePOIExcelRead {
private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行上面的代码后,您将获得以下输出。
Datatype--Type--Size(in bytes)--
int--Primitive--2.0--
float--Primitive--4.0--
double--Primitive--8.0--
char--Primitive--1.0--
String--Non-Primitive--No fixed size--
参考文献:
- 有关弃用getCellTypeEnum的详细信息
- 关于弃用的Apache POI参考
- Apache POI Maven回购
- Apache POI API文档
翻译自: https://mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/