EasyExcel is one of the open source poi plug-ins in Alibaba, which mainly solves the problems such as the complex use of poi framework, the difficult operation of sax parsing method, and the large amount of data easy to OOM, thus solving the errors caused by POI concurrency.
The main solution: by decompressing files and loading them line by line, discarding unimportant data such as styles and fonts, the occupation of memory is reduced.
Advantages of EasyExcel
Annotate custom actions.
I/O is simple, it provides an interface for I/O process.
Support some flexible operations such as cell merging.
Second, common notes
@ExcelProperty
Specifies which column in excel the current field corresponds to. You can match by name or index. Of course, you can leave it blank. By default, the first field is index=0, and so on. Be careful, either don't write it all, or use index all or names all to match it. Don't mix these three together unless you know exactly how to sort them in the source code. If you study Spring Boot, recommend a free tutorial that has been updated continuously for many years: /spring-boot-learning-2x/
@ExcelIgnore
By default, all fields will match excel. If you add this comment, the field will be ignored.
@ Date Time Format
Date conversion, receiving data in excel date format with String will call this annotation. The values refer to java.text.SimpleDateFormat
@ number format
Digital conversion. Receiving data in excel digital format with String will call this comment. The values refer to java.text.DecimalFormat
@ ExcelIgnoreUnannotated
By default, those who don't add ExcelProperty comments will participate in reading and writing, and those who do.
Third,? Com。 Alibaba easy Excel 2. 1.4 javax. Servlet Javax。 Servlet-API 4.0. 1 provides com. Alibaba express JSON 1.2.47 IV. Monitoring? /* * * Easy Excel Import Monitor */Public Class Excel Listener Extensions AnalyzeEventListener {//You can get this value through an example; Private list data = new ArrayList (); @ Override public void invoke(Object o,analysis context analysis context){ datas . add(o); //Data is stored in the list for batch processing or subsequent business logic processing. do something(o); //Handle according to your own business} private void do something (object object) {/1,warehousing call interface} public list get data () {return data; } public void set datas(List datas){ this . datas = datas; } @ Override public void doafterallanalyzed(analysis context analysis context){//datas . clear(); //Destroy unused resources after parsing }} 5. Import the Excel trial interface {//to get the file name string filename = file. getOriginalfilename(); //Get the file stream inputstream = file.getinputstream (); //Instantiate the class Excel Listener = New Excel Listener () that implements the AnalysisEventListener interface; //Pass in the parameter Excel Reader Excel Reader = New Excel Reader (input stream, exceltypeenum.xls, null, listener); //Excel reader for reading information. Read (news page (1, 0, test. Class)); //Get data list list = listener . Get data(); if(list . size()& gt; 1){ for(int I = 0; I< list. size (); i++){ Test obj =(Test)list . get(I); JSON object jo = new JSON object(); }}} catch (exception e) {system.out.println (e.getmessage ()); }, the interface exports Excel.
HttpServletResponse response, HttpServletRequest request
Try {string filenames = "11111"; string User Agent = request . get header(" User-Agent "); if(user agent . contains(" MSIE ")| | user agent . contains(" Trident ")){ filenames = urlencoder . encode(filenames," UTF-8 "); } else { filenames = new String(filenames . getbytes(" UTF-8 ")," ISO-8859- 1 "); } response . setcontenttype(" application/vnd . ms-exce "); response . setcharacterencoding(" utf-8 "); response . add header(" Content-Disposition "," filename=" + filenames +"。 xlsx”); easy excel . write(response . get output stream()、Test.class)。 Paper ("paper"). DoWrite (test list); } catch (Exception e) {} In addition, if you study Spring Cloud, recommend a free tutorial that has been updated for many years: /spring-cloud-learning/ VII. Local import and local export list test list = new array list
The above is the basic use process of EasyExcel. Welcome to like and pay attention to communication.
Source | /p/4e6aa6342b33
More good articles
Ten thousand words will take you to learn ElasticSearch.