package com.jfinal.club._admin.sample;

import com.jfinal.club.common.model.Sample;
import com.jfinal.club.util.Cons;
import com.jfinal.club.util.ExcelUtils;
import com.jfinal.kit.PathKit;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @Auther: LiuChao
 * @Date: 2021/3/2 20:04
 * @Description:
 */
public class AdminSampleService {



    /**
     * 生成excel  文件
     */
    public void createExcel(List<Sample> list) {

            //excel标题
            String[] title = {"用户名","公司","手机","邮箱","申请产品","备注","申请时间"};
            //sheet名
            String sheetName = "样品申请";

            if(list == null && list.size() <= 0){
                return;
            }

            String [][] content = new String[list.size()][7];

            for (int i = 0; i < list.size(); i++) {
                content[i] = new String[title.length];
                Sample obj = list.get(i);
                content[i][0] = obj.getName();
                content[i][1] = obj.getCompany();
                content[i][2] = obj.getMobile();
                content[i][3] = obj.getEmail();
                content[i][4] = obj.getProducts();
                content[i][5] = obj.getRemark();
                content[i][6] = DateFormatUtils.format(obj.getCreateTime(),"yyyy-MM-dd hh:mm");
            }

            //创建HSSFWorkbook
            HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            FileOutputStream out = new FileOutputStream(new File(PathKit.getWebRootPath(),"upload"+Cons.EXPORT_SAMPLE));
            wb.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
