读取以下两种格式的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的更多相关文章

  1. [Training Video - 6] [File Reading] [Java] Read Properties file

    package com.file.properties; import java.io.FileInputStream; import java.util.Properties; public cla ...

  2. [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. ...

  3. 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, ...

  4. java 读取excel 2007 .xlsx文件 poi实现

    工作需要读取excel里面的行内容,使用java实现较为简单. 在最开始,尝试使用 jxl-2.6.12 来实现读取excel 的行内容.但是按照网上的方法,程序根本无法正确处理文件流.经过谷姐的一番 ...

  5. Java对Excel数据处理(利用POI解析Excel)

    前言 研究生复试结束我在学校官网上看到了全校按姓氏排列的拟录取名单,但是官网并没有给出每个人的专业,只有学号,另外还知道本专业的复试名单,所以我想知道对于本专业的拟录取名单.具体做法就是,扫描复试名单 ...

  6. java 在Excel中插入图片 POI实现

    一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...

  7. Java读写Excel文件,利用POI

    直接看工具类代码吧, package com.example.demo.util; import com.example.demo.entity.ExcelDataVO; import org.apa ...

  8. Java操作Excel工具类(poi)

    分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展 1 import org.apache.commons.lang3.exception.ExceptionUtil ...

  9. [转]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- ...

随机推荐

  1. 打包python文件,让文件程序化

    通过对源文件打包,Python程序可以在没有安装 Python的环境中运行,也可以作为一个独立文件方便传递和管理. 现在网上主流的打包方式有两种py2exe或者pyinstaller两款多平台的Pyt ...

  2. python 面向对象(三大特性)

    python 面向对象(初级) (思维导图 ↑↑↑↑↑) 概述: 面向过程:根据业务逻辑从上到下垒代码. 函数式:将某功能代码封装至函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类 ...

  3. Tool:Visual Studio

    ylbtech-Tool:Visual Studio Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.VS是一个基本完整的开发工具集,它包括了整个软件生命 ...

  4. hsqldb简单使用总结

      hsqldb数据库是一款纯Java实现的开源免费数据库,相对其他数据库来说,体积非常小,使用方便,非常利于在测试环境中使用,无需复杂的数据库配置.   hsqldb数据库引擎有几种服务器模式:Se ...

  5. java之IO整理(下)

    一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...

  6. Drools简单例子

    转自:http://www.blogjava.net/diggbag/articles/359347.html 1.Drools简单例子 首先是搭建一个可供进行Drools开发的框架.Jboss官方推 ...

  7. SVN命令解析以及问题解决(update...)

    SVN常用指令 1.Repo-browser(浏览版本库) 通过“浏览版本库”可以直接查看服务器上指定目录下的所有目录结构(需要有相关权限),包括特定版本的作者,提交时间等,并且在浏览版本库里面链接了 ...

  8. linux中sed命令

    sed sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. ...

  9. 浅谈AVL树,红黑树,B树,B+树原理及应用(转)

    出自:https://blog.csdn.net/whoamiyang/article/details/51926985 背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上 ...

  10. Window虚拟内存管理(转)

    内存管理是操作系统非常重要的部分,处理器每一次的升级都会给内存管理方式带来巨大的变化,向早期的8086cpu的分段式管理,到后来的80x86 系列的32位cpu推出的保护模式和段页式管理.在应用程序中 ...