logo头像

不破不立

Java 使用 Apache POI 生成及解析 Excel

Java 使用 Apache POI 生成及解析 Excel

本文简单介绍一下Apache POI工具包如何生存以及解析Excel表格。

Apache POI 库的基本定义

  1. HSSF`在类名前面加上前缀,以指示与Microsoft Excel 2003文件相关的操作。
  2. XSSF`在类名前面加前缀,表示与Microsoft Excel 2007文件或更高版本相关的操作。
  3. XSSFWorkbook`HSSFWorkbook是充当Excel工作簿的类
  4. HSSFSheet`XSSFSheet是充当Excel工作表的类
  5. Row定义Excel行
  6. Cell定义了一个引用行的Excel单元格。

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>

实例 Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package site.sixteen.demoutils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.Iterator;

public class ApachePOIUtils {


public static void writeToExcel(Object[][] data, String fileName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("A班");
int rowNum = 0;
for (Object[] list : data) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object value : list) {
Cell cell = row.createCell(colNum++);
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Boolean) {
cell.setCellValue((boolean) value);
} else {
cell.setCellValue(String.valueOf(value));
}
}
}

try (FileOutputStream outputStream = new FileOutputStream(fileName)) {
workbook.write(outputStream);
}
}

public static void readExcel(File file) throws IOException {
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellTypeEnum() == CellType.STRING) {
System.out.print(cell.getStringCellValue() + "\t");
} else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(cell.getNumericCellValue() + "\t");
} else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
System.out.print(cell.getBooleanCellValue() + "\t");
} else {
System.out.print(cell.getStringCellValue() + "\t");
}
}
System.out.println();
}
}

public static void main(String[] args) {
Object[][] datas = {
{"编号", "名字", "年龄", "性别"},
{"001", "Tom", 18, true},
{"002", "Jac", 10, true},
{"003", "Oct", 24, true},
{"004", "Lil", 21, false},
};
String fileName = "/tmp/demo-write.xlsx";
try {
writeToExcel(datas, fileName);
} catch (IOException e) {
e.printStackTrace();
}
try {
readExcel(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
}

}

运行结果:

生成的Excel在/tmp/demo-write.xlsx

解析的结果:

1
2
3
4
5
6
7
编号	名字	年龄	性别	
001 Tom 18.0 true
002 Jac 10.0 true
003 Oct 24.0 true
004 Lil 21.0 false

Process finished with exit code 0
上一篇

评论系统未开启,无法评论!

如果有好的建议或疑问等可以发送邮件至:panhainan@yeah.net,或者添加QQ:1016593477,将你的建议或者疑问告诉作者,作者会对你的建议进行处理并补充到文章的尾部,谢谢大家的谅解!