[Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API
读取以下两种格式的Excel : *.xls and *.xlsx
用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) (.xls) file format.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.


These 4 JARs are needed to read excel:

将这四个JAR包加入到Java Build Path里面去

Java code to read Excel :
package com.file.properties; import java.io.*;
import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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; class ReadExcel
{
static void readXlsx(File inputFile)
{
try
{
// Get the workbook instance for XLSX file
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile)); // Get first sheet from the workbook
XSSFSheet sheet = wb.getSheetAt(0); Row row;
Cell cell; // Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext())
{
row = rowIterator.next(); // For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext())
{
cell = cellIterator.next(); switch (cell.getCellType())
{ case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break; case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break; case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break; case Cell.CELL_TYPE_BLANK:
System.out.println(" ");
break; default:
System.out.println(cell); }
}
}
}
catch (Exception e)
{
System.err.println("Exception :" + e.getMessage());
}
} static void readXls(File inputFile)
{
try
{
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row; // Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext())
{
row = rowIterator.next(); // For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext())
{
cell = cellIterator.next(); switch (cell.getCellType())
{ case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break; case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break; case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break; case Cell.CELL_TYPE_BLANK:
System.out.println(" ");
break; default:
System.out.println(cell);
}
}
} } catch (FileNotFoundException e)
{
System.err.println("Exception" + e.getMessage());
}
catch (IOException e)
{
System.err.println("Exception" + e.getMessage());
}
} public static void main(String[] args)
{
File inputFile = new File("D:\\SoapUIStudy\\input.xls");
File inputFile2 = new File("D:\\SoapUIStudy\\input.xlsx");
readXls(inputFile);
readXlsx(inputFile2);
}
}
Result :
User in xls
Password in xls
U1
P1
U2
P2
User in xlsx
Password in xlsx
User1
Password1
User2
Password2
User3
Password3
[Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API的更多相关文章
- [Training Video - 6] [File Reading] [Java] Read Properties file
package com.file.properties; import java.io.FileInputStream; import java.util.Properties; public cla ...
- [Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API
package com.file.properties; import java.io.File; import java.io.FileNotFoundException; import java. ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- java 读取excel 2007 .xlsx文件 poi实现
工作需要读取excel里面的行内容,使用java实现较为简单. 在最开始,尝试使用 jxl-2.6.12 来实现读取excel 的行内容.但是按照网上的方法,程序根本无法正确处理文件流.经过谷姐的一番 ...
- Java对Excel数据处理(利用POI解析Excel)
前言 研究生复试结束我在学校官网上看到了全校按姓氏排列的拟录取名单,但是官网并没有给出每个人的专业,只有学号,另外还知道本专业的复试名单,所以我想知道对于本专业的拟录取名单.具体做法就是,扫描复试名单 ...
- java 在Excel中插入图片 POI实现
一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...
- Java读写Excel文件,利用POI
直接看工具类代码吧, package com.example.demo.util; import com.example.demo.entity.ExcelDataVO; import org.apa ...
- Java操作Excel工具类(poi)
分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展 1 import org.apache.commons.lang3.exception.ExceptionUtil ...
- [转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)
本文转自:http://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing- ...
随机推荐
- JAVA课程设计(坦克大战)
2019-01-16 坦克大战游戏背景: 1. 需求分析 1.1环境要求 操作系统:Windows 7(SP1)以上 JAVA虚拟机:JDK1.8以上 开发环境:Eclipse(4.5以上) 1.2角 ...
- 方法 中 void 的解释
- mysql命令之二:查看mysql版本的四种方法
1:在终端下:mysql -V. 以下是代码片段: [shengting@login ~]$ mysql -V mysql Ver 14.7 Distrib 4.1.10a, for redhat-l ...
- Thread.setDaemon详解
Thread.setDaemon详解 线程分为两种类型:用户线程和守护线程.通过Thread.setDaemon(false)设置为用户线程:通过Thread.setDaemon(true)设置为守护 ...
- mysql 存储过程简单学习
转载自:http://blog.chinaunix.net/uid-23302288-id-3785111.html ■存储过程Stored Procedure 存储过程就是保存一系列SQL命令的集合 ...
- JAVA访问控制变量、类变量、类方法
1.私有:同类中 2.默认:同包中的类 3.保护:同包中的类 子类中(继承性) 4.公有:无范围 创建子类并覆盖方法时,必须考虑原来方法的访问控制: 作为通用的规则,覆盖方法是,新方法的访问控制不能 ...
- 我编辑的JAVA日历程序
class calendar { public static void main(String[]args) { int yearIn ; yearIn = Integer.parseInt(args ...
- verilog 之数字电路 寄存器,触发器。
我一直听说没有由code到circuit就只是入门了.实在没办法了.我想了一招,一个一个的写,然后看RTL,然后分析.这是第一篇. 1.触发器. 没有复位,置位.posedge clk 是触发沿时钟. ...
- python执行报错 configparser.NoSectionError: No section: 'section_1'
场景:请求获取验证码模块regVC.py读取配置文件config.ini时,regVC.py模块单独执行正常,但通过run_all.py模块批量执行时报错,找不到section 解决办法:配置文件路径 ...
- 读<分布式一致性原理>初识zookeeper
zookeeper是什么 zookeeper是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它实现诸如:数据发布/订阅,负载均衡,命名服务,分布式协调/通知 ,集群管理,Master选举 ...