TestExcel.java 2.74 KB
import com.yanzuoguang.excel.*;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class TestExcel {

    @Test
    public void testExcel() {
        List<TextExcelRow> list = new ArrayList<>();
        list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光", "18641143400"));
        list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光1", "18641143401"));
        list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光2", "18641143402"));
        list.add(new TextExcelRow("订单2", "长江索道单程票", 2, 40, "颜佐", "18651143401"));
        list.add(new TextExcelRow("订单2", "长江索道单程票", 2, 40, "颜佐1", "18651143402"));

        ExportData config = new ExportData();
        config.setServerPath("./target/");
        config.setFileName("测试文件.xlsx");
        config.setTitle("测试文件");
        config.setSubTitle("子标题");
        config.setLine(true);
        config.setDownFileName(String.format("%s-%s.xlsx", config.getTitle(), config.getSubTitle()));
        config.getColumns().add(new ExportColumn("订单编号", "orderId", true, (short) 250));
        config.getColumns().add(new ExportColumn("产品名称", "productName", true, (short) 250));
        config.getColumns().add(new ExportColumn("购买数量", "buyNum", true, (short) 250));
        config.getColumns().add(new ExportColumn("购买金额", "buyMoney", true, (short) 250));
        config.getColumns().add(new ExportColumn("游客名称", "visitorName", false, (short) 250));
        config.getColumns().add(new ExportColumn("游客手机", "visitorMobile", false, (short) 250));

        FileHelper.checkFolder(config.getServerPath());
        FileHelper.checkFolder(config.getFileName());
        FileHelper.checkFolder(config.getDownFileName());

        config.getEndRows().add(new ExcelDefineRow(
                (short) 40,
                new ExcelDefineCell((short) 0, "合计", StringHelper.EMPTY),
                new ExcelDefineCell((short) 2, "SUM({buyNum})"),
                new ExcelDefineCell((short) 3, "SUM({buyMoney})")
        ));
        ExcelConsole<TextExcelRow> excel = new ExcelConsole<>(config, new ExcelStatus<TextExcelRow>() {
            @Override
            public void excelInit(ExcelConsole<TextExcelRow> excel) {
            }

            @Override
            public void excelRow(TextExcelRow excel, int rowIndex) {
            }

            @Override
            public void excelFinish(ExcelConsole<TextExcelRow> excel) {
            }
        });
        excel.open();
        list.forEach(excel::handle);
        excel.save();
    }
}