package com.ruoyi.web.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;

public class MarjorMatchResultWriter {
    static String[] headers = "匹配,计ID,组id,年度,级别,类型,备选,选择,编辑,状态,计专项,专专项,计院校,专院校,计专业,专专业,计学制,专学制,计专业备注,专专业备注,计无效,专无效,计备注1,专备注1,计备注2,专备注2,计包含,专包含,计类别,专类别".split(",");
    SXSSFWorkbook wb = new SXSSFWorkbook(100);
    Sheet sheet = null; // 工作表对象
    Integer pageRowNo = 0;

    public MarjorMatchResultWriter() {

    }

    public String[] getHeaders() {
        return headers;
    }

    public void appendSheet(String name) {
        sheet = wb.createSheet(name);// 建立新的sheet对象
        Row nRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cel0 = nRow.createCell(i);
            cel0.setCellValue(headers[i]);
        }
        pageRowNo = 1;
    }

    public void appendRow(String[] datas) {
        Row nRow = sheet.createRow(pageRowNo++); // 新建行对象
        for (int i = 0; i < datas.length; i++) {
            Cell nCell = nRow.createCell(i);
            nCell.setCellValue(datas[i]);
        }
    }

    public void write(OutputStream out) throws IOException {
        // FileOutputStream fOut = new FileOutputStream(excelFile);//将数据写入Excel
        wb.write(out);
        out.flush(); // 刷新缓冲区
        out.close();
    }
}
