Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic usage of Apache POI

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about the basic use of Apache POI, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Basic introduction

POI

Pache POI is a free, open source, cross-platform Java API,Apache POI written in Java that provides API to Java programs to read and write files in Microsoft Office format.

The most commonly used is to use POI to manipulate Excel files.

It can also manipulate other forms of documents such as word.

Jxl: specializes in operating Excel and Excel.

Using POI, you need to import maven coordinates

Org.apache.poi poi 3.14 org.apache.poi poi-ooxml 3.14

POI structure: some classes are provided when operating for different document forms

HSSF-provides the ability to read and write files in Microsoft Excel XLS format

XSSF-provides the ability to read and write files in Microsoft Excel OOXML XLSX format

HWPF-provides the ability to read and write files in Microsoft Word DOC format

HSLF-provides the ability to read and write files in Microsoft PowerPoint format

HDGF-provides the ability to read files in Microsoft Visio format

HPBF-provides the ability to read files in Microsoft Publisher format

HSMF-provides the ability to read files in Microsoft Outlook format

Getting started test (reading data from an Excel file)

Using POI, you can read data from an existing Excel file

Step 1: import maven coordinates

Here's the second step.

Step 2: create an Excel file

Step 3: write the test code package com.yy.test;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.junit.Test;import java.io.File;import java.io.FileInputStream / * * @ author Marston * @ date 2021-10-29 * / public class POITest {@ Test public void test () throws Exception {/ / pass in an input stream, load the specified file, and create an Excel object (workbook) XSSFWorkbook excel = new XSSFWorkbook (new File ("E:\\ testNomal\\ poi.xlsx")) / / read the first Sheet tag page XSSFSheet sheet = excel.getSheetAt (0) in the Excel file / / there are many rows in a sheet page. Iterate through the sheet tab, get each row of data for (Row row: sheet) {/ / traverse the rows, and get each cell object for (Cell cell: row) {/ / cell represents the cell object System.out.println (cell.getStringCellValue ()) / / the second column of getStringCellValue cannot be converted to String type because it is a numeric value. / / just change the contents of the second column in the Excel table to string type}} / / close the Excel file excel.close ();}}

Running result:

Because it is an introductory case, I will change the type here to the following, after modifying the contents of the Excel file:

Code description and extension

As you can see from the primer above, POI manipulates the Excel table encapsulating several core objects:

XSSFWorkbook: workbook

XSSFSheet: worksheet

Row: OK

Cell: cell

The above example is to get the row by traversing the worksheet, traversing the row to get the cell, and finally getting the value in the cell.

Another way is to get the last row number of the worksheet, get the row object according to the row number, get the last cell index through the row, and get a cell object for each row according to the cell index, as follows:

Package com.yy.test;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.junit.Test;import java.io.File;import java.io.FileInputStream / * * @ author Marston * @ date 2021-10-29 * / public class POITest {@ Test public void test2 () throws Exception {/ / pass in an input stream, load the specified file, and create an Excel object (workbook) XSSFWorkbook excel = new XSSFWorkbook (new File ("E:\\ testNomal\\ poi.xlsx")) / / read the first Sheet tag page in the Excel file, XSSFSheet sheet = excel.getSheetAt (0); / / get the line number of the last row of the current worksheet, starting with 0, int lastRowNum = sheet.getLastRowNum (); System.out.println ("lastRowNum:" + lastRowNum); for (int item0 / I)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report