The most complete history of reading and writing excel technology sharing

This article about the public with an exclusive license number:
[Xinhua] before and after the end of the development
[House] script

[ Quick, simple process to avoid java OOM Excel tool ] presentation about the project on github

Brief introduction

poi use userModel mode feature of this model is easy to get started. Write code is very complicated. And very few public places. Every cause needs to be rewritten to read and write excel.
EasyExcel using SAX mode such easyexcel save memory. And easyexcel solve the memory leak problem. If you want to know the SAX model development costs that learning takes 3 to 5 days.

Export to commonly used several methods excel

By Java to read and write excel about the following:
POI, CSV, JXL, JXLS, easyPoi, easyExcel
according to the performance of their sort:
JXL, easyexcel, CSV, POI, easypoi, JXLS

THEN

  • POI is an open source project of apache. He is based on Microsoft to provide an API for Java programs. Reading through which we can control the content of the cell excel and writing style.
  • But precisely because of the details of his lead us to develop together a lot of code. And can not be pulled out.

CSV

  • csv is actually a text, but can be opened by the office of a Chinese book. If you open a real excel objects you will find is actually a binary file through the normal text tool. Because csv is a text, it is to read the actual text of his time reading and writing. No POI's workbook, sheet, row, cell said. So reading and writing efficiency or quickly.
  • But because the text is so that we can not control the cell style. Such as style, add drop-down box, a merged cell and the like.

jxl

  • jxl practical and almost POI. As both the idea, is through a table object - "cells of the page -" line - "column -" logic cell to the read and write operations. Common features are substantially providing method. Difference is the order parameter passing method. Compare performance on both jxl better performance.
  • Because the POI popular. jxl not very well known. It also found the time to organize jxl. I did not know the shortcomings of jxl. I have to point out the shortcomings so that he and poi logic is not the same. Write up a little awkward.

jxls

  • It should be worth noting that jxls jxl and nothing to do. Use of logic is both poles Wan else. jxls better focus is to write the template excel itself. jxls frame is rendered in a data template injection. His greatest amount of code is a bit small. Basically we just need to prepare the data can be derived.
  • Because it is based on a template. So jxls achieve export is very simple. But realize it is harder to read the data here. Here we temporarily do not know how to achieve. This issue is left to the intelligent reader now! ! !

    easypoi

  • easypoi and easyexcel very similar. Excel both header and one of the mapping entity object is achieved by way of annotations. The other is a @Excel @ExcelProperty. Relative easyexcel, easypoi function relatively single point.
  • Both can be insufficient in case of their functions, to implement custom POI function by function

easyexcel

  • easyexcel is the focus. He previously POI of complex code into the module pulled annotation-based approach. Basically, we need only need to be resolved in excelproperty annotation.
    - easyexcel biggest feature is to solve the problem of memory leaks. Several more poi Export excel when the data are affected. And the performance is not very good. easyexcel is the best choice for POI products

Getting Started

easyexcel name is very consistent with his personality. He is really very easy. Let's implement the code export a student information


String fileName = EasyExcelTools.class.getResource("/").getPath() + "student" + System.currentTimeMillis() + ".xlsx";
ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(fileName, Student.class);
//excelWriterBuilder.registerConverter(new SexConverter()).registerWriteHandler(new AgeRowHandler()).registerWriteHandler(new SexCellWriteHandler());
ExcelWriter excelWriter = excelWriterBuilder.build();
WriteSheet writeSheet = EasyExcel.writerSheet("中化安元").build();
try {
    excelWriter.write(ts, writeSheet);
} catch (Exception e) {
    e.printStackTrace();
}finally {
    excelWriter.finish();
}

Code Reading

  • Student entity is required to export. Inside equipped with some basic information table
  • Student ts is a collection of data
  • fileName is exported file Address

    student


public class Student {

/**
  * 学生索引id
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"学号"})
private String id;
/**
  * 姓名
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"姓名"})
private String userName;

/**
  * 用户昵称
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"昵称"})
@ExcelIgnore
private String userNick;

/**
  * 年龄
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"年龄"})
private Integer age;
/**
  * 性别 true : 男  ; false : 女
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"性别"})
private boolean sex;
/**
  * 生日
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"生日"})
private Date birth;
/**
  * 身高
  */
@ExcelProperty(value = {HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME,"身高"})
private Double height;

}

to sum up

  • By easyexcel we just need to be ready to export data, and then export the two lines of code.

Common API

  • EasyExcel class entry, starts various operations for constructing
  • ExcelReaderBuilder ExcelWriterBuilder construct a ReadWorkbook WriteWorkbook, it can be understood as an excel objects, a build a long excel
  • ExcelReaderSheetBuilder ExcelWriterSheetBuilder construct a ReadSheet WriteSheet objects, can be understood as an excel inside, each page must construct a
  • ReadListener ReadListener will be called after each row has been read to process the data
  • WriteHandler comprising creating a cell in each operation, creating tables, etc. will be called to process the data WriteHandler
  • All configurations are inherited, Workbook configuration will be inherited Sheet, so set the parameters with EasyExcel time before EasyExcel ... sheet () method scope is the whole sheet, then for a single sheet

Cell styles

  • Because the layer is encapsulated. If easyexcel not satisfy us, we can go to the specific operation cell contents and style through workbook. This method is in use as a last resort. For example, we would want to change the cell style. easyexcel provides a development interface CellWriteHandler. We only need to implement this interface and override his beforeCellCreate, , whichafterCellCreate method is an opportune time ago destroyed after the cell is created. At this time we can change the cell contents. easyExcel capture interface provides four times CellWriteHandler WorkbookWriteHandler SheetWriteHandler RowWriteHandlerafterCellDisposeafterCellDispose



Merge Cells

  • In the POI, we realize we need to specify a merged cell merged range. However, when the contents of the header we only need to add annotations in the ExcelProperty easyexcel added at the same corresponding positions are automatically merged cell.


Data Style

  • Java development pattern data so that we often encounter. For example, student information in our rough sex in most cases in the database are controlled by 0,1. But when we exported certainly not directly show 01. This time we need a data pattern. He said the point is to understand the data format conversion. Converter provides the interface easyexcel.
    convertToJavaData: Excel data into Java objects
    convertToExcelData: Java Object convert excel data

Multi-sheet set

  • Multi-page fact sheet to create multiple sheet. Each sheet has a different number. The remaining operations are the same.

Cell add a hyperlink

  • CellWriteHandler achieved through afterCellDisposeimplementation of
CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
hyperlink.setAddress("https://gitee.com/zxhTom");
cell.setHyperlink(hyperlink);

rely


<dependency>
   <groupId>net.sourceforge.javacsv</groupId>
   <artifactId>javacsv</artifactId>
   <version>2.0</version>
</dependency>

Use version


<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.0-beta4</version>
</dependency>

Mainstay

  • ExcelProperty: entity property configuration notes
  • BaseRowModel: write entity inherits the entity class
  • WriteHandler: used to control the output of the cell, including style and formatting data
  • ExcelWriter: for export excel

notes

system time

1900 windowing 1900 date system
1904 windowing 1904 date system and
Excel for windows using 1900
Excel2008 previous versions for mac and 1904
Excel 2016 for mac; Excel 1900 for mac 2011

Read and write data format converter built

  • BigDecimalBooleanConverter()
  • BigDecimalNumberConverter();
  • BigDecimalStringConverter();
  • BooleanBooleanConverter();
  • BooleanNumberConverter();
  • BooleanStringConverter();
  • ByteBooleanConverter();
  • ByteNumberConverter();
  • ByteStringConverter();
  • DateNumberConverter();
  • DateStringConverter();
  • DoubleBooleanConverter();
  • DoubleNumberConverter();
  • DoubleStringConverter();
  • FloatBooleanConverter();
  • FloatNumberConverter();
  • FloatStringConverter();
  • IntegerBooleanConverter();
  • IntegerNumberConverter();
  • IntegerStringConverter();
  • LongBooleanConverter();
  • LongNumberConverter ();
  • LongStringConverter();
  • ShortBooleanConverter();
  • ShortNumberConverter (Converter data conversion interface);
  • Short String Converter ();
  • StringBooleanConverter();
  • StringNumberConverter();
  • StringStringConverter();
  • StringErrorConverter();

Model Build Event Listener

  • ModelBuildEventListener default is the first data listener, the main function is to read the data into the current line or entity map

write

  • FileUtils.createPoiFilesDirectory ();
    create a temporary cache directory during initialization to avoid POI concurrent writes error

Reading and writing process analysis

read

./easyexcel

write

./easyexcel

Join team

# Join team

Micro-channel public number

Micro-channel public number

Guess you like

Origin www.cnblogs.com/zhangxinhua/p/11833582.html