JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過程解析
經(jīng)過一番搜索發(fā)現(xiàn),java操縱excel文件常用的有jxl和poi兩種方式,孰好孰壞看自己需求而定。
其中最主要的區(qū)別在于jxl不支持.xlsx,而poi支持.xlsx
這里介紹的使用poi方式(XSSFWorkbook),實(shí)際上poi提供了HSSFWorkbook和XSSFWorkbook兩個(gè)實(shí)現(xiàn)類。區(qū)別在于HSSFWorkbook是針對(duì).xls文件,XSSFWorkbook是針對(duì).xslx文件。
首先明確一下基本概念:
先創(chuàng)建一個(gè)工作簿,一個(gè)工作簿可以有多個(gè)工作表,一個(gè)工作表可以有多個(gè)行,一個(gè)行可以有多個(gè)單元格
工作簿 >>>>>>>>XSSFWorkbook
工作表 >>>>>>>>XSSFSheet
行 >>>>>>>>XSSFRow
單元格 >>>>>>>>XSSFCell
下圖為創(chuàng)建的student.xlsx的內(nèi)容:

讀取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 {
//創(chuàng)建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\test-excel\\student.xlsx"));
System.out.println("xssfWorkbook對(duì)象:" + xssfWorkbook);
//讀取第一個(gè)工作表(這里的下標(biāo)與list一樣的,從0開始取,之后的也是如此)
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
System.out.println("sheet對(duì)象:" + sheet);
//獲取第一行的數(shù)據(jù)
XSSFRow row = sheet.getRow(0);
System.out.println("row對(duì)象:" + row);
//獲取該行第一個(gè)單元格的數(shù)據(jù)
XSSFCell cell0 = row.getCell(0);
System.out.println("cello對(duì)象:" + cell0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制臺(tái)輸出結(jié)果:可以發(fā)現(xiàn)具體到行對(duì)象時(shí),就解析成xml文件了
xssfWorkbook對(duì)象: Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml sheet對(duì)象: Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml row對(duì)象: <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對(duì)象:姓名
以上可以實(shí)現(xiàn)了讀取某行某單元格的數(shù)據(jù),那么接下來就該讀取整個(gè)表的所有數(shù)據(jù)了:
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 {
//創(chuàng)建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\test-excel\\student.xlsx"));
System.out.println("xssfWorkbook對(duì)象:" + xssfWorkbook);
//讀取第一個(gè)工作表
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
System.out.println("sheet對(duì)象:" + sheet);
//獲取最后一行的num,即總行數(shù)。此處從0開始計(jì)數(shù)
int maxRow = sheet.getLastRowNum();
System.out.println("總行數(shù)為:" + maxRow);
for (int row = 0; row <= maxRow; row++) {
//獲取最后單元格num,即總單元格數(shù) ***注意:此處從1開始計(jì)數(shù)***
int maxRol = sheet.getRow(row).getLastCellNum();
System.out.println("--------第" + row + "行的數(shù)據(jù)如下--------");
for (int rol = 0; rol < maxRol; rol++){
System.out.print(sheet.getRow(row).getCell(rol) + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制臺(tái)輸出:
xssfWorkbook對(duì)象:Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml sheet對(duì)象:Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml 總行數(shù)為:2 --------第0行的數(shù)據(jù)如下-------- 姓名 學(xué)號(hào) 班級(jí) 入學(xué)日期 --------第1行的數(shù)據(jù)如下-------- 張三 2.0190001E7 三班 01-八月-2019 --------第2行的數(shù)據(jù)如下-------- 李四 2.0190002E7 三班 01-八月-2019
注意:2.0190001E7 = 2.0190001 * 107 = 20190001
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot /tmp 臨時(shí)目錄的具體實(shí)現(xiàn)
springboot應(yīng)用服務(wù)再啟動(dòng)的時(shí)候,會(huì)在操作系統(tǒng)的/tmp目錄,本文主要介紹了springboot /tmp 臨時(shí)目錄的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Arthas在線java進(jìn)程診斷工具在線調(diào)試神器詳解
Arthas是 Alibaba 開源的Java診斷工具,深受開發(fā)者喜愛。這篇文章主要介紹了Arthas在線java進(jìn)程診斷工具 在線調(diào)試神器,需要的朋友可以參考下2021-11-11
基于自定義BufferedReader中的read和readLine方法
下面小編就為大家分享一篇基于自定義BufferedReader中的read和readLine方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
jmeter如何自動(dòng)生成測(cè)試報(bào)告
這篇文章主要介紹了jmeter如何自動(dòng)生成測(cè)試報(bào)告,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹形(Tree)結(jié)構(gòu)
本文主要介紹了Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹形(Tree)結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Maven如何解決添加依賴之后沒有加載jar包報(bào)錯(cuò)問題
這篇文章主要介紹了Maven如何解決添加依賴之后沒有加載jar包報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot 接口返回字符串帶引號(hào)的問題解決
本文主要介紹了springboot 接口返回字符串帶引號(hào)的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java.net.SocketTimeoutException: Read timed o
本文主要介紹了java.net.SocketTimeoutException: Read timed out異常的解決,可能是因?yàn)榫W(wǎng)絡(luò)延遲、服務(wù)器響應(yīng)慢或連接不穩(wěn)定等原因造成的,下面就一起來介紹一下,感興趣的可以了解一下2024-05-05

