Get the App
SLTechnology News&Howtos  ›  Development  › 

How JAVA uses POI to read EXCEL files

Shulou Source: shulou.com Published: 2022-06-03 09:20:47 09月25日 Update

This article mainly introduces JAVA how to use POI to read EXCEL files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The main difference is that jxl does not support .xlsx, while poi supports .xlsx

Using the poi approach (XSSFWorkbook) introduced here, poi actually provides two implementation classes, HSSFWorkbook and XSSFWorkbook. The difference is that HSSFWorkbook is for .xls files and XSSFWorkbook is for .xslx files.

First of all, let's clarify the basic concepts:

First create a workbook, a workbook can have multiple worksheets, a worksheet can have multiple rows, a row can have multiple cells

Workbook > XSSFWorkbook

Worksheet > XSSFSheet

Line > XSSFRow

Cell > XSSFCell

The following figure shows the contents of the created student.xlsx:

Read the student.xlsx file code:

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 {/ / create workbook XSSFWorkbook xssfWorkbook = new XSSFWorkbook (new FileInputStream ("D:\ test-excel\\ student.xlsx"); System.out.println ("xssfWorkbook object:" + xssfWorkbook) / / read the first worksheet (here the subscript is the same as list, starting with 0, and then the same) XSSFSheet sheet = xssfWorkbook.getSheetAt (0); System.out.println ("sheet object:" + sheet); / / get the data of the first row XSSFRow row = sheet.getRow (0); System.out.println ("row object:" + row); / / get the data of the first cell of the row XSSFCell cell0 = row.getCell (0)

Console output result: when you can find a specific line object, it will be parsed into a xml file

XssfWorkbook object: Name: / xl/workbook.xml-Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xmlsheet object: Name: / xl/worksheets/sheet1.xml-Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xmlrow object: 0 1 2 3 cello object: name

The above can be achieved to read the data of a certain row and a certain cell, then it is time to read all the data of the entire table:

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 {/ / create workbook XSSFWorkbook xssfWorkbook = new XSSFWorkbook (new FileInputStream ("D:\ test-excel\\ student.xlsx"); System.out.println ("xssfWorkbook object:" + xssfWorkbook); / / read the first worksheet XSSFSheet sheet = xssfWorkbook.getSheetAt (0) System.out.println ("sheet object:" + sheet); / / gets the num of the last line, that is, the total number of rows. Count here from 0: int maxRow = sheet.getLastRowNum (); System.out.println ("total number of rows:" + maxRow); for (int row = 0; row)

Tags: Object work data file unit article multiple head line support one line name console control output next following figure subscript two code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Shulou Tech Info MySQL NVidia