本文主要是介绍java操作Excel、Word等,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java读取Excel
1. 引入pom依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
2. POI结构
HSSF - 提供读写Microsoft Excel XLS格式档案的功能
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能
HWPF - 提供读写Microsoft Word DOC格式档案的功能
HSLF - 提供读写Microsoft PowerPoint格式档案的功能
HDGF - 提供读Microsoft Visio格式档案的功能
HPBF - 提供读Microsoft Publisher格式档案的功能
HSMF - 提供读Microsoft Outlook格式档案的功能
3. 读取实例
public void test1() throws IOException {
//加载指定文件,创建一个Excel对象(工作溥
XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\lenovo\\Desktop\\poi.xlsx")));
//读取Excel文件中第一个Sheet标签页
XSSFSheet sheetAt = excel.getSheetAt(0);
//遍历Sheet标签页,获得每一行数据
for (Row cells : sheetAt) {
//遍历行,后的每个单元格
for (Cell cell : cells) {
System.out.println(cell.getStringCellValue());
}
}
//关闭资源
excel.close();
}
这篇关于java操作Excel、Word等的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!