当前位置:网站首页>POI dealing with Excel learning
POI dealing with Excel learning
2022-07-03 06:14:00 【Muyu】
POI Study
Import dependencies first
<dependencies>
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- Date formatting tool -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!--test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
Excel2003 The version can only be put at most 65536 Row data (xls)
Excel2007 The version does not have this limitation (xlsx)
Excel form
- workbook
- Worksheet
- That's ok
- Column
- Cell
- Worksheet
Big files write HSSF
shortcoming : At most, it can only deal with 65536 That's ok , Otherwise, an exception will be thrown
advantage : Write cache in process , Do not operate disk , The last one-time write to disk , Fast
Big files write XSSF
shortcoming : Writing data is very slow , Very memory intensive , Memory overflow can also occur , Such as 100 Ten thousand
advantage : Can write a large amount of data , Such as 20 Ten thousand
Big files write SXSSF
advantage : Can write a large amount of data , Such as 20 Ten thousand
operation Excel
Create a Excel 07 Version of xlsx file
package com.muyu.main;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.joda.time.DateTime;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWriteTest {
private static String path = "E:\\code\\IDEA code\\ControlExcel\\Excel-poi";
public static void main(String[] args) throws IOException {
//1. Create Workbook
Workbook workbook = new SXSSFWorkbook();
//2. Create a worksheet
Sheet sheet = workbook.createSheet(" Fan Xu practice table ");
//3. Create a line
Row row1 = sheet.createRow(0);
//4. Create a cell
Cell cell11 = row1.createCell(0);
cell11.setCellValue(" New learning today :");
Cell cell12 = row1.createCell(1);
cell12.setCellValue("poi");
// The second line
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell21.setCellValue(" Count the time :");
cell22.setCellValue(time);
FileOutputStream fileOutputStream = new FileOutputStream(path + "\\FanXu.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
}
image xls Write large amount of data in
@Test
public void xls65536Test() throws IOException {
// Fast execution
long begain = System.currentTimeMillis();
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("66636");
for (int i = 0; i <65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j <10 ; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "\\xls65536Test.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("over");
long end = System.currentTimeMillis();
double time = (double)(end-begain)/1000 ;
System.out.println(time);
}
//over Output
//1.776
towards xlsx Write large amount of data in
@Test
public void xlsx65536Test() throws IOException {
// Slow execution 1 Ten thousand have been implemented 2.682 second
long begain = System.currentTimeMillis();
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("66636");
for (int i = 0; i <10000; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j <10 ; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "\\xlsx65536Test.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("over");
long end = System.currentTimeMillis();
double time = (double)(end-begain)/1000 ;
System.out.println(time);
}
//over Output
//2.682
Optimize ! The second method is to xlsx Write large amount of data in
@Test
public void sxlsx65536Test() throws IOException {
//20 Ten thousand have been implemented 4.205 second
long begain = System.currentTimeMillis();
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet("66636");
for (int i = 0; i <200000; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j <10 ; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "\\xlsx65536Test.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
((SXSSFWorkbook)workbook).dispose();
System.out.println("over");
long end = System.currentTimeMillis();
double time = (double)(end-begain)/1000 ;
System.out.println(time);
}
//over Output
//4.205
read Excel The operation of
package com.muyu.main;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelReadTest {
private static String path = "E:\\code\\IDEA code\\ControlExcel\\Excel-poi";
@Test
public void getExcelValue() throws IOException {
FileInputStream fileInputStream = new FileInputStream("FanXu.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet(" Fan Xu practice table ");
Row row = sheet.getRow(0);
Cell cell = row.getCell(1);
System.out.println(cell.getStringCellValue());
fileInputStream.close();
}
}
边栏推荐
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- Understand the first prediction stage of yolov1
- [system design] proximity service
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Leetcode solution - 02 Add Two Numbers
- Simple solution of small up main lottery in station B
- Introduction to software engineering
- Kubernetes notes (II) pod usage notes
- 多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
- Why should there be a firewall? This time xiaowai has something to say!!!
猜你喜欢
Method of converting GPS coordinates to Baidu map coordinates
剖析虚幻渲染体系(16)- 图形驱动的秘密
Oauth2.0 - using JWT to replace token and JWT content enhancement
Oauth2.0 - explanation of simplified mode, password mode and client mode
Kubesphere - Multi tenant management
GPS坐标转百度地图坐标的方法
Deep learning, thinking from one dimensional input to multi-dimensional feature input
Synthetic keyword and NBAC mechanism
Simple handwritten ORM framework
23 design models
随机推荐
Synthetic keyword and NBAC mechanism
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Code generator - single table query crud - generator
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
从 Amazon Aurora 迁移数据到 TiDB
Simple solution of small up main lottery in station B
88. 合并两个有序数组
Leetcode solution - 01 Two Sum
Kubernetes notes (10) kubernetes Monitoring & debugging
Zhiniu stock -- 03
conda和pip的区别
Simple handwritten ORM framework
Naive Bayes in machine learning
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Selenium ide installation recording and local project maintenance
Oracle database synonym creation
88. Merge two ordered arrays