In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.