java使用poi(XSSFWorkbook)讀取excel(.xlsx)文件


經過一番搜索發現,java操縱excel文件常用的有jxl和poi兩種方式,孰好孰壞看自己需求而定。

其中最主要的區別在於jxl不支持.xlsx,而poi支持.xlsx

這里介紹的使用poi方式(XSSFWorkbook),實際上poi提供了HSSFWorkbookXSSFWorkbook兩個實現類。區別在於HSSFWorkbook是針對.xls文件XSSFWorkbook是針對.xslx文件。


 

首先明確一下基本概念:

  先創建一個工作簿,一個工作簿可以有多個工作表,一個工作表可以有多個行,一個行可以有多個單元格

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

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

  行        ----------->XSSFRow

  單元格 ----------->XSSFCell

下圖為創建的student.xlsx的內容:

 


導入依賴:

<!--poi-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>

讀取student.xlsx文件代碼:

 1 package com.zjk.testexcel;  2 
 3 import org.apache.poi.xssf.usermodel.*;  4 import java.io.FileInputStream;  5 import java.io.IOException;  6 
 7 /**
 8  * @Auther: zjk  9  * @Date: 2019/8/30 10  * @Description: 11  */
12 public class TestExcel1 { 13     public static void main(String[] args) { 14         try { 15             //創建工作簿
16             XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\test-excel\\student.xlsx")); 17             System.out.println("xssfWorkbook對象:" + xssfWorkbook); 18             //讀取第一個工作表(這里的下標與list一樣的,從0開始取,之后的也是如此)
19             XSSFSheet sheet = xssfWorkbook.getSheetAt(0); 20             System.out.println("sheet對象:" + sheet); 21             //獲取第一行的數據
22             XSSFRow row = sheet.getRow(0); 23             System.out.println("row對象:" + row); 24             //獲取該行第一個單元格的數據
25             XSSFCell cell0 = row.getCell(0); 26             System.out.println("cello對象:" + cell0); 27         } catch (IOException e) { 28  e.printStackTrace(); 29  } 30  } 31 }

控制台輸出結果:可以發現具體到行對象時,就解析成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對象:姓名

以上可以實現了讀取某行某單元格的數據,那么接下來就該讀取整個表的所有數據了:

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+xml sheet對象: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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM