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是針對.xls文件,XSSFWorkbook是針對.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對象:" + xssfWorkbook);
//讀取第一個(gè)工作表(這里的下標(biāo)與list一樣的,從0開始取,之后的也是如此)
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
System.out.println("sheet對象:" + sheet);
//獲取第一行的數(shù)據(jù)
XSSFRow row = sheet.getRow(0);
System.out.println("row對象:" + row);
//獲取該行第一個(gè)單元格的數(shù)據(jù)
XSSFCell cell0 = row.getCell(0);
System.out.println("cello對象:" + cell0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制臺輸出結(jié)果:可以發(fā)現(xiàn)具體到行對象時(shí),就解析成xml文件了
xssfWorkbook對象: Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml sheet對象: Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml row對象: <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對象:姓名
以上可以實(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對象:" + xssfWorkbook);
//讀取第一個(gè)工作表
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
System.out.println("sheet對象:" + 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();
}
}
}
控制臺輸出:
xssfWorkbook對象:Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml sheet對象:Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml 總行數(shù)為:2 --------第0行的數(shù)據(jù)如下-------- 姓名 學(xué)號 班級 入學(xué)日期 --------第1行的數(shù)據(jù)如下-------- 張三 2.0190001E7 三班 01-八月-2019 --------第2行的數(shù)據(jù)如下-------- 李四 2.0190002E7 三班 01-八月-2019
注意:2.0190001E7 = 2.0190001 * 107 = 20190001
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ThreadPoolExecutor中的submit()方法詳細(xì)講解
在使用線程池的時(shí)候,發(fā)現(xiàn)除了execute()方法可以執(zhí)行任務(wù)外,還發(fā)現(xiàn)有一個(gè)方法submit()可以執(zhí)行任務(wù),本文就詳細(xì)的介紹一下ThreadPoolExecutor中的submit()方法,具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04
SpringBoot中使用Cookie實(shí)現(xiàn)記住登錄的示例代碼
這篇文章主要介紹了SpringBoot中使用Cookie實(shí)現(xiàn)記住登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
從java源碼分析線程池(池化技術(shù))的實(shí)現(xiàn)原理
這篇文章主要介紹了從java源碼分析線程池(池化技術(shù))的實(shí)現(xiàn)原理,池化技術(shù)是一種編程技巧,當(dāng)程序出現(xiàn)高并發(fā)時(shí),能夠明顯的優(yōu)化程序,降低系統(tǒng)頻繁創(chuàng)建銷毀連接等額外開銷,下文更多的相關(guān)介紹需要的小伙伴可以參考一下2022-04-04

