当前位置:网站首页>Apache POI implements excel import, read data, write data and export
Apache POI implements excel import, read data, write data and export
2022-07-29 05:11:00 【nianyuw】
Apache POI
POI Introduce
Apache POI Yes, it is Java Write free open source cross platform Java API,Apache POI Provide API to Java Program pair Microsoft Office Format file reading and writing functions , One of the most used is the use of POI operation Excel file .
maven coordinate :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
POI structure :
HSSF - Provide reading and writing Microsoft Excel XLS The function of format file
XSSF - Provide reading and writing Microsoft Excel OOXML XLSX The function of format file
HWPF - Provide reading and writing Microsoft Word DOC The function of format file
HSLF - Provide reading and writing Microsoft PowerPoint The function of format file
HDGF - Offer to read Microsoft Visio The function of format file
HPBF - Offer to read Microsoft Publisher The function of format file
HSMF - Offer to read Microsoft Outlook The function of format file
Introductory cases
ExcelTest .java file
from Excel File read data
package tech.niua.common.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
public class ExcelTest {
public static void main(String[] args) throws IOException {
// Create Workbook
XSSFWorkbook workbook = new XSSFWorkbook("E:\\qcby_software\\ Test data \\test.xlsx");
// Get worksheet , It can be obtained according to the order of the worksheet , You can also get... By the name of the worksheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Set cell type , Not set some number The data will report an error
sheet.setCellType(CellType.STRING);
// Traverse the worksheet to get the row object
for (Row row : sheet) {
// Traverse the row object to get the cell object
for (Cell cell : row) {
// Get the value in the cell
String value = cell.getStringCellValue();
System.out.print(value +" ");
}
System.out.println();
}
workbook.close();
}
}
The above introductory case can be seen ,POI operation Excel The table encapsulates several core objects :
XSSFWorkbook: workbook
XSSFSheet: Worksheet
Row: That's ok
Cell: Cell
The above case is to get rows by traversing the worksheet , Traverse rows to get cells , Finally get the value in the cell .
There is another way to get the row number of the worksheet , To get the line object according to the line number , Get the index of the last cell from the row , Thus, one cell object of each row is obtained according to the cell index , The code is as follows :
package tech.niua.common.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
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 java.io.IOException;
public class ExcelTest {
public static void main(String[] args) throws IOException {
// Create Workbook
XSSFWorkbook workbook = new XSSFWorkbook("E:\\qcby_software\\ Test data \\test.xlsx");
// Get worksheet , It can be obtained according to the order of the worksheet , You can also get... By the name of the worksheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Set cell type , Not set some number The data will report an error
sheet.setCellType(CellType.STRING);
// Gets the line number of the last line of the current sheet , Line number from 0 Start
int lastRowNum = sheet.getLastRowNum();
for(int i=0;i<=lastRowNum;i++){
// Get the line object according to the line number
XSSFRow row = sheet.getRow(i);
short lastCellNum = row.getLastCellNum();
for(short j=0;j<lastCellNum;j++){
String value = row.getCell(j).getStringCellValue();
System.out.print(value +" ");
}
System.out.println();
}
workbook.close();
}
}
Be careful :4.1.2 Version of poi No longer in use sheet.setCellType(CellType.STRING); This is set to String This form of type , Will no longer support
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
Change it to DataFormatter To modify the data format
DataFormatter formatter = new DataFormatter();
String value = formatter.formatCellValue(rowValue.getCell(j));
After modification
package tech.niua.common.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
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 java.io.IOException;
public class ExcelTest {
public static void main(String[] args) throws IOException {
// Create Workbook
XSSFWorkbook workbook = new XSSFWorkbook("E:\\qcby_software\\ Test data \\test.xlsx");
// Get worksheet , It can be obtained according to the order of the worksheet , You can also get... By the name of the worksheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Gets the line number of the last line of the current sheet , Line number from 0 Start
int lastRowNum = sheet.getLastRowNum();
for(int i=0;i<=lastRowNum;i++){
// Get the line object according to the line number
XSSFRow row = sheet.getRow(i);
short lastCellNum = row.getLastCellNum();
for(short j=0;j<lastCellNum;j++){
DataFormatter formatter = new DataFormatter();
String value = formatter.formatCellValue(row.getCell(j));
System.out.print(value +" ");
}
System.out.println();
}
workbook.close();
}
}
Running effect


In the actual development process , You can transform this tool class , Through the excel The uploaded address is passed to the tool class for reading , The read value can be received in the form of a set , Or receive as a collection object , In this way, you can do an operation on the read data .
towards Excel File write data
Use POI You can create a... In memory Excel File and write data to this file , Finally, through the output stream, the Excel Download files to disk
package tech.niua.common.utils.uuid;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelTest2 {
public static void main(String[] args) throws IOException {
// Create a... In memory Excel file
XSSFWorkbook workbook = new XSSFWorkbook();
// Create sheet , Specify the sheet name
XSSFSheet sheet = workbook.createSheet("TEST surface ");
// Create lines ,0 The first line
XSSFRow row = sheet.createRow(0);
// Create cells ,0 Represents the first cell
row.createCell(0).setCellValue(" Number ");
row.createCell(1).setCellValue(" name ");
row.createCell(2).setCellValue(" Age ");
XSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("1");
row1.createCell(1).setCellValue(" Xiao Ming ");
row1.createCell(2).setCellValue("10");
XSSFRow row2 = sheet.createRow(2);
row2.createCell(0).setCellValue("2");
row2.createCell(1).setCellValue(" Xiao Wang ");
row2.createCell(2).setCellValue("20");
// Through the output stream workbook Object to disk
FileOutputStream out = new FileOutputStream("E:\\qcby_software\\ Test data \\TEST surface .xlsx");
workbook.write(out);
out.flush();
out.close();
workbook.close();
}
}
test result


边栏推荐
- Simple user-defined authentication interface rules
- 【config】配置数组参数
- Improve the readability of your regular expressions a hundred times
- 2021-11-02
- MySQL regularly calls preset functions to complete data update
- Conv1d of torch
- On prepayment of house purchase
- How to install Office2010 installation package? How to install Office2010 installation package on computer
- How to solve the problem of configuring the progress every time Office2010 is opened?
- 1 sentence of code, get asp Net core binds multiple sources to the same class
猜你喜欢

Deadlock analysis using jstack, jconsole, and jvisualvm

Force deduction ----- sort odd and even subscripts respectively

Jackson parsing JSON detailed tutorial

How to monitor micro web services

WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
![[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]](/img/ee/b424d876c64dac652d76f9f26e4b20.png)
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]

Office提示系统配置无法运行怎么办?

Mysql语句中的函数

How does excel filter out the content you want? Excel table filtering content tutorial

< El table column> place multiple pictures
随机推荐
The song of the virtual idol was originally generated in this way!
时间序列分析的表示学习时代来了?
Force deduction ----- sort odd and even subscripts respectively
Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
Let you understand several common traffic exposure schemes in kubernetes cluster
搭建手机APP需要用到什么服务器
P1009 [noip1998 popularization group] sum of factorials
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
excel怎么设置行高和列宽?excel设置行高和列宽的方法
2021-11-02
Mysql的自连接和联合查询
2021-10-23
How to add traffic statistics codes to the legendary Development Zone website
电脑无法打开excel表格怎么办?excel打不开的解决方法
Conv2d of torch
stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解
Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
How to monitor micro web services
Traffic flow prediction pit climbing record (I): traffic flow data set, original data
Unity metaverse (III), protobuf & socket realize multi person online