package com.qmth.boot.test.tools.excel.write;

import com.qmth.boot.test.tools.excel.model.ExcelTestEntity;
import com.qmth.boot.test.tools.excel.model.GenderEnum;
import com.qmth.boot.tools.excel.ExcelWriter;
import com.qmth.boot.tools.excel.enums.ExcelType;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;

public class ExcelWriteTest {

    @Test
    public void testXlsx() throws IOException {
        ExcelTestEntity entity = new ExcelTestEntity();
        entity.setId(System.currentTimeMillis());
        entity.setName("name");
        entity.setScore(95.5);
        entity.setGender(GenderEnum.FEMALE);
        entity.setTime(new Date());
        entity.setEnable(true);
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
        writer.writeObjects("sheet1", new String[] { "标题" }, ExcelTestEntity.class,
                Collections.singletonList(entity).iterator());
        writer.output(ous);
        Assert.assertTrue(ous.toByteArray().length > 0);
    }

    @Test
    public void testXls() throws IOException {
        ExcelTestEntity entity = new ExcelTestEntity();
        entity.setId(System.currentTimeMillis() * 100);
        entity.setName("name");
        entity.setScore(65d);
        entity.setGender(GenderEnum.MALE);
        entity.setTime(new Date());
        entity.setEnable(false);
        entity.setClassName("   ");
        entity.setTeacher("!@#");
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        ExcelWriter writer = ExcelWriter.create(ExcelType.XLS);
        writer.writeDataArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" },
                Collections.singletonList(new String[] { "1", "张三" }).iterator());
        writer.output(ous);
        Assert.assertTrue(ous.toByteArray().length > 0);
    }
}
