现在的位置: 首页 > 编程语言 > 正文

JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析

2020年02月14日 编程语言 ⁄ 共 3708字 ⁄ 字号 评论关闭

经过一番搜索发现,java操纵excel文件常用的有jxl和poi两种方式,孰好孰坏看自己需求而定。

其中最主要的区别在于jxl不支持.xlsx,而poi支持.xlsx

这里介绍的使用poi方式(XSSFWorkbook),实际上poi提供了HSSFWorkbook和XSSFWorkbook两个实现类。区别在于HSSFWorkbook是针对.xls文件,XSSFWorkbook是针对.xslx文件。

首先明确一下基本概念:

  先创建一个工作簿,一个工作簿可以有多个工作表,一个工作表可以有多个行,一个行可以有多个单元格

  工作簿 >>>>>>>>XSSFWorkbook

  工作表 >>>>>>>>XSSFSheet

  行 >>>>>>>>XSSFRow

  单元格 >>>>>>>>XSSFCell

下图为创建的student.xlsx的内容:

读取student.xlsx文件代码:

package com.zjk.testexcel;import org.apache.poi.xssf.usermodel.*;import java.io.FileInputStream;import java.io.IOException;/** * @Auther: zjk * @Date: 2019/8/30 * @Description: */public class TestExcel1 { public static void main(String[] args) { try { //创建工作簿 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\test-excel\\student.xlsx")); System.out.println("xssfWorkbook对象:" + xssfWorkbook); //读取第一个工作表(这里的下标与list一样的,从0开始取,之后的也是如此) XSSFSheet sheet = xssfWorkbook.getSheetAt(0); System.out.println("sheet对象:" + sheet); //获取第一行的数据 XSSFRow row = sheet.getRow(0); System.out.println("row对象:" + row); //获取该行第一个单元格的数据 XSSFCell cell0 = row.getCell(0); System.out.println("cello对象:" + cell0); } catch (IOException e) { e.printStackTrace(); } }}

控制台输出结果:可以发现具体到行对象时,就解析成xml文件了

xssfWorkbook对象:  Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xmlsheet对象:  Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xmlrow对象:  <xml-fragment r="1" spans="1:4" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:etc="http://www.wps.cn/officeDocument/2017/etCustomData" xmlns:main="http://schemas.openxmlformats.org/spreadsheetml/2006/main">   <main:c r="A1" t="s">   <main:v>0</main:v>   </main:c>   <main:c r="B1" t="s">   <main:v>1</main:v>   </main:c>   <main:c r="C1" t="s">   <main:v>2</main:v>   </main:c>   <main:c r="D1" t="s"> <main:v>3</main:v> </main:c></xml-fragment>cello对象:姓名

以上可以实现了读取某行某单元格的数据,那么接下来就该读取整个表的所有数据了:

package com.zjk.testexcel;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;import java.io.IOException;/** * @Auther: zjk * @Date: 2019/8/30 * @Description: */public class TestExcel2 { public static void main(String[] args) { try { //创建工作簿 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\test-excel\\student.xlsx")); System.out.println("xssfWorkbook对象:" + xssfWorkbook); //读取第一个工作表 XSSFSheet sheet = xssfWorkbook.getSheetAt(0); System.out.println("sheet对象:" + sheet);       //获取最后一行的num,即总行数。此处从0开始计数 int maxRow = sheet.getLastRowNum(); System.out.println("总行数为:" + maxRow); for (int row = 0; row <= maxRow; row++) { //获取最后单元格num,即总单元格数 ***注意:此处从1开始计数*** int maxRol = sheet.getRow(row).getLastCellNum(); System.out.println("--------第" + row + "行的数据如下--------"); for (int rol = 0; rol < maxRol; rol++){ System.out.print(sheet.getRow(row).getCell(rol) + " "); } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } }}

控制台输出:

xssfWorkbook对象:Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xmlsheet对象:Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml总行数为:2--------第0行的数据如下--------姓名 学号 班级 入学日期 --------第1行的数据如下--------张三 2.0190001E7 三班 01-八月-2019 --------第2行的数据如下--------李四 2.0190002E7 三班 01-八月-2019

注意:2.0190001E7 = 2.0190001 * 107 = 20190001

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析

以上就上有关JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.