读取以下两种格式的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. Top Android App使用的组件

    唱吧_462 smack:de.measite.smack:??? ???:org.apache:??? smack:org.jivesoftware.smack:XMPP客户端类库 dnsjava: ...

  2. bean对grub4dos做出的巨大贡献总结

    bean对grub4dos做出的巨大贡献总结 ===================================================================bean对grub4 ...

  3. [转载]Linux驱动mmap内存映射

    原文地址:https://www.cnblogs.com/wanghuaijun/p/7624564.html mmap在linux哪里? 什么是mmap? 上图说了,mmap是操作这些设备的一种方法 ...

  4. GITBOOK/HEXO TRAVIS GITHUB-PAGES 博客搭建

    简介 这年头要是没有个博客都不好意思给别人说你是程序员,我用XX笔记呀,不行吗?不行,这玩意儿要么不能公开分享,要么公开分享要会员,现在到处都是开源,自己学到了东西都不能分享给需要帮助的人,真是伤心呀 ...

  5. R语言可视化学习笔记之添加p-value和显著性标记

    R语言可视化学习笔记之添加p-value和显著性标记 http://www.jianshu.com/p/b7274afff14f?from=timeline   上篇文章中提了一下如何通过ggpubr ...

  6. 使用CXF发布和调用webservice之HelloWorld入门

    依赖的JAR     cxf-2.2.10.jar     jetty-6.1.21.jar     jetty-util-6.1.21.jar     servlet-2_5-api.jar     ...

  7. 基于Tesseract的身份证识别Android端应用

    以开源的Tesseract为基础,做了一个身份证识别的app. 图片资源是百度找的,而且手机对着电脑屏幕拍照,拍出很多花纹,影响比较大,所以误差不小,实测对着自己身份证拍照会好很多. 效果图: 1.拍 ...

  8. TCP超时与重传机制

    TCP超时与重传机制    TCP协议是一种面向连接的可靠的传输层协议,它保证了数据的可靠传输,对于一些出错,超时丢包等问题TCP设计的超时与重传机制.其基本原理:在发送一个数据之后,就开启一个定时器 ...

  9. sort_region——对区域进行排序

    The operator sort_region sorts the regions with respect to their relative position. All sorting meth ...

  10. C# 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke [问题点数:40分

    注意:  this.DateTimeRun = true;            new Thread(jishi_kernel).Start(); 线程的启动,不能放在    public Form ...