- 💂 个人主页: 程序员爱摸鱼
- 🤟 版权: 本文由【程序员爱摸鱼】原创、在CSDN首发、需要转载请联系博主
- 💬 如果文章对你有帮助、欢迎关注+点赞+收藏(一键三连)哦
- 💅 想寻找共同成长的小伙伴,可以互粉哦
💅 1.3官网
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”
| 功能 | 描述 |
|---|---|
| HSSFWorkBook | 提供读写Microsoft Excel格式档案的功能,xls文档 |
| XSSFWorkBook | 提供读写Microsoft Excel OOXML格式档案的功能,xlsx文件 |
| HWPF | 提供读写Microsoft Word格式档案的功能 |
| HSLF | 提供读写Microsoft PowerPoint格式档案的功能 |
| HDGF | 提供读写Microsoft Visio格式档案的功能 |
| HPBF | 提供读Microsoft Publisher格式档案的功能 |
| HSMF | 提供读Microsoft Outlook格式档案的功能 |

-
-
-
org.apache.poi -
poi -
3.9 -
-
-
-
org.apache.poi -
poi-ooxml -
3.9 -
-
-
-
joda-time -
joda-time -
2.10.1 -
-
-
-
junit -
junit -
4.12 -
-
excel2003 文件扩展名为 xls
名词:
工作簿:一个excel文件,就是一个工作簿
工作表:一个工作簿中,可以所有多个工作表Sheet
行:每一个工作表,包含多行row
单元格:每行有多个单元格Cell组成。
- public class TestXls {
-
- //程序运行的根目录,classpath
- public String getPath(){
- return this.getClass().getResource("/").getPath();
- }
-
- @Test
- public void testWrite() throws IOException {
- //1.创建工作簿 Workbook
- Workbook workbook = new HSSFWorkbook();
- //2. 通过工作簿,创建工作表Sheet
- Sheet sheet = workbook.createSheet("表名");
- for (int i = 0; i < 5; i++) {
- //3.通过表,创建行 Row
- Row row = sheet.createRow(i);
- for (int j = 0; j < 10; j++) {
- //4. 通过行,创建单元格 Cell
- Cell cell = row.createCell(j);
- // 给单元格添加数据
- cell.setCellValue("测试"+i+":"+j);
- }
- }
- //5. 将工作簿以流的方式写入硬盘
- String file= getPath()+"demo01.xls";
- OutputStream out=new FileOutputStream(file);
- //6. 添加
- workbook.write(out);
- //7. 释放资源
- out.close();
- }
- }
- public class TestXlsx {
-
- //程序运行的根目录,classpath
- public String getPath(){
- return this.getClass().getResource("/").getPath();
- }
-
- @Test
- public void testWrite() throws IOException {
- //1.创建工作簿 Workbook
- Workbook workbook = new XSSFWorkbook();
- //2. 通过工作簿,创建工作表Sheet
- Sheet sheet = workbook.createSheet("Java12班");
- for (int i = 0; i < 5; i++) {
- //3.通过表,创建行 Row
- Row row = sheet.createRow(i);
- for (int j = 0; j < 10; j++) {
- //4. 通过行,创建单元格 Cell
- Cell cell = row.createCell(j);
- // 给单元格添加数据
- cell.setCellValue("测试"+i+":"+j);
- }
- }
- //5. 将工作簿以流的方式写入硬盘
- String file= getPath()+"demo01.xlsx";
- OutputStream out=new FileOutputStream(file);
- workbook.write(out);
- //释放资源
- out.close();
- }
- }
- @Test
- public void testRead() throws IOException {
- String file=getPath()+"demo01.xls";
- FileInputStream is = new FileInputStream(file);
- HSSFWorkbook workbook = new HSSFWorkbook(is);
-
- //获得工作表
- Sheet sheet=workbook.getSheet("java12班");
-
- //获得行
- int startRow = sheet.getFirstRowNum(); //第一列的索引号,从0开始
- int endRow = sheet.getLastRowNum(); //最后一列的索引号,从0开始
-
- for (int i = startRow; i
- Row row = sheet.getRow(i);
-
- short startCell = row.getFirstCellNum(); //第一列的索引号,从0开始
- short endCell = row.getLastCellNum(); //最后一列的索引号,但从1开始
-
- for (int j = startCell; j
- //4.获取列(单元格)
- Cell cell = row.getCell(j);
- //5.打印内容
- System.out.print(cell.getStringCellValue());
- System.out.print(",");
- }
- // 断点
- System.out.println();
- }
- }
- @Test
- public void testRead() throws IOException {
- String file=getPath()+"demo01.xlsx";
- FileInputStream is = new FileInputStream(file);
- Workbook workbook = new XSSFWorkbook(is);
-
- //获得工作表
- Sheet sheet=workbook.getSheet("java12班");
-
- //获得行
- int startRow = sheet.getFirstRowNum(); //第一列的索引号,从0开始
- int endRow = sheet.getLastRowNum(); //最后一列的索引号,从0开始
-
- for (int i = startRow; i
- Row row = sheet.getRow(i);
-
- short startCell = row.getFirstCellNum(); //第一列的索引号,从0开始
- short endCell = row.getLastCellNum(); //最后一列的索引号,但从1开始
-
- for (int j = startCell; j
- //4.获取列(单元格)
- Cell cell = row.getCell(j);
- //5.打印内容
- System.out.print(cell.getStringCellValue());
- System.out.print(",");
- }
- System.out.println();
- }
- }
- @Test
- public void testRead07() throws Exception{
-
- InputStream is = new FileInputStream("d:/0704.xlsx");
-
- Workbook workbook = new XSSFWorkbook(is);
- Sheet sheet = workbook.getSheetAt(0);
-
- // 读取第一行第一列
- Row row = sheet.getRow(0);
- Cell cell1 = row.getCell(0);
- Cell cell2 = row.getCell(1);
-
-
- // 输出单元内容
- System.out.println(cell1.getStringCellValue());
- System.out.println(cell2.getNumericCellValue());
-
- // 操作结束,关闭文件
- is.close();
- }
到这里说明你已经学会了哦,努力学习!学无止境!!!

想要了解更多吗?没时间解释了,快来点一点!!!
程序员爱摸鱼🐟
————————————————
版权声明:本文为CSDN博主「程序员爱摸鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:程 序 员 爱 摸 鱼🐟
-
相关阅读:
$概率DP$
Postman设置全局变量和传参
web前端期末大作业——基于HTML+CSS+JavaScript蓝色的远程监控设备系统后台管理界面模板
java+springboot+vue高校毕业生就业统计管理系统022xr
学习STM32第十八天
计算数组中各元素的平方numpy.square()
Vue.js模板语法概述
C++学习day--23 枚举、类型定义、头文件
Redis入门 (店铺营业状态设置) --苍穹外卖day4
18.2 使用NPCAP库抓取数据包
-
原文地址:https://blog.csdn.net/lcshen1234/article/details/126745604