溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過程解析

發(fā)布時間:2020-09-02 08:41:37 來源:腳本之家 閱讀:173 作者:坤~~ 欄目:編程語言

經(jīng)過一番搜索發(fā)現(xiàn),java操縱excel文件常用的有jxl和poi兩種方式,孰好孰壞看自己需求而定。

其中最主要的區(qū)別在于jxl不支持.xlsx,而poi支持.xlsx

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

首先明確一下基本概念:

  先創(chuàng)建一個工作簿,一個工作簿可以有多個工作表,一個工作表可以有多個行,一個行可以有多個單元格

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

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

  行 >>>>>>>>XSSFRow

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

下圖為創(chuàng)建的student.xlsx的內(nèi)容:

JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過程解析

讀取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);
      //讀取第一個工作表(這里的下標與list一樣的,從0開始取,之后的也是如此)
      XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
      System.out.println("sheet對象:" + sheet);
      //獲取第一行的數(shù)據(jù)
      XSSFRow row = sheet.getRow(0);
      System.out.println("row對象:" + row);
      //獲取該行第一個單元格的數(shù)據(jù)
      XSSFCell cell0 = row.getCell(0);
      System.out.println("cello對象:" + cell0);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

控制臺輸出結(jié)果:可以發(fā)現(xiàn)具體到行對象時,就解析成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對象:姓名

以上可以實現(xiàn)了讀取某行某單元格的數(shù)據(jù),那么接下來就該讀取整個表的所有數(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);
      //讀取第一個工作表
      XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
      System.out.println("sheet對象:" + sheet);
       //獲取最后一行的num,即總行數(shù)。此處從0開始計數(shù)
      int maxRow = sheet.getLastRowNum();
      System.out.println("總行數(shù)為:" + maxRow);
      for (int row = 0; row <= maxRow; row++) {
        //獲取最后單元格num,即總單元格數(shù) ***注意:此處從1開始計數(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ù)如下--------
姓名 學號 班級 入學日期 
--------第1行的數(shù)據(jù)如下--------
張三 2.0190001E7 三班 01-八月-2019 
--------第2行的數(shù)據(jù)如下--------
李四 2.0190002E7 三班 01-八月-2019

注意:2.0190001E7 = 2.0190001 * 107 = 20190001

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI