当前位置:网站首页>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


边栏推荐
- JS daily question (11)
- Vivo market API event reporting and docking
- 玩家访问网站自动弹窗加QQ群方法以及详细代码
- Climbing the pit of traffic flow prediction (III): using pytorch to realize LSTM to predict traffic flow
- JS daily question (12)
- Mysql多对多关系,分组拼接把多个数据查询到一条数据上
- Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
- Jackson解析JSON详细教程
- Deadlock to be resolved
- P5714 [deep foundation 3. Case 7] obesity
猜你喜欢

时间序列分析的表示学习时代来了?
![[file download] easyexcel quick start](/img/fd/b3b08c67c5a81f0ab58c20653f9398.png)
[file download] easyexcel quick start

Glory 2023 push, push code ambubk

Big silent event Google browser has no title

SparkSql批量插入或更新,保存数据到Mysql中

Pivot table of odoo development tutorial

【文件下载】Easyexcel快速上手

【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】

MySQL定时调用预置函数完成数据更新

Climbing the pit of traffic flow prediction (III): using pytorch to realize LSTM to predict traffic flow
随机推荐
电脑无法打开excel表格怎么办?excel打不开的解决方法
pytorch学习笔记
Wechat picture identification
MySQL定时调用预置函数完成数据更新
How to add a map to the legendary server
JS daily question (11)
Operator operation list of spark
怎样监测微型的网站服务
What servers are needed to build mobile app
Solve the warning prompt of MySQL mapping file
[config] configure array parameters
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
Solution | get the relevant information about the current employees' highest salary in each department |
office2010每次打开都要配置进度怎么解决?
Google gtest事件机制
深度学习刷SOTA的一堆trick
如何安装office2010安装包?office2010安装包安装到电脑上的方法
Jackson parsing JSON detailed tutorial
How to debug UDP port
Sparksql inserts or updates in batches and saves data to MySQL