package com.diagbot.util;

import com.diagbot.entity.operationLog;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class ReadExcelUtils {
    public static List<operationLog> read2007Xlsx(InputStream in) throws IOException, InvalidFormatException, ParseException {

        List<operationLog> list = new ArrayList<>();
        XSSFWorkbook xWorkbook = new XSSFWorkbook(in);
        // Read the Sheet
        XSSFSheet xssfSheet = xWorkbook.getSheetAt(0);

        // Read the Row
        for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
            XSSFRow xssfRow = xssfSheet.getRow(rowNum);
            if (xssfRow != null) {
                operationLog operationLog =new operationLog();
                // 单元格从 0 开始
                xssfRow.getCell(0).setCellType(CellType.STRING);
                xssfRow.getCell(1).setCellType(CellType.STRING);
                xssfRow.getCell(2).setCellType(CellType.STRING);
                xssfRow.getCell(3).setCellType(CellType.STRING);
                xssfRow.getCell(4).setCellType(CellType.STRING);
                xssfRow.getCell(5).setCellType(CellType.STRING);
                xssfRow.getCell(6).setCellType(CellType.STRING);
                operationLog.setId(xssfRow.getCell(0).getStringCellValue());
                operationLog.setGmtCreate(xssfRow.getCell(1).getStringCellValue());
                operationLog.setType(xssfRow.getCell(2).getStringCellValue());
                operationLog.setModule(xssfRow.getCell(3).getStringCellValue());
                operationLog.setAccount(xssfRow.getCell(4).getStringCellValue());
                operationLog.setIPAddress(xssfRow.getCell(5).getStringCellValue());
                operationLog.setContent(xssfRow.getCell(6).getStringCellValue());
                list.add(operationLog);

            }

        }

        xWorkbook.close();
        return list;
    }

}
