Excel Office - [ I ] use EasyExce to realize data "write with object" and "write without object"

Keywords: Java JavaEE easyexcel

Write in front

Recently, in project development, easy excel technology was used to analyze and write excel, so today I share with you how to use easy excel technology to analyze Excel files

Introduction to Easyexcel Technology

Easyexcel technology is developed on the basis of poi. It is the optimization and promotion of poi '. Compared with poi, the advantage of easyexcel is that when reading and writing files, it reads them line by line. The advantage of this is that when data is stored in the database, it can well avoid memory consumption and occupy more CPU resources. Moreover, compared with poi, the operation of easyexcel is more convenient. For the same operation, easyexcel has less code.

Easyexcel realizes simple write operation

There are many ways to use easyexcel to realize write operation, among which the most commonly used are object write and no object write. Next, I will introduce these two methods to you respectively.

Object write

Object writing in easyexcel means that you need to specify what type of data to write. This writing method is also relatively simple. Let's demonstrate it with an example below.
First, create an instance object DemoData class, as follows:

/**
 * Basic data demodata
 *
 */
@Data
public class DemoData {
    @ExcelProperty(value = "String title")
    private String stringTitle;
    @ExcelProperty(value = "Time title")
    private Date dateTitle;
    @ExcelProperty(value = "Digital title")
    private int doubleTitle;
}

There are three properties in this class. We take them as our header. In easyexcel, you can use @ ExcelProperty() annotation to specify the header content of this property after it is written to Excel,
Then, for writing data to Excel, we can directly call the Write() method of Easyexcel. The specific usage is as follows:

    public void writeDataToExcel_01() {
        log.info("Write data to excel´╝îCommon writing");
        /**
         * Get written data
         */


        log.info("Writing is about to begin[" + demoData.size() + "]Data bar");
        EasyExcel.write(FILEPATH + "testExcel_1.xlsx", DemoData.class).sheet("testSheet01").doWrite(demoData);
        log.info("[" + demoData.size() + "]Data written successfully!");
    }

You need to specify the file path and data type to be written in write(), then specify the name of the sheet to be written through the setSheet() method, and finally pass in the list set of data to be written in dowrite(). The results are as follows:

No object write

In fact, writing without objects is simpler. We do not need to specify the type of data to be written. We can directly pass in a list to store data to complete the data writing operation. However, both header and content need to be passed in the form of list,
Examples are as follows:

    /**
     * Do not create write to object
     */
    @Test
    public void noModelWrite() {
        // Writing method 1
        String fileName =  "testExcel_1.xlsx";
        // Here, you need to specify which class to write, and then write to the first sheet with the name of template, and then the file stream will be closed automatically
        EasyExcel.write(fileName).head(head()).sheet("Template").doWrite(dataList());
    }

    private List<List<String>> head() {
        List<List<String>> list = ListUtils.newArrayList();
        List<String> head0 = ListUtils.newArrayList();
        head0.add("character string" + System.currentTimeMillis());
        List<String> head1 = ListUtils.newArrayList();
        head1.add("number" + System.currentTimeMillis());
        List<String> head2 = ListUtils.newArrayList();
        head2.add("date" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    private List<List<Object>> dataList() {
        List<List<Object>> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            List<Object> data = ListUtils.newArrayList();
            data.add("character string" + i);
            data.add(new Date());
            data.add(0.56);
            list.add(data);
        }
        return list;
    }

One thing to note here is: when writing without object, for the header data stored in the list, the header of each column needs to be stored separately in a list, otherwise the header of the written table will be arranged vertically. No, you can try!

The above is the operation of using easyexcel to write data with and without objects. There are many operations about easyexcel, and I'll share them with you after listening to the little ape~
I think it's good. Remember to praise the collection, and then continue to share more practical skills about easyexcel,
I'm the grey ape. I'll see you next time!

Posted by zplits on Thu, 25 Nov 2021 13:21:09 -0800