Excel file import method, device and equipment based on EasyExcel, medium and program product

Through pre-designed universal listening and reading classes and generic interface objects, the problems of code redundancy and low efficiency when importing EasyExcel are solved, and efficient Excel file import and support for complex data structures are achieved.

CN118820337APending Publication Date: 2024-10-22CHINA MOBILE M2M +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410719621.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-04
Publication Date
2024-10-22

AI Technical Summary

Technical Problem

When using EasyExcel to import Excel files, different listening and reading classes need to be created for different types of tables, resulting in code redundancy and low import efficiency, especially when the data structure is complex, which consumes a lot of time.

Method used

It uses pre-designed universal listening and reading classes, supports generics, and realizes batch insertion of different data types through common interface objects and reflection mechanisms, reducing the need to create listening and reading classes for different tables.

Benefits of technology

It improves the efficiency of Excel file import, reduces code redundancy, supports the import of complex data structures, and improves the stability of data processing through batch insertion of cache lists.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118820337A_ABST
    Figure CN118820337A_ABST
Patent Text Reader

Abstract

The invention discloses an Excel file importing method and device based on EasyExcel, equipment, a medium and a program product, a general monitoring reading class supporting multiple data types is obtained by utilizing a generic pre-design monitoring reading class, when the Excel file is imported, a first interface object corresponding to the to-be-imported Excel file is imported, and when the Excel file is imported, the first interface object corresponding to the to-be-imported Excel file is imported, the first interface object corresponding to the to-be-imported Excel file is imported. In this way, the data in the Excel file can be inserted into the database in batches based on the data batch insertion method in the first interface object directly through the preset general monitoring and reading class. And monitoring reading classes do not need to be created for tables of different data types. According to the method, the time consumed for importing the Excel file is shortened, the importing efficiency is improved, the code redundancy is reduced, and the code quality is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the technical field of Internet of Things, and in particular, relates to an Excel file import method, device, equipment, medium and program product based on EasyExcel. Background Art

[0002] In daily development work, reading and writing Excel files is a very common requirement, especially in the background management system. Operating Excel based on the traditional method is usually cumbersome. The emergence of EasyExcel has brought a simpler and more efficient solution for reading and writing Excel files. EasyExce is a Java library for reading and writing Excel files quickly and easily.

[0003] At present, when using EasyExcel to import Excel files into a database, a listening reading class corresponding to the Excel file is usually created, and the data in the Excel file is parsed, read and stored in this listening reading class. However, this method requires creating different listening reading classes for different types of tables in the Excel file to be imported. If the imported data structure is complex, it will take a lot of time to do repetitive things, affecting work efficiency and causing code redundancy. Summary of the invention

[0004] The embodiments of the present application provide an Excel file import method, apparatus, device, medium and program product based on EasyExcel, which can reduce the time required for Excel file import, improve import efficiency, and reduce code redundancy.

[0005] In a first aspect, an embodiment of the present application provides an Excel file import method based on EasyExcel, comprising:

[0006] Obtain parameters of the Excel file to be imported, the parameters at least including a first interface object corresponding to the Excel file, the first interface object is an instance of a preset universal listening reading class, the first interface object includes a data batch insert method, and the universal listening reading class supports generics;

[0007] Through the general listening reading class, each piece of data in the Excel file is parsed in turn;

[0008] After each piece of data is parsed, the parsed data corresponding to the data is stored in the cache list through the invoke method, and when the amount of parsed data stored in the cache list is greater than the data amount threshold, the saveData method is called. The cache list supports generics;

[0009] Through the saveData method, based on the reflection mechanism and the preset general interface object, the data batch insertion method in the first interface object is called, and the parsed data in the cache list is inserted into the database using the data batch insertion method.

[0010] In a second aspect, an embodiment of the present application provides an Excel file importing device based on EasyExcel, comprising:

[0011] An acquisition module is used to acquire parameters of the Excel file to be imported, wherein the parameters at least include a first interface object corresponding to the Excel file, the first interface object is an instance of a preset universal listening reading class, the first interface object includes a data batch insertion method, and the universal listening reading class supports generics;

[0012] The parsing module is used to parse each piece of data in the Excel file in turn through the general listening reading class;

[0013] The cache module is used to store the parsed data corresponding to each piece of data into the cache list through the invoke method after the parsing of each piece of data is completed, and to call the saveData method when the amount of parsed data stored in the cache list is greater than the data amount threshold. The cache list supports generics;

[0014] The storage module is used to call the data batch insertion method in the first interface object based on the reflection mechanism and the preset general interface object through the saveData method, and insert the parsed data in the cache list into the database using the data batch insertion method.

[0015] In a third aspect, an embodiment of the present application provides an electronic device, the electronic device comprising: a processor and a memory storing computer program instructions;

[0016] When the processor executes the computer program instructions, the Excel file import method based on EasyExcel as in the first aspect is implemented.

[0017] In a fourth aspect, an embodiment of the present application provides a computer storage medium having computer program instructions stored thereon. When the computer program instructions are executed by a processor, an Excel file import method based on EasyExcel as in the first aspect is implemented.

[0018] In a fifth aspect, an embodiment of the present application provides a computer program product. When the instructions in the computer program product are executed by a processor of an electronic device, the electronic device executes the Excel file import method based on EasyExcel as in the first aspect.

[0019] The Excel file import method, device, equipment, medium and program product based on EasyExcel of the embodiment of the present application utilizes generic pre-designed monitoring and reading classes to obtain a general monitoring and reading class that supports multiple data types. When importing an Excel file, the first interface object corresponding to the Excel file to be imported is passed in, and the data in the Excel file can be directly inserted into the database in batches through the pre-set general monitoring and reading class based on the data batch insertion method in the first interface object. There is no need to create monitoring and reading classes for tables of different data types respectively. The time required for importing Excel files is reduced, the import efficiency is improved, and the redundancy of the code is reduced, thereby improving the code quality. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] In order to more clearly illustrate the technical solution of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0021] Figure 1 It is a flowchart of an Excel file import method based on EasyExcel provided in an embodiment of the present application;

[0022] Figure 2 It is a flowchart of the EasyExcel initialization method provided in the embodiment of the present application;

[0023] Figure 3 It is a flowchart of an EasyExcel initialization method in an application scenario provided by an embodiment of the present application;

[0024] Figure 4 It is a flowchart of an EasyExcel initialization method in another application scenario provided by an embodiment of the present application;

[0025] Figure 5 It is a structural schematic diagram of an Excel file import method device based on EasyExcel provided in an embodiment of the present application;

[0026] Figure 6 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0027] The features and exemplary embodiments of various aspects of the present application will be described in detail below. In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are only intended to explain the present application, rather than to limit the present application. For those skilled in the art, the present application can be implemented without the need for some of these specific details. The following description of the embodiments is only to provide a better understanding of the present application by illustrating the examples of the present application.

[0028] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the statement "include..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.

[0029] EasyExcel is a Java-based open source project for reading and writing Excel files in a simple and memory-saving way. It supports reading and writing hundreds of megabytes of Excel files while saving as much memory as possible. Compared with other technical methods for reading and writing Excel files, EasyExcel has the following advantages:

[0030] Easy to use: EasyExcel provides an easy-to-use API, allowing developers to easily read and write Excel files without complicated settings and complex codes.

[0031] High performance: EasyExcel adopts an event-driven model, which can effectively process large-scale Excel data, with high read and write performance and low memory consumption.

[0032] Support for large amounts of data: EasyExcel supports reading and writing large amounts of Excel files and can process millions of data. It can also perform streaming reading and writing to reduce memory usage.

[0033] Convenient data conversion: EasyExcel provides rich data conversion functions, which can easily map Java objects to Excel cells, support custom data converters, and flexibly control the data reading, writing and conversion process.

[0034] Cross-platform compatibility: EasyExcel can run stably on different operating system platforms, such as Windows, Linux, Mac, etc., which perfectly matches the cross-platform characteristics of the Java language.

[0035] In summary, compared with other methods of importing Excel files, EasyExcel has the advantages of being simple and easy to use, high performance, large data support, convenient data conversion, rich export styles and cross-platform compatibility. It is a good choice for processing Excel files in Java development.

[0036] Currently, when importing Excel files based on EasyExcel, a corresponding listening reading class will be created for the Excel file to be imported. This will result in the creation of different listening reading classes when importing data into different database tables. If the imported data structure is complex, this will take up a lot of time to do repetitive tasks, affecting work efficiency and reducing code quality.

[0037] In order to solve the above problems, the embodiments of the present application provide a novel Excel file import method, device, equipment, medium and program product based on EasyExcel. The Excel file import method based on EasyExcel provided by the embodiments of the present application is first described below.

[0038] The inventive concept of the Excel file import method based on EasyExcel provided in the embodiment of the present application is to create a general listening reading class in advance, and parse and store the Excel file to be substituted through the general listening reading class. In this way, when creating the object of the general listening reading class, it is only necessary to write the import class into the generic type, and pass the interface object of the Excel file into the general listening reading class, so as to realize the function of importing Excel files.

[0039] See also Figure 1 A flow chart of an Excel file import method based on EasyExcel provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the method at least includes the following steps S11-S14, which are described in detail below.

[0040] S11. Obtain parameters of the Excel file to be imported, the parameters at least including a first interface object corresponding to the Excel file, the first interface object is an instance of a preset general listening reading class, the first interface object includes a data batch insertion method, and the general listening reading class supports generics.

[0041] In this embodiment, the Excel file is imported through the general listening reading class pre-set in EasyExcel, wherein the general listening reading class supports generics. Supporting generics can make the general listening reading class more flexible to handle data of different complex types. Data can be converted into corresponding objects according to the generic type to achieve support for different data structures, such as importing Excel tables containing complex data structures such as nested objects, collections or mappings. The flexibility of generics can help parse these complex data structures and convert them into Java objects for processing.

[0042] Based on this, before executing S11, EasyExcel can be initialized in advance to set a general listening and reading class therein. Afterwards, each time an Excel file is imported, the general listening and reading class can be directly used without creating a listening and reading class each time.

[0043] In some embodiments of the present application, Figure 2 As shown, the following steps S21-S24 can be used to initialize EasyExcel.

[0044] S21. Create a general listening read class that supports generics, and implement the listening read interface in the general listening read class.

[0045] In some embodiments of the present application, when creating a general listening reading class, a java class file named DemoListener can be created, and the java class file is set to be generic <e>And implement the interface ReadListener in the java class file through implements <e>, use the java class file as a general listening reading class. Among them, implements is a keyword used by a class to implement an interface, ReadListener <e>It is a monitoring read interface used to monitor events when reading data.

[0046] S22. Create a general interface object of type Object in the general listening and reading class.

[0047] Here, Object type is the basic type in programming languages ​​such as Java and C#, and it exists as the superclass of all classes.

[0048] In some embodiments of the present application, when creating a general interface object, an interface object of type Object can be created in the created general listening reading class DemoListener, and the interface object is used as the general interface object. Wherein, the interface object is mainly used to map data from one object to another object. It is used for data conversion, mapping and filtering, including but not limited to converting database query results into Java objects, converting XML data into JSON format, mapping a row of a database into a Java object, or mapping the attributes of an object into the attributes of another object, etc. In addition, the interface object also defines operation methods related to the database, such as insert, update, delete, query, etc., and by calling these methods, database operations can be easily performed.

[0049] In some embodiments of the present application, different general interface objects can be created according to different application scenarios. For example, when a simple Excel file is imported through a general listening reading class, a mapper object of an Object type can be created as a general interface object through a private Object mapper. When a complex Excel file is imported through a general listening reading class, a serviceImpl class of an Object type can be created as a general interface object. The serviceImpl class contains at least one mapper interface object, and the number of mapper interface objects in the serviceImpl class can be the same as the number of tables in the Excel file. Among them, a simple Excel file can include an Excel file whose number of tables is less than or equal to a quantity threshold, and a complex Excel file can include an Excel file whose number of tables is greater than a quantity threshold. The quantity threshold can be set according to actual conditions, for example, it can be 1. In this way, when multiple tables are contained in the Excel file to be imported, multiple tables can be imported at the same time.

[0050] A mapper interface object is an object or component used to map input data to output data. It is usually used to map data in a database to Java objects, or to map Java objects to data in a database. The serviceImpl class contains at least one mapper interface object, which is used to call methods defined in the mapper interface object.

[0051] S23. Initialize a cache list that supports generics, and set a data volume threshold for the cache list.

[0052] In this embodiment, the data in the Excel file to be written is written in batches through a general listening reading class. Based on this, a cache list with a data volume threshold is initialized. When the data stored in the cache list is greater than the corresponding data volume threshold, the data in the cache list is batch inserted into the database, so that the data import efficiency can be improved. Here, the cache list supports generics, so data of various data types can be stored in it.

[0053] In some embodiments of the present application, the private list <e>dataList = new ArrayList<>(BATCH_COUNT) to initialize a generic E list and set the size field name of the list to BATCH_COUNT(). BATCH_COUNT() is used to fill in the data volume threshold corresponding to the list. This list is used as a cache list, which is used to cache the parsed data. The data volume threshold in BATCH_COUNT() can be determined according to the specific server and business.

[0054] S24. Create a parameterized constructor for the general listening and reading class, where the input parameter of the parameterized constructor is the interface object.

[0055] In some embodiments of the present application, a parameterized constructor of the general listening reading class DemoListener can be created by public DemoDataListener(Object mapper){this.mapper=mapper;}, and the parameter of the parameterized constructor is the interface object. When the general interface object is a mapper interface object, the parameter of the parameterized constructor is the mapper interface object. When the general interface object is a serviceImpl class, the parameter of the parameterized constructor is the serviceImpl class. Among them, the parameterized constructor is a special constructor used to pass parameters to the object when creating the object. Through the parameterized constructor, the member variables of the object can be initialized when the object is instantiated. In this embodiment, the parameterized constructor is mainly used to initialize the member variables of the first interface object corresponding to the Excel file set by the user when importing the Excel file, to ensure that the first interface object can be passed into the general listening interface class.

[0056] After setting up the general listening and reading class, when importing Excel files based on EasyExcel, you can import the data of Excel files into the database through the general listening and reading class.

[0057] In some embodiments of the present application, when an Excel file is imported, the user may set parameters corresponding to the Excel file. The parameters corresponding to the Excel file include at least a first interface object corresponding to the Excel file, the first interface object is an instance of a general listening read class, and the first interface object includes a data processing method corresponding to the data type of the Excel file. The data processing method includes at least a data batch insertion method, which is used to insert data into a database in batches.

[0058] In some embodiments of the present application, the parameters of the Excel file may further include the file stream of the Excel file and the name and data type of each column in the Excel file, where the data type specifies what type of data the column can accommodate.

[0059] S12. Through the general listening reading class, each piece of data in the Excel file is parsed in turn.

[0060] In this embodiment, after obtaining the parameters of the Excel file, the Excel file can be parsed through the preset general listening reading class. When parsing the Excel file, each piece of data is parsed in turn, so as to obtain the parsed data corresponding to each piece of data. Among them, each piece of data corresponds to a row of data in the Excel file.

[0061] S13. After each piece of data is parsed, the parsed data corresponding to the data is stored in the cache list through the invoke method, and when the amount of parsed data stored in the cache list is greater than the data amount threshold, the saveData method is called. The cache list supports generics. The cache list supports generics.

[0062] Since the method of importing Excel files provided in this embodiment is different from the traditional import method, the traditional invoke method is no longer used. Based on this, before executing the above step S11, Figure 2 As shown, the following step S25 may also be performed:

[0063] S25. Rewrite the invoke method so that the invoke method can store the parsed data into the cache list, and call the saveData method when the amount of parsed data stored in the cache list is greater than the data amount threshold.

[0064] The saveData method is used to insert the data in the cache list into the database in batches.

[0065] In some embodiments of the present application, the invoke method can be overridden by annotating @Override.

[0066] In addition, in order to ensure that the cache list can be reused, the rewritten invoke method can also clear the cache list after inserting the data in the cache list into the database through the saveData method, so that the cache list can continue to store other data.

[0067] Based on this, when importing Excel files through the general listening reading class, after each piece of data is parsed, the overridden invoke method is called once, so that the parsed data corresponding to the data is stored in the cache list using the invoke method, and after each parsed data is written to the cache list, it is determined whether the amount of parsed data stored in the cache list is greater than the data amount threshold. When it is determined whether the amount of parsed data stored in the cache list is greater than the data amount threshold, the saveData method is called.

[0068] S14. Through the saveData method, based on the reflection mechanism and the preset general interface object, the data batch insertion method in the first interface object is called, and the parsed data in the cache list is imported into the database using the data batch insertion method.

[0069] In this embodiment, the saveData method is used to insert the data in the cache list into the database in batches. When the saveData method is called, it indicates that the data in the cache list needs to be inserted into the database in batches.

[0070] Since the method of importing Excel files provided in this embodiment is different from the traditional import method, the traditional saveData method is no longer used. Based on this, before executing the above step S11, Figure 2 As shown, the following step S27 may also be performed:

[0071] S27. Create a saveData method so that the saveData method can call the data batch insertion method in the first interface object based on the reflection mechanism and the general interface object.

[0072] The reflection mechanism in Java means that during the running of the program, the internal information of any class can be obtained, including member variables, constructors, member methods, etc., and the properties and methods of instance objects of the class can be operated.

[0073] Considering that if the first interface object is created directly through reflection mechanism, the first interface object created by reflection will not be able to instantiate DAO (Data Access Object) interface, resulting in inability to access the database. Therefore, in this embodiment, the first interface object cannot be created directly through reflection. Instead, a general interface object of Object type is used through reflection mechanism to call the specified in the first interface object.

[0074] In some embodiments of the present application, in the above step S14, the data batch insertion method in the first interface object can be called by the saveData method using the following steps S31-S35:

[0075] S31. Get the pre-created ApplicationContext object.

[0076] In some embodiments of the present application, the ApplicationContextAware interface may be implemented in the saveData method, based on which the created ApplicationContext object may be obtained through the ApplicationContextAware interface.

[0077] The main function of ApplicationContextAware is to facilitate the acquisition of the ApplicationContext object in the Spring container, so as to obtain the beans in the container. Among them, bean refers to the object managed by the container. Bean is a java object instantiated, assembled and managed by the Spring IOC container. In simple terms, bean is a java object.

[0078] ApplicationContext is a core interface in the Spring framework. It provides rich functions and flexible configuration options, making it easier to develop and manage Spring applications. The main functions of ApplicationContext include managing bean objects. ApplicationContext is responsible for managing all bean objects in the application, including bean objects defined in the configuration file and bean objects defined by annotations or other means. It is responsible for creating, configuring, and managing bean instances, and providing access and control to them. Through ApplicationContext, bean objects can be easily injected into other beans to realize dependencies between beans.

[0079] S32. Get the Class object of the general interface object.

[0080] In this embodiment, the Class object of the general interface object is the Class object of the general listening and reading class, and the Class object contains information related to the general listening and reading class. Reflection operations can be performed through the Class object.

[0081] S33. Obtain the method name and parameter type of the data batch insertion method to be called from the Class object.

[0082] In this embodiment, the Class object contains the method name and parameter type of each method in the interface object in the general listening reading class. Based on this, the method name and parameter type of the data batch insertion method to be called can be obtained from the Class object.

[0083] S34. Obtain the instantiated bean corresponding to the method name and parameter type through the ApplicationContext object. The bean is created through the parameterized construction method in the general listening reading class. The input parameters of the parameterized construction method include the first interface object.

[0084] In this embodiment, the bean obtained through the ApplicationContext object is a bean created based on the first interface object by listening to the parameterized construction method in the reading class.

[0085] S35. Based on the reflection mechanism, method name and parameter type, call the data batch insertion method in the first interface object from the bean.

[0086] In this embodiment, the bean corresponds to the first interface object. Based on this, after obtaining the bean and knowing the method name and parameter type of the data batch insertion method, the data batch insertion method in the first interface object can be obtained from the bean through the reflection mechanism.

[0087] After obtaining the data batch insertion method, the cache list is passed as a parameter to batch insert the parsed data stored in the cache list into the database through the data batch insertion method.

[0088] The Excel file import method based on EasyExcel provided in the embodiment of the present application utilizes a generic pre-designed monitoring reading class to obtain a general monitoring reading class that supports multiple data types. When importing an Excel file, the first interface object corresponding to the Excel file to be imported is passed in, and the data in the Excel file can be directly inserted into the database in batches through the pre-set general monitoring reading class based on the data batch insertion method in the first interface object. There is no need to create monitoring reading classes for tables of different data types respectively. The time required for importing Excel files is reduced, the import efficiency is improved, the redundancy of the code is reduced, and the code quality is improved.

[0089] In addition, in the invoke method, the cache list is used to store the parsed data. When the amount of data in the cache list is greater than the data volume threshold, the data batch insertion method is called to insert the data in the cache list into the database, thereby realizing batch insertion of data, thereby reducing various problems caused by insufficient memory.

[0090] Moreover, because generics are used, even when facing the import of complex data structures, such as importing Excel tables containing nested objects, collections, or mappings, the flexibility of generics can help parse these complex data structures and convert them into Java objects for processing, thereby realizing the import of complex data structures.

[0091] In addition, when the Excel file includes multiple tables, the import method provided in this embodiment can realize the simultaneous import of multiple tables.

[0092] In some embodiments, in order to improve the integrity of the imported data and reduce data omissions, the above method further includes:

[0093] After all the data in the Excel file is parsed, the saveData method is called through the doAfterAllAnalysed method;

[0094] Through the saveData method, the data batch insertion method in the first interface object is called based on the general interface object in the preset general listening reading class, and the parsed data in the cache list is imported into the database through the data batch insertion method.

[0095] The doAfterAllAnalysed method is used to call the saveData method after all the data in the Excel file has been parsed. For each import, the doAfterAllAnalysed method can be called after all the data has been parsed to ensure that no data is missed.

[0096] Since the method of importing Excel files provided in this embodiment is different from the traditional import method, the traditional doAfterAllAnalysed method is no longer used. Based on this, before executing the above step S11, Figure 2 As shown, the following step S26 may also be performed:

[0097] Rewrite the doAfterAllAnalysed method so that the doAfterAllAnalysed method can call the saveData method after all the data in the Excel file to be imported has been parsed.

[0098] In some embodiments of the present application, the doAfterAllAnalysed method can be overridden by annotating @Override.

[0099] The following describes the initialization process of EasyExcel in combination with specific application scenarios.

[0100] In the scenario of importing a simple Excel file through a general listening reading class, such as Figure 3 As shown, EasyExcel can be initialized through the following steps S41-S47:

[0101] S41. Create a generic listening reading class DemoListener <e>, and implement the interface ReadListener in the general listening reading class <e>.

[0102] S42. Read the DemoListener class in the general listening <e>In the private Object mapper, a mapper interface object of Object type is created as a general interface object.

[0103] S43. Through private List <e>dataList = new ArrayList<>(BATCH_COUNT) initializes a cache list list of generic type E, and initializes the field name used to set the data volume threshold to BATCH_COUNT().

[0104] S44. Create a general listening reading class DemoListener through public DemoDataListener(Object mapper){this.mapper=mapper;} <e>The parameterized constructor of the parameterized constructor takes the mapper interface object as input.

[0105] S45. Override the invoke method by annotating @Override so that the invoke method can store the parsed data into the cache list list, and when the amount of data stored in the cache list list is greater than the data amount threshold, call the saveData method and then clear the data in the cache list list.

[0106] S46. Override doAfterAllAnalysed with the @Override annotation so that the doAfterAllAnalysed method can call the saveData method.

[0107] S47. Create a saveData method so that the saveData method can obtain the bean instantiated by the parameterized constructor through the ApplicationContext object, and call the data processing method in the parameterized constructor from the bean through reflection.

[0108] In the scenario where a complex Excel file is imported through a general listening reading class, such as Figure 4 As shown, EasyExcel can be initialized through the following steps S51-S57:

[0109] S51. Create a generic general listening reading class DemoListener <e>, and implement the interface ReadListener in the general listening reading class <e>.

[0110] S52. Read the DemoListener class in the general listening <e>A serviceImpl class of Object type is created as a general interface object through private Object mapper.

[0111] S53. Through private List <e>dataList = new ArrayList<>(BATCH_COUNT) initializes a cache list list of generic type E, and initializes the field name used to set the data volume threshold to BATCH_COUNT().

[0112] S54. Create a general listening reading class DemoListener through public DemoDataListener(Object mapper){this.mapper=mapper;} <e>The parameterized constructor of the serviceImpl class is the input parameter of the parameterized constructor.

[0113] S55. Override the invoke method by annotating @Override so that the invoke method can store the parsed data into the cache list list, and when the amount of data stored in the cache list list is greater than the data amount threshold, call the saveData method and then clear the data in the cache list list.

[0114] S56. Override doAfterAllAnalysed through the @Override annotation so that the doAfterAllAnalysed method can call the saveData method.

[0115] S57. Create a saveData method so that the saveData method can obtain the bean instantiated by the parameterized constructor through the ApplicationContext object, and call the data processing method in the parameterized constructor from the bean through reflection.

[0116] Based on the Excel file import method based on EasyExcel provided in the above embodiment, accordingly, the present application also provides a specific implementation of an Excel file import device based on EasyExcel. Please refer to the following embodiment.

[0117] See also Figure 5 , is a schematic diagram of an Excel file importing device based on EasyExcel provided in an embodiment of the present application, such as Figure 5 As shown, the device 500 includes the following modules:

[0118] The acquisition module 501 is used to acquire the parameters of the Excel file to be imported, wherein the parameters at least include a first interface object corresponding to the Excel file, the first interface object is an instance of a preset universal listening reading class, the first interface object includes a data batch inserting method, and the universal listening reading class supports generics;

[0119] The parsing module 502 is used to parse each piece of data in the Excel file in turn through a general listening reading class;

[0120] The cache module 503 is used to store the parsed data corresponding to each data into the cache list through the invoke method after the parsing of each data is completed, and to call the saveData method when the amount of parsed data stored in the cache list is greater than the data amount threshold. The cache list supports generics;

[0121] The storage module 504 is used to call the data batch insertion method in the first interface object based on the reflection mechanism and the preset general interface object through the saveData method, and insert the parsed data in the cache list into the database using the data batch insertion method.

[0122] The Excel file import device based on EasyExcel in the embodiment of the present application uses a generic pre-designed monitoring and reading class to obtain a general monitoring and reading class that supports multiple data types. When importing an Excel file, the first interface object corresponding to the Excel file to be imported is passed in, and the data in the Excel file can be directly inserted into the database in batches through the pre-set general monitoring and reading class based on the data batch insertion method in the first interface object. There is no need to create monitoring and reading classes for tables of different data types respectively. The time required for importing Excel files is reduced, the import efficiency is improved, and the redundancy of the code is reduced, thereby improving the code quality.

[0123] In some embodiments, the storage module 504 is configured to:

[0124] Get the pre-created ApplicationContext object;

[0125] Get the Class object of the general interface object;

[0126] Get the method name and parameter type of the data batch insertion method to be called from the Class object;

[0127] Get the instantiated bean through the parameterized construction method. The bean is created through the parameterized construction method in the general listening reading class. The input parameters of the parameterized construction method include the first interface object.

[0128] Based on the reflection mechanism, method name and parameter type, the data batch insertion method in the first interface object is called from the bean.

[0129] In some embodiments, the apparatus 500 further includes an end calling module, configured to:

[0130] After all the data in the Excel file is parsed, the saveData method is called through the doAfterAllAnalysed method;

[0131] Through the saveData method, the data batch insertion method in the first interface object is called based on the general interface object in the preset general listening reading class, and the parsed data in the cache list is imported into the database through the data batch insertion method.

[0132] In some embodiments, when the number of tables contained in the Excel file is less than or equal to the number threshold, the first interface object and the general interface object both use the mapper interface object;

[0133] When the number of tables contained in the Excel file is greater than the quantity threshold, both the first interface object and the general interface object adopt the serviceImpl class, and the serviceImpl class includes at least one mapper interface object.

[0134] In some embodiments, the apparatus 500 further includes an initialization module, configured to:

[0135] Create a general listening and reading class that supports generics, and implement the listening and reading interface in the general listening and reading class;

[0136] Create a general interface object of type Object in the general listening reading class;

[0137] Initialize a cache list that supports generics and set the data volume threshold of the cache list;

[0138] Create a parameterized constructor for the general listening reading class. The parameterized constructor takes the interface object as an input parameter.

[0139] In some embodiments, the initialization module is further configured to:

[0140] Rewrite the invoke method so that the invoke method can store the parsed data into the cache list, and call the saveData method when the amount of parsed data stored in the cache list is greater than the data amount threshold.

[0141] In some embodiments, the initialization module is further configured to:

[0142] Rewrite the doAfterAllAnalysed method so that the doAfterAllAnalysed method can call the saveData method after all the data in the Excel file to be imported has been parsed.

[0143] In some embodiments, the initialization module is further configured to:

[0144] A saveData method is created so that the saveData method can call a data batch insertion method in the first interface object based on a reflection mechanism and a preset general interface object.

[0145] The Excel file import device based on EasyExcel provided in the embodiment of the present application can realize Figures 1 to 4 To avoid repetition, the various processes implemented by the method embodiment are not described here.

[0146] Figure 6 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application is shown.

[0147] The electronic device 600 may include a processor 601 and a memory 602 storing computer program instructions.

[0148] Specifically, the processor 601 may include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiments of the present application.

[0149] The memory 602 may include a large capacity memory for data or instructions. By way of example and not limitation, the memory 602 may include a hard disk drive (HDD), a floppy disk drive, a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive or a combination of two or more of these. In appropriate cases, the memory 602 may include a removable or non-removable (or fixed) medium. In appropriate cases, the memory 602 may be inside or outside the integrated gateway disaster recovery device. In a specific embodiment, the memory 602 is a non-volatile solid-state memory.

[0150] The memory 602 may include a read-only memory (ROM), a random access memory (RAM), a disk storage medium device, an optical storage medium device, a flash memory device, an electrical, optical or other physical / tangible memory storage device. Therefore, generally, the memory 602 includes one or more tangible (non-transitory) computer-readable storage media (e.g., a memory device) encoded with software including computer executable instructions, and when the software is executed (e.g., by one or more processors), it can perform any of the operations described in the Excel file import method based on EasyExcel in the above-mentioned embodiments.

[0151] The processor 601 implements any one of the Excel file importing methods based on EasyExcel in the above embodiments by reading and executing the computer program instructions stored in the memory 602 .

[0152] In one example, the electronic device 600 may further include a communication interface 603 and a bus 610. Figure 6 As shown, the processor 601, the memory 602, and the communication interface 603 are connected via a bus 610 and communicate with each other.

[0153] The communication interface 603 is mainly used to implement communication between various modules, devices, units and / or equipment in the embodiments of the present application.

[0154] Bus 610 includes hardware, software or both, and the parts of online data flow billing equipment are coupled to each other. For example, but not limitation, bus may include accelerated graphics port (AGP) or other graphics bus, enhanced industrial standard architecture (EISA) bus, front-end bus (FSB), hypertransport (HT) interconnection, industrial standard architecture (ISA) bus, infinite bandwidth interconnection, low pin count (LPC) bus, memory bus, micro channel architecture (MCA) bus, peripheral component interconnection (PCI) bus, PCI-Express (PCI-X) bus, serial advanced technology attachment (SATA) bus, video electronics standard association local (VLB) bus or other suitable bus or two or more of these combinations. In appropriate cases, bus 610 may include one or more buses. Although the present application embodiment describes and shows a specific bus, the present application considers any suitable bus or interconnection.

[0155] In addition, in combination with the Excel file import method based on EasyExcel in the above embodiment, the embodiment of the present application can provide a computer storage medium for implementation. The computer storage medium stores computer program instructions; when the computer program instructions are executed by the processor, any Excel file import method based on EasyExcel in the above embodiment is implemented.

[0156] An embodiment of the present application also provides a computer program product, including a computer program, which, when executed, implements any one of the methods for importing Excel files based on EasyExcel in the above embodiments.

[0157] It should be clear that the present application is not limited to the specific configuration and processing described above and shown in the figures. For the sake of simplicity, a detailed description of the known method is omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present application is not limited to the specific steps described and shown, and those skilled in the art can make various changes, modifications and additions, or change the order between the steps after understanding the spirit of the present application.

[0158] The functional blocks shown in the above-described block diagram can be implemented as hardware, software, firmware or a combination thereof. When implemented in hardware, it can be, for example, an electronic circuit, an application specific integrated circuit (ASIC), appropriate firmware, a plug-in, a function card, etc. When implemented in software, the elements of the present application are programs or code segments that are used to perform the required tasks. Programs or code segments can be stored in machine-readable media, or transmitted on a transmission medium or a communication link by a data signal carried in a carrier wave. "Machine-readable media" can include any medium capable of storing or transmitting information. Examples of machine-readable media include electronic circuits, semiconductor memory devices, ROM, flash memory, erasable ROM (EROM), floppy disks, CD-ROMs, optical disks, hard disks, optical fiber media, radio frequency (RF) links, etc. Code segments can be downloaded via computer networks such as the Internet, intranets, etc.

[0159] It should also be noted that the exemplary embodiments mentioned in this application describe some methods or systems based on a series of steps or devices. However, this application is not limited to the order of the above steps, that is, the steps can be performed in the order mentioned in the embodiment, or in a different order from the embodiment, or several steps can be performed simultaneously.

[0160] Aspects of the present disclosure are described above with reference to the flowchart and / or block diagram of the method, device (system) and computer program product according to the embodiment of the present disclosure. It should be understood that each box in the flowchart and / or block diagram and the combination of each box in the flowchart and / or block diagram can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device to produce a machine so that these instructions executed by the processor of the computer or other programmable data processing device enable the implementation of the function / action specified in one or more boxes of the flowchart and / or block diagram. Such a processor can be, but is not limited to, a general-purpose processor, a special-purpose processor, a special application processor, or a field programmable logic circuit. It can also be understood that each box in the block diagram and / or flowchart and the combination of boxes in the block diagram and / or flowchart can also be implemented by dedicated hardware that performs a specified function or action, or can be implemented by a combination of dedicated hardware and computer instructions.

[0161] The above is only a specific implementation of the present application. Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working processes of the systems, modules and units described above can refer to the corresponding processes in the aforementioned method embodiments, and will not be repeated here. It should be understood that the protection scope of the present application is not limited to this. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in this application, and these modifications or replacements should be included in the protection scope of this application.< / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e> < / e>

Claims

1. A method for importing Excel files based on EasyExcel, It is characterized in that include: Obtaining parameters of the Excel file to be imported, wherein the parameters at least include a first interface object corresponding to the Excel file, wherein the first interface object is an instance of a preset universal listening reading class, wherein the first interface object includes a data batch inserting method, and the universal listening reading class supports generics; Through the general listening reading class, each piece of data in the Excel file is parsed in turn; After parsing of each piece of data is completed, the parsed data corresponding to the data is stored in the cache list through the invoke method, and when the amount of parsed data stored in the cache list is greater than the data amount threshold, the saveData method is called, and the cache list supports generics; Through the saveData method, based on the reflection mechanism and the preset general interface object, the data batch insertion method in the first interface object is called, and the parsed data in the cache list is inserted into the database using the data batch insertion method.

2. The method according to claim 1, It is characterized in that The method of calling the data batch insertion method in the first interface object based on the preset general interface object through the saveData method includes: Get the pre-created ApplicationContext object; Get the Class object of the general interface object; Obtain the method name and parameter type of the data batch insertion method to be called from the Class object; Obtaining an instantiated bean through the parameterized construction method, wherein the bean is created through the parameterized construction method in the general listening reading class, and the input parameters of the parameterized construction method include the first interface object; Based on the reflection mechanism, the method name and the parameter type, the data batch insertion method in the first interface object is called from the bean.

3. The method according to claim 1, It is characterized in that The method further comprises: After all the data in the Excel file is analyzed, the saveData method is called through the doAfterAllAnalysed method; Through the saveData method, the data batch insertion method in the first interface object is called based on the general interface object in the preset general listening reading class, and the parsed data in the cache list is imported into the database through the data batch insertion method.

4. The method according to claim 1, It is characterized in that When the number of tables contained in the Excel file is less than or equal to the number threshold, the first interface object and the general interface object both adopt mapper interface objects; When the number of tables contained in the Excel file is greater than the number threshold, the first interface object and the general interface object both adopt the serviceImpl class, and the serviceImpl class includes at least one mapper interface object.

5. The method according to any one of claims 1 to 4, It is characterized in that Before obtaining the parameters of the Excel file to be imported, the method further includes: Create a general listening read class that supports generics, and implement the listening read interface in the general listening read class; Create a general interface object of Object type in the general listening and reading class; Initialize a cache list supporting generics, and set a data volume threshold of the cache list; Create a parameterized construction method of the general listening reading class, wherein the parameterized construction method takes the interface object as an input parameter.

6. The method according to claim 5, It is characterized in that Before obtaining the parameters of the Excel file to be imported, the method further includes: The invoke method is rewritten so that the invoke method can store the parsed data into the cache list, and when the amount of the parsed data stored in the cache list is greater than a data amount threshold, the saveData method is called.

7. The method according to claim 5, It is characterized in that Before obtaining the parameters of the Excel file to be imported, the method further includes: The doAfterAllAnalysed method is rewritten so that the doAfterAllAnalysed method can call the saveData method after all the data in the Excel file to be imported are analyzed.

8. The method according to claim 5, It is characterized in that Before obtaining the parameters of the Excel file to be imported, the method further includes: A saveData method is created so that the saveData method can call a data batch insertion method in the first interface object based on a reflection mechanism and a preset general interface object.

9. An Excel file import device based on EasyExcel, It is characterized in that include: An acquisition module, used for acquiring parameters of an Excel file to be imported, wherein the parameters at least include a first interface object corresponding to the Excel file, wherein the first interface object is an instance of a preset universal listening reading class, wherein the first interface object includes a method for inserting data in batches, and the universal listening reading class supports generics; A parsing module, used to parse each piece of data in the Excel file in turn through the general listening reading class; The cache module is used to store the parsed data corresponding to each piece of data into a cache list through the invoke method after the parsing of the data is completed, and to call the saveData method when the amount of parsed data stored in the cache list is greater than the data amount threshold, and the cache list supports generics; The storage module is used to call the data batch insertion method in the first interface object based on the reflection mechanism and the preset general interface object through the saveData method, and use the data batch insertion method to insert the parsed data in the cache list into the database.

10. An electronic device, It is characterized in that The electronic device comprises: a processor and a memory storing computer program instructions; When the processor executes the computer program instructions, it implements the Excel file import method based on EasyExcel as described in any one of claims 1 to 8.

11. A computer storage medium, It is characterized in that The computer storage medium stores computer program instructions, and when the computer program instructions are executed by the processor, the Excel file import method based on EasyExcel as described in any one of claims 1 to 8 is implemented.

12. A computer program product, It is characterized in that When the instructions in the computer program product are executed by a processor of an electronic device, the electronic device executes the Excel file import method based on EasyExcel as described in any one of claims 1 to 8.