package shop.alien.util.excel;

import com.alibaba.excel.EasyExcel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * EasyExcel 2.2.10 写入测试
 * 
 * @author ssk
 * @version 2.0
 * @date 2020/7/21 17:18
 */
public class ExcelWriteTest {

    /**
     * 每行数据是List<String>无表头
     *
     * @throws IOException
     */
    @Test
    public void writeWithoutHead() throws IOException {
        String fileName = "d://withoutHead.xlsx";
        List<List<String>> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            List<String> item = new ArrayList<>();
            item.add("item0" + i);
            item.add("item1" + i);
            item.add("item2" + i);
            data.add(item);
        }
        EasyExcel.write(fileName)
                .sheet("sheet1")
                .doWrite(data);
    }

    /**
     * 带表头写入
     *
     * @throws IOException
     */
    @Test
    public void writeWithHead() throws IOException {
        String fileName = "D://withHead.xlsx";
        List<UserExcel> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            UserExcel item = new UserExcel();
            item.name = "name" + i;
            item.age = "age" + i;
            item.email = "email" + i;
            item.address = "address" + i;
            item.sax = "sax" + i;
            item.height = "height" + i;
            item.last = "last" + i;
            data.add(item);
        }
        EasyExcel.write(fileName, UserExcel.class)
                .sheet("sheet1")
                .doWrite(data);
    }
}
