package com.post.excel;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class ExcelTest {
    public static void main(String[] args) throws Exception {

//        if (true) {
//            test02();
//            return;
//        }
//        File file = new File("C:\\test\\postcheck\\1、岗位责任清单-办公室主任.xlsx");
        File file = new File("C:\\test\\postcheck\\1234.xlsx");
//        File file = new File("C:\\test\\另存为了.xlsx");
//        File file = new File("C:\\test\\postcheck\\2、岗位责任制检查表（模板）1225-方联殷V2.xlsx");
        InputStream inputStream = new FileInputStream(file);

        ExcelUtilsPostcheck excelUtilsPostcheck = new ExcelUtilsPostcheck(ExcelParseBean.class);
//        List<ExcelBean> beanList = excelUtils.importExcel(inputStream);
        List<ExcelParseBean> beanList = excelUtilsPostcheck.importExcel(inputStream, -1);
//        List<ExcelBean> beanList = excelUtils.importExcel(null,inputStream,3);
        log.error("=====================================解析到 {} 行=====================================", beanList.size());

        for (int i = 1; i <= beanList.size(); i++) {
            ExcelParseBean excelParseBean = beanList.get(i - 1);
            log.error("{} 行数据 {}", i, excelParseBean);
        }

        log.error("======================================解析到结束 {} 行=================================", beanList.size());
    }

    public static void test02() throws Exception {
        File file = new File("C:\\test\\postcheck\\1234.xlsx");
//        File file = new File("C:\\test\\另存为了.xlsx");
//        File file = new File("C:\\test\\postcheck\\2、岗位责任制检查表（模板）1225-方联殷V2.xlsx");
        InputStream inputStream = new FileInputStream(file);
        Workbook wb = WorkbookFactory.create(inputStream);

        DataFormatter dataFormatter = new DataFormatter();
        FormulaEvaluator fe = wb  //bbb是Workbook 对象
                .getCreationHelper()
                .createFormulaEvaluator();

        Sheet sheet = getFirstNotHiddenSheet(wb); // 第一个不隐藏的sheet


        System.out.println(wb.getActiveSheetIndex());// 当前选定的sheet 可能是第二个

//        Sheet sheet = wb.getSheetAt(1);
//        boolean hidden = wb.isHidden();
//        System.out.println("hidden = " + hidden);
//        boolean sheetVeryHidden = wb.isSheetVeryHidden(0);
//        System.out.println("sheetVeryHidden = " + sheetVeryHidden);
//
//        boolean sheetVeryHidden = ;
        System.out.println("0 sheetVeryHidden = " + wb.isSheetVeryHidden(0));
        System.out.println("1 sheetVeryHidden = " + wb.isSheetVeryHidden(1));
        System.out.println("2 sheetVeryHidden = " + wb.isSheetVeryHidden(2));

        Iterator<Row> rowIterator = sheet.rowIterator();
        int index = 1;
        while (rowIterator.hasNext()) {


            Row row = rowIterator.next();
//        Row row = sheet.getRow(0);
            //cell是对应单元格

            String dataStr = "";
            for (int i = 0; i < 2; i++) {

                Cell cell = row.getCell(0);
                String a = dataFormatter.formatCellValue(cell, fe);
                dataStr += a + "\t";
            }
            System.out.println(index++ + "\t" + dataStr);
        }

        wb.close();

    }

    private static Sheet getFirstNotHiddenSheet(Workbook wb) {
        Sheet sheet = wb.getSheetAt(0);

        try {
            int index = 0;

            while (true && index < 10) {
                boolean sheetHidden = wb.isSheetHidden(index);

                if (!sheetHidden) {
                    System.out.println("sheet is show" + index);

                    sheet = wb.getSheetAt(index);

                    break;

                }

                System.out.println("sheet is hidden");

                index++;

            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return sheet;

    }
}
